Skip to content
This repository has been archived by the owner on Sep 23, 2020. It is now read-only.

Commit

Permalink
Make query interface XML more like EC2
Browse files Browse the repository at this point in the history
but hybridfox still doesn't work entirely...
  • Loading branch information
labisso committed Dec 10, 2010
1 parent 3b9243a commit 4b90997
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
Expand Up @@ -18,6 +18,7 @@
import org.apache.axis.description.TypeDesc;
import org.apache.axis.message.MessageElement;
import org.apache.axis.encoding.SerializationContext;
import org.xml.sax.Attributes;

import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
Expand All @@ -30,6 +31,7 @@
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand All @@ -41,6 +43,10 @@ public class AxisBodyWriter implements MessageBodyWriter<Object> {
private static final String GET_TYPE_DESC = "getTypeDesc";
private static final String TYPE_SUFFIX = "Type";

@javax.ws.rs.core.Context
javax.ws.rs.core.UriInfo uriInfo;


// this is a travesty

public boolean isWriteable(Class<?> aClass, Type type,
Expand Down Expand Up @@ -73,16 +79,24 @@ public void writeTo(Object o, Class aClass, Type type,
name = name.substring(0, name.length() - TYPE_SUFFIX.length());
}

String namespace = getNamespaceUriFromContext();
if (namespace == null) {
namespace = qName.getNamespaceURI();
}

final OutputStreamWriter writer =
new OutputStreamWriter(outputStream);

MessageElement element = new MessageElement(qName.getNamespaceURI(), name);
//manually write the xml header deal
writer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

MessageElement element = new MessageElement(name, "", namespace);
try {
element.setObjectValue(o);

final SerializationContext context =
new SerializationContext(writer);
//context.setSendDecl(false);
new CleanSerializationContext(writer, namespace);
context.setDoMultiRefs(false);
context.setPretty(true);
element.output(context);

Expand All @@ -95,6 +109,21 @@ public void writeTo(Object o, Class aClass, Type type,
writer.close();
}

private String getNamespaceUriFromContext() {
if (this.uriInfo != null) {
final MultivaluedMap<String, String> queryParameters =
this.uriInfo.getQueryParameters();
if (queryParameters != null) {
final String version = queryParameters.getFirst("Version");

if (version != null && version.length() > 0) {
return "http://ec2.amazonaws.com/doc/" + version + "/";
}
}
}
return null;
}

private static TypeDesc getTypeDescVerySlowly(Class aClass) {
try {

Expand All @@ -112,3 +141,25 @@ private static TypeDesc getTypeDescVerySlowly(Class aClass) {
}

}

class CleanSerializationContext extends SerializationContext {
private final String defaultNamespace;

public CleanSerializationContext(Writer writer, String defaultNamespace) {
super(writer);
this.defaultNamespace = defaultNamespace;
}

public boolean shouldSendXSIType() {
return false;
}

public void startElement(QName qName, Attributes attributes) throws IOException {
// for some reason many of the EC2 types are coming through with an empty namespace
// override it with the one provided at constructor time
if ("".equals(qName.getNamespaceURI())) {
qName = new QName(defaultNamespace, qName.getLocalPart());
}
super.startElement(qName, attributes);
}
}
Expand Up @@ -68,7 +68,7 @@ public ElasticService(ServiceRM serviceRM, ServiceGeneral serviceGeneral,
new CreateKeyPair(), new DeleteKeyPair(), new DescribeKeyPairs(),
new RunInstances(), new RebootInstances(), new DescribeInstances(),
new TerminateInstances(), new DescribeImages(),
new DescribeAvailabilityZones()
new DescribeAvailabilityZones(), new DescribeSecurityGroups()
};
actionMap = new HashMap<String, ElasticAction>(actions.length);
for (ElasticAction action : actions) {
Expand Down Expand Up @@ -96,6 +96,7 @@ public ElasticAction handleAction(@FormParam("Action") String action) {


@Path("/")
@Produces("text/xml")
public class CreateKeyPair implements ElasticAction {
public String getName() {
return "CreateKeyPair";
Expand All @@ -119,6 +120,7 @@ public CreateKeyPairResponseType handlePost(@FormParam("KeyName") String keyName
}
}

@Produces("text/xml")
public class DeleteKeyPair implements ElasticAction {
public String getName() {
return "DeleteKeyPair";
Expand All @@ -142,6 +144,7 @@ public DeleteKeyPairResponseType handlePost(@FormParam("KeyName") String keyName
}
}

@Produces("text/xml")
public class DescribeKeyPairs implements ElasticAction {
public String getName() { return "DescribeKeyPairs"; }

Expand All @@ -168,6 +171,7 @@ public DescribeKeyPairsResponseType handlePost(@Context UriInfo uriInfo) {
}
}

@Produces("text/xml")
public class RunInstances implements ElasticAction {
public String getName() {
return "RunInstances";
Expand Down Expand Up @@ -218,6 +222,7 @@ public RunInstancesResponseType handlePost(
}
}

@Produces("text/xml")
public class RebootInstances implements ElasticAction {
public String getName() {
return "RebootInstances";
Expand Down Expand Up @@ -256,6 +261,7 @@ public RebootInstancesResponseType handlePost(@Context UriInfo uriInfo) {
}
}

@Produces("text/xml")
public class DescribeInstances implements ElasticAction {
public String getName() {
return "DescribeInstances";
Expand Down Expand Up @@ -289,6 +295,7 @@ public DescribeInstancesResponseType handlePost(@Context UriInfo uriInfo) {
}
}

@Produces("text/xml")
public class TerminateInstances implements ElasticAction {
public String getName() {
return "TerminateInstances";
Expand Down Expand Up @@ -328,6 +335,7 @@ public TerminateInstancesResponseType handlePost(@Context UriInfo uriInfo) {
}
}

@Produces("text/xml")
public class DescribeImages implements ElasticAction {
public String getName() {
return "DescribeImages";
Expand Down Expand Up @@ -385,6 +393,7 @@ public DescribeImagesResponseType handlePost(
}
}

@Produces("text/xml")
public class DescribeAvailabilityZones implements ElasticAction {
public String getName() {
return "DescribeAvailabilityZones";
Expand Down Expand Up @@ -419,4 +428,20 @@ public DescribeAvailabilityZonesResponseType handlePost(
return handleGet(zoneName);
}
}

@Produces("text/xml")
public class DescribeSecurityGroups implements ElasticAction {
public String getName() {
return "DescribeSecurityGroups";
}

@GET
public DescribeSecurityGroupsResponseType handleGet() {
return new DescribeSecurityGroupsResponseType(null, new SecurityGroupSetType(new SecurityGroupItemType[0]));
}
@POST
public DescribeSecurityGroupsResponseType handlePost() {
return handleGet();
}
}
}

0 comments on commit 4b90997

Please sign in to comment.