volume upload: fixed circular reference error while generating json

This commit is contained in:
Rajani Karuturi 2015-01-13 10:41:39 +05:30
parent 2c169bc02d
commit 1ae34d98d4
3 changed files with 57 additions and 20 deletions

View File

@ -0,0 +1,52 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cloudstack.storage.command;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import org.apache.cloudstack.engine.subsystem.api.storage.DataObject;
import org.apache.cloudstack.engine.subsystem.api.storage.EndPoint;
import java.lang.reflect.Type;
public class TemplateOrVolumePostUploadCommandTypeAdapter implements JsonSerializer<TemplateOrVolumePostUploadCommand> {
@Override public JsonElement serialize(TemplateOrVolumePostUploadCommand src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject obj = new JsonObject();
EndPoint endPoint = src.getEndPoint();
JsonObject endpointJSON = new JsonObject();
endpointJSON.addProperty("id", endPoint.getId());
endpointJSON.addProperty("hostAddress", endPoint.getHostAddr());
endpointJSON.addProperty("publicAddr", endPoint.getPublicAddr());
obj.add(endPoint.getClass().getName(), endpointJSON);
DataObject dataObject = src.getDataObject();
JsonObject dataobjectJSON = new JsonObject();
dataobjectJSON.addProperty("id", dataObject.getId());
dataobjectJSON.addProperty("size", dataObject.getSize());
dataobjectJSON.addProperty("uuid", dataObject.getUuid());
dataobjectJSON.addProperty("type", String.valueOf(dataObject.getType()));
obj.add(dataObject.getClass().getName(), dataobjectJSON);
return obj;
}
}

View File

@ -29,14 +29,13 @@ import java.util.concurrent.ExecutionException;
import javax.inject.Inject;
import com.cloud.utils.EncryptionUtil;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.cloudstack.api.command.user.volume.GetUploadParamsForVolumeCmd;
import org.apache.cloudstack.api.response.GetUploadParamsResponse;
import org.apache.cloudstack.engine.subsystem.api.storage.EndPoint;
import org.apache.cloudstack.storage.command.TemplateOrVolumePostUploadCommand;
import org.apache.cloudstack.storage.command.TemplateOrVolumePostUploadCommandTypeAdapter;
import org.apache.log4j.Logger;
import org.apache.cloudstack.api.command.user.volume.AttachVolumeCmd;
@ -319,14 +318,8 @@ public class VolumeApiServiceImpl extends ManagerBase implements VolumeApiServic
/*
* encoded metadata using the post upload config ssh key
*/
Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
@Override public boolean shouldSkipField(FieldAttributes f) {
return f.getDeclaredType().getClass().isInstance(Logger.class);
}
@Override public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
}).create();
Gson gson = new GsonBuilder().registerTypeAdapter(TemplateOrVolumePostUploadCommand.class, new TemplateOrVolumePostUploadCommandTypeAdapter()).create();
String jsonPayload = gson.toJson(command);
response.setMetadata(EncryptionUtil.encodeData(jsonPayload, key));

View File

@ -34,13 +34,12 @@ import javax.inject.Inject;
import javax.naming.ConfigurationException;
import com.cloud.utils.EncryptionUtil;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.cloudstack.api.command.user.template.GetUploadParamsForTemplateCmd;
import org.apache.cloudstack.api.response.GetUploadParamsResponse;
import org.apache.cloudstack.storage.command.TemplateOrVolumePostUploadCommand;
import org.apache.cloudstack.storage.command.TemplateOrVolumePostUploadCommandTypeAdapter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
@ -366,14 +365,7 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
/*
* encoded metadata using the post upload config ssh key
*/
Gson gson = new GsonBuilder().setExclusionStrategies(new ExclusionStrategy() {
@Override public boolean shouldSkipField(FieldAttributes f) {
return f.getDeclaredType().getClass().isInstance(Logger.class);
}
@Override public boolean shouldSkipClass(Class<?> clazz) {
return false;
}
}).create();
Gson gson = new GsonBuilder().registerTypeAdapter(TemplateOrVolumePostUploadCommand.class, new TemplateOrVolumePostUploadCommandTypeAdapter()).create();
String jsonPayload = gson.toJson(payload);
response.setMetadata(EncryptionUtil.encodeData(jsonPayload, key));