SOE REST Web Service - How to return different MIME content types? java

406
1
01-22-2013 08:57 PM
prettyboy123123prettyboy123123
New Contributor
Folks,

I posted this in the ArcObjects forum, but thought I'd see if anyone here might have an idea, too.

After upgrading to 10.0 Release, and making the requisite changes to my REST operation's method signature (by adding an additional parameter, requestProperties), my SOE no longer returns a PNG image that clients can use; instead, it returns the PNG's byte array representation as text.
I've spent some time today looking into the cause of this, and it seems like this is the case:

1) At 10.0 Prerelease, my SOE's HTTP response contained no content type.
2) At 10.0 Release, the response contains this content type: "text/plain;charset=utf-8"

To my surprise, I noticed a new method- handleRESTRequest and parameter - ring[] responseProperties. Looking at its documentation in the object browser, this seemed to be something useful, and might just give me what I need. However, I'm not actually able to get it to work. When I try something like this...

@Override
 public byte[] handleRESTRequest(String capabilities, String resourceName,
   String operationName, String operationInput, String outputFormat,
   String requestProperties, String[] responseProperties)
   throws IOException, AutomationException {

  /*
   * This method handles REST requests by determining whether an operation
   * or resource has been invoked and then forwards the request to
   * appropriate methods.
   */

  try
  {
   // if no operationName is specified send description of specified
   // resource
   if (operationName.length() == 0)
   {
    return getResource(resourceName);
   }
   else
   // invoke REST operation on specified resource
   {
    return invokeRESTOperation(capabilities, resourceName, operationName, operationInput, outputFormat,
      requestProperties, responseProperties);
   }
  }
  catch (Exception e)
  {
   return ServerUtilities.sendError(0, "Exception occurred: " + e.getMessage(), null).getBytes("utf-8");
  } 
 }



private byte[] invokeRESTOperation(String capabilitiesList, String resourceName, String operationName,
   String operationInput, String outputFormat, String requestProperties, String[] responseProperties) throws Exception
 {
  JSONObject response=null;
  
  byte[] operationOutput = null;
  JSONObject operationInputAsJSON = new JSONObject(operationInput);

  
  
  //parse request properties and create a map
  java.util.Map<String, String> requestPropertiesMap = new HashMap<String, String>();
  if (requestProperties != null && requestProperties.length() > 0)
  {
   JSONObject requestPropertiesJSON = new JSONObject(requestProperties);
   Iterator<String> jsonKeys = requestPropertiesJSON.keys();
   while (jsonKeys.hasNext())
   {
    String key = jsonKeys.next();
    requestPropertiesMap.put(key, requestPropertiesJSON.getString(key));
   }
  }
  //create a Map to hold response properties
  java.util.Map<String, String> responsePropertiesMap = new HashMap<String, String>(); 
  if (resourceName.equalsIgnoreCase("") || resourceName.length() == 0)
  {       
   if (operationName!=null)
   {
    response= new JSONObject();
    MapWMSServer mapWMSServer=new MapWMSServer();
    operationOutput =mapWMSServer.doPost(operationInputAsJSON,operationName, response, mapServer);
   }
   
   else
   {
    operationOutput = sendError(0, "Operation " + "\"" + operationName + "\" not supported on resource " + resourceName + ".", 
      new String[]{"No other details", " specified"}).getBytes();     
   }    
  }
  else //if non existent sub-resource specified, report error
  {
   operationOutput = sendError(0, "No sub-resource by name \"" + resourceName + "\" found.", new String[]{""}).getBytes("utf-8"); 
  }
 
  
  if(response!=null){
   /*responseProperties = new String[response.length()];
   Iterator it=response.keys();
   int i=0;
   while(it.hasNext()){
    String key = (String)it.next();
    String value =  response.getString(key);
    //responseProperties="\""+ key+"\":\""+value+"\"";
    responseProperties = key + "=" + value;
    i++;
   }*/
   /*if(!response.isNull("Content-Type")&& response.getString("Content-Type")!=null)
      responseProperties[0]="\"Content-Type\":\""+response.getString("Content-Type")+"\"";*/
   //responseProperties= new String[]{response.toString()};
  }
  JSONObject resp=new JSONObject();
  resp.put("Content-Type", "image/png");
  responseProperties = new String[]{resp.toString()};
  String ss=responseProperties[0];
  serverLog.addMessage(1, 800, ss);
  //convert response properties to String array
  /*if (!responsePropertiesMap.isEmpty())
  {
   Set<String> keys = responsePropertiesMap.keySet();
   Iterator<String> keysIterator = keys.iterator();
   int i = 0;
   while (keysIterator.hasNext())
   {
    String key = keysIterator.next();
    String value = responsePropertiesMap.get(key);
    responseProperties = key + "=" + value;
    i++;
   }
  }*/
  
  return operationOutput; 
 } 
Tags (2)
0 Kudos
1 Reply
nicogis
MVP Frequent Contributor
0 Kudos