2 Replies Latest reply on Aug 23, 2013 9:41 AM by vivedha

    HTTP Status 415 - Cannot consume content type error in RESTEasy WAR

    vivedha

      Hi all,

      I generated a RESTEasy WAR using Eclipse indigo 4.2.1, teiid designer 7.8,Teiid runtime 7.7.5 and implementing insert,update and select operation refering the links 

      http://docs.jboss.org/teiid/designer/7.4/user-guide/en-US/html/webservice-generators-chapter.html

      After deployment, GET(Select) operation is working fine, while the POST(insert) and PUT(update) operation gives an error

      HTTP Status 415 - Cannot consume content type


      Type: Status report

       

      message: Cannot consume content type

       

      description: The server refused this request because the request entity is in a format not supported by the requested resource for the requested method (Cannot consume content type).

      IF there is any error the file or i nput ..please guide..

       

      My input(for update):

      <input>

      <Source>Chennai</Source>

      <Destination>Mumbai</Destination>

      </input>

       

      RESTView.java file

      package org.teiid.rest.services;

       

      import javax.xml.transform.Source;

      import javax.xml.ws.ServiceMode;

      import javax.xml.ws.WebServiceProvider;

       

      import java.io.BufferedReader;

      import java.io.IOException;

      import java.io.InputStream;

      import java.io.InputStreamReader;

      import java.io.Reader;

      import java.io.StringWriter;

      import java.io.Writer;

      import java.util.LinkedHashMap;

      import java.util.Map;

      import java.util.Properties;

      import java.util.logging.Level;

      import java.util.logging.Logger;

      import javax.ws.rs.Consumes;

      import javax.ws.rs.DELETE;

      import javax.ws.rs.GET;

      import javax.ws.rs.POST;

      import javax.ws.rs.PUT;

      import javax.ws.rs.Path;

      import javax.ws.rs.PathParam;

      import javax.ws.rs.Produces;

      import javax.ws.rs.WebApplicationException;

      import javax.ws.rs.core.MediaType;

      import javax.ws.rs.core.Response;

      import javax.xml.parsers.DocumentBuilder;

      import javax.xml.parsers.DocumentBuilderFactory;

      import org.codehaus.jackson.JsonFactory;

      import org.codehaus.jackson.JsonParseException;

      import org.codehaus.jackson.JsonParser;

      import org.codehaus.jackson.JsonToken;

      import org.codehaus.jackson.map.ObjectMapper;

      import org.json.JSONException;

      import org.json.JSONObject;

      import org.json.XML;

      import org.teiid.rest.RestPlugin;

      import org.w3c.dom.Document;

      import org.w3c.dom.Element;

      import org.w3c.dom.Node;

      import org.w3c.dom.NodeList;

       

      @Path( "RESTView" )

      public class RESTView{

       

          org.teiid.rest.services.TeiidRSProvider teiidProvider = new org.teiid.rest.services.TeiidRSProvider();

          Map<String, String> parameterMap = new LinkedHashMap<String, String>();

          private static Properties properties = new Properties();

          private static Logger logger = Logger.getLogger("org.teiid.rest"); //$NON-NLS-1$

       

          public RESTView() {

              loadProperties();

          }

       

          @GET

          @Path( "travels/{Passenger_ID}" )

          @Produces( MediaType.APPLICATION_XML )

          public String viewData( @PathParam( "Passenger_ID" ) String Passenger_ID ) {

              parameterMap.clear();

              parameterMap.put("Passenger_ID", Passenger_ID);

              return teiidProvider.execute("RESTView.viewData", parameterMap);

      }

          @GET

          @Path( "json/travels/{Passenger_ID}" )

          @Produces( MediaType.APPLICATION_JSON )

          public String viewDatajson( @PathParam( "Passenger_ID" ) String Passenger_ID ) {

              parameterMap.clear();

              parameterMap.put("Passenger_ID", Passenger_ID);

              String result = teiidProvider.execute("RESTView.viewData", parameterMap);

              String json = convertXMLToJSON(result);

              return json;

          }

          @POST

          @Path( "travels/{Passenger_ID}" )

          @Consumes( MediaType.APPLICATION_XML )

          @Produces( MediaType.APPLICATION_XML )

          public String InsertData( @PathParam( "Passenger_ID" ) String Passenger_ID,  InputStream is ) {

              parameterMap.clear();

              parameterMap.put("Passenger_ID", Passenger_ID);

              parameterMap = getInputs(is);

              return teiidProvider.execute("RESTView.InsertData", parameterMap);

      }

          @POST

          @Path( "json/travels/{Passenger_ID}" )

          @Consumes( MediaType.APPLICATION_JSON )

          @Produces( MediaType.APPLICATION_JSON )

          public String InsertDatajson( @PathParam( "Passenger_ID" ) String Passenger_ID,  InputStream is ) {

              parameterMap.clear();

              parameterMap.put("Passenger_ID", Passenger_ID);

              parameterMap = getJSONInputs(is);

              String result = teiidProvider.execute("RESTView.InsertData", parameterMap);

              String json = convertXMLToJSON(result);

              return json;

          }

          @PUT

          @Path( "travels/{Passenger_ID}" )

          @Consumes( MediaType.APPLICATION_XML )

          @Produces( MediaType.APPLICATION_XML )

          public String UpdateData( @PathParam( "Passenger_ID" ) String Passenger_ID,  InputStream is ) {

              parameterMap.clear();

              parameterMap.put("Passenger_ID", Passenger_ID);

              parameterMap = getInputs(is);

              return teiidProvider.execute("RESTView.UpdateData", parameterMap);

      }

          @PUT

          @Path( "json/travels/{Passenger_ID}" )

          @Consumes( MediaType.APPLICATION_JSON )

          @Produces( MediaType.APPLICATION_JSON )

          public String UpdateDatajson( @PathParam( "Passenger_ID" ) String Passenger_ID,  InputStream is ) {

              parameterMap.clear();

              parameterMap.put("Passenger_ID", Passenger_ID);

              parameterMap = getJSONInputs(is);

              String result = teiidProvider.execute("RESTView.UpdateData", parameterMap);

              String json = convertXMLToJSON(result);

              return json;

          }

         

         

          protected Map<String, String> getInputs( InputStream is ) {

              try {

                  DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

                  Document doc = builder.parse(is);

                  Element root = doc.getDocumentElement(); // input

                  Map<String, String> parameters = new LinkedHashMap<String, String>();

                  if (!root.getNodeName().equals("input")) throw new WebApplicationException(Response.Status.BAD_REQUEST);

                  NodeList nodes = root.getChildNodes();

                  for (int i = 0; i < nodes.getLength(); i++) {

                      Element element = findElement(nodes.item(i));

                      if (element != null){

                          parameters.put(element.getNodeName(), element.getTextContent());

                      }

                  }

                  return parameters;

              } catch (Exception e) {

                  throw new WebApplicationException(e, Response.Status.BAD_REQUEST);

              }

          }

       

          private Element findElement( Node node ) {

              while (node != null && node.getNodeType() != Node.ELEMENT_NODE)

                  node = node.getNextSibling();

              return (Element)node;

          }

       

          protected Map<String, String> getJSONInputs( InputStream is ) {

              Map parameters;

              try {

                  String jsonString = convertStreamToString(is);

       

                  // Do this to validate the JSON string. If we don't blow up, then we are good.

                  new JSONObject(jsonString);

       

                  parameters = convertJSONStringToMap(jsonString);

              } catch (Exception e) {

                  throw new WebApplicationException(e, Response.Status.BAD_REQUEST);

              }

              return parameters;

          }

       

          public String convertStreamToString( InputStream is ) throws IOException {

              /*

               * To convert the InputStream to String we use the

               * Reader.read(char[] buffer) method.

               */

              if (is != null) {

                  Writer writer = new StringWriter();

       

                  char[] buffer = new char[1024];

                  try {

                      Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); //$NON-NLS-1$

                      int n;

                      while ((n = reader.read(buffer)) != -1) {

                          writer.write(buffer, 0, n);

                      }

                  } finally {

                      is.close();

                  }

                  return writer.toString();

              }

       

              return ""; //$NON-NLS-1$

          }

       

          public Map<String, String> convertJSONStringToMap( String jsonString ) {

       

              ObjectMapper mapper = new ObjectMapper();

              JsonFactory factory = mapper.getJsonFactory();

              JsonParser jp = null;

              try {

                  jp = factory.createJsonParser(jsonString);

              } catch (JsonParseException e) {

                  throw new WebApplicationException(e, Response.Status.BAD_REQUEST);

              } catch (IOException e) {

                  throw new WebApplicationException(e, Response.Status.BAD_REQUEST);

              }

       

              try {

                  jp.nextToken();

              } catch (JsonParseException e) {

                  throw new WebApplicationException(e, Response.Status.BAD_REQUEST);

              } catch (IOException e) {

                  throw new WebApplicationException(e, Response.Status.BAD_REQUEST);

              }

              try {

                  while (jp.nextToken() != JsonToken.END_OBJECT) {

                      String fieldname = jp.getCurrentName();

                      jp.nextToken(); // move to value, or START_OBJECT/START_ARRAY

                      String value = jp.getText();

                      parameterMap.put(fieldname, value);

                  }

              } catch (JsonParseException e) {

                  throw new WebApplicationException(e, Response.Status.BAD_REQUEST);

              } catch (IOException e) {

                  throw new WebApplicationException(e, Response.Status.BAD_REQUEST);

              }

       

              return parameterMap;

          }

       

          private void loadProperties() {

              try {

                  // Get the inputStream

                  InputStream inputStream = getClass().getClassLoader().getResourceAsStream("teiidrest.properties"); //$NON-NLS-1$

       

                  properties = new Properties();

       

                  // load the inputStream using the Properties

                  properties.load(inputStream);

       

              } catch (IOException e) {

                  String msg = RestPlugin.Util.getString("TeiidRSProvider.1"); //$NON-NLS-1$

                  logger.logrb(Level.SEVERE, "TeiidWSProvider", "loadProperties", RestPlugin.PLUGIN_ID, msg, new Throwable(e)); //$NON-NLS-1$ //$NON-NLS-2$

                  throw new RuntimeException(e);

              }

          }

       

          /**

           * Converts XML to JSON

           */

          public static String convertXMLToJSON( String XMLfile ) {

              // obj that will convert xml string to json obj

              JSONObject jsonObj;

              String jsonString = ""; //$NON-NLS-1$

       

              // convert xml to json obj

              try {

                  jsonObj = XML.toJSONObject(XMLfile);

                  jsonString = jsonObj.toString(1);

              } catch (JSONException je) {

                  String msg = RestPlugin.Util.getString("TeiidRSProvider.1"); //$NON-NLS-1$

                  logger.logrb(Level.SEVERE, "TeiidRSProvider", "convertXMLToJSON", RestPlugin.PLUGIN_ID, msg, new Throwable(je)); //$NON-NLS-1$ //$NON-NLS-2$

                  throw new RuntimeException(je);

              }

       

              return jsonString;

          }

      }