예제 #1
0
    private void sendHttpResponseFromOnem2mResponse(
        HttpServletResponse httpResponse, ResponsePrimitive onem2mResponse) throws IOException {

      // the content is already in the required format ...
      String content = onem2mResponse.getPrimitive(ResponsePrimitive.CONTENT);
      String rscString = onem2mResponse.getPrimitive(ResponsePrimitive.RESPONSE_STATUS_CODE);
      String rqi = onem2mResponse.getPrimitive(ResponsePrimitive.REQUEST_IDENTIFIER);
      if (rqi != null) {
        httpResponse.setHeader(Onem2m.HttpHeaders.X_M2M_RI, rqi);
      }

      int httpRSC = mapCoreResponseToHttpResponse(httpResponse, rscString);
      if (content != null) {
        httpResponse.setStatus(httpRSC);
        httpResponse.getWriter().println(content);
      } else {
        httpResponse.setStatus(httpRSC);
      }
      if (rscString.charAt(0) == '2') {
        Onem2mStats.getInstance().inc(Onem2mStats.HTTP_REQUESTS_OK);
      } else {
        Onem2mStats.getInstance().inc(Onem2mStats.HTTP_REQUESTS_ERROR);
      }

      String ct = onem2mResponse.getPrimitive(ResponsePrimitive.HTTP_CONTENT_TYPE);
      if (ct != null) {
        httpResponse.setContentType(ct);
      }
      String cl = onem2mResponse.getPrimitive(ResponsePrimitive.HTTP_CONTENT_LOCATION);
      if (cl != null) {
        httpResponse.setHeader("Content-Location", cl);
      }
    }
예제 #2
0
    /**
     * The handler for the HTTP requesst
     *
     * @param target target
     * @param baseRequest request
     * @param httpRequest request
     * @param httpResponse response
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void handle(
        String target,
        Request baseRequest,
        HttpServletRequest httpRequest,
        HttpServletResponse httpResponse)
        throws IOException, ServletException {

      Onem2mRequestPrimitiveClientBuilder clientBuilder = new Onem2mRequestPrimitiveClientBuilder();
      String headerValue;

      clientBuilder.setProtocol(Onem2m.Protocol.HTTP);

      Onem2mStats.getInstance().endpointInc(baseRequest.getRemoteAddr());
      Onem2mStats.getInstance().inc(Onem2mStats.HTTP_REQUESTS);

      String contentType = httpRequest.getContentType();
      if (contentType == null) contentType = "json";
      contentType = contentType.toLowerCase();
      if (contentType.contains("json")) {
        clientBuilder.setContentFormat(Onem2m.ContentFormat.JSON);
      } else if (contentType.contains("xml")) {
        clientBuilder.setContentFormat(Onem2m.ContentFormat.XML);
      } else {
        httpResponse.setStatus(HttpServletResponse.SC_NOT_ACCEPTABLE);
        httpResponse.getWriter().println("Unsupported media type: " + contentType);
        httpResponse.setContentType("text/json;charset=utf-8");
        baseRequest.setHandled(true);
        Onem2mStats.getInstance().inc(Onem2mStats.HTTP_REQUESTS_ERROR);
        return;
      }

      clientBuilder.setTo(httpRequest.getRequestURI());

      // pull fields out of the headers
      headerValue = httpRequest.getHeader(Onem2m.HttpHeaders.X_M2M_ORIGIN);
      if (headerValue != null) {
        clientBuilder.setFrom(headerValue);
      }
      headerValue = httpRequest.getHeader(Onem2m.HttpHeaders.X_M2M_RI);
      if (headerValue != null) {
        clientBuilder.setRequestIdentifier(headerValue);
      }
      headerValue = httpRequest.getHeader(Onem2m.HttpHeaders.X_M2M_NM);
      if (headerValue != null) {
        clientBuilder.setName(headerValue);
      }
      headerValue = httpRequest.getHeader(Onem2m.HttpHeaders.X_M2M_GID);
      if (headerValue != null) {
        clientBuilder.setGroupRequestIdentifier(headerValue);
      }
      headerValue = httpRequest.getHeader(Onem2m.HttpHeaders.X_M2M_RTU);
      if (headerValue != null) {
        clientBuilder.setResponseType(headerValue);
      }
      headerValue = httpRequest.getHeader(Onem2m.HttpHeaders.X_M2M_OT);
      if (headerValue != null) {
        clientBuilder.setOriginatingTimestamp(headerValue);
      }

      // the contentType string can have ty=val attached to it so we should handle this case
      Boolean resourceTypePresent = false;
      String contentTypeResourceString = parseContentTypeForResourceType(contentType);
      if (contentTypeResourceString != null) {
        resourceTypePresent =
            clientBuilder.parseQueryStringIntoPrimitives(contentTypeResourceString);
      }
      String method = httpRequest.getMethod().toLowerCase();
      // look in query string if didnt find it in contentType header
      if (!resourceTypePresent) {
        resourceTypePresent =
            clientBuilder.parseQueryStringIntoPrimitives(httpRequest.getQueryString());
      } else {
        clientBuilder.parseQueryStringIntoPrimitives(httpRequest.getQueryString());
      }
      if (resourceTypePresent && !method.contentEquals("post")) {
        httpResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        httpResponse.getWriter().println("Specifying resource type not permitted.");
        Onem2mStats.getInstance().inc(Onem2mStats.HTTP_REQUESTS_ERROR);
        return;
      }

      // take the entire payload text and put it in the CONTENT field; it is the representation of
      // the resource
      String cn = IOUtils.toString(baseRequest.getInputStream()).trim();
      if (cn != null && !cn.contentEquals("")) {
        clientBuilder.setPrimitiveContent(cn);
      }

      switch (method) {
        case "get":
          clientBuilder.setOperationRetrieve();
          Onem2mStats.getInstance().inc(Onem2mStats.HTTP_REQUESTS_RETRIEVE);
          break;
        case "post":
          if (resourceTypePresent) {
            clientBuilder.setOperationCreate();
            Onem2mStats.getInstance().inc(Onem2mStats.HTTP_REQUESTS_CREATE);
          } else {
            clientBuilder.setOperationNotify();
            Onem2mStats.getInstance().inc(Onem2mStats.HTTP_REQUESTS_NOTIFY);
          }
          break;
        case "put":
          clientBuilder.setOperationUpdate();
          Onem2mStats.getInstance().inc(Onem2mStats.HTTP_REQUESTS_UPDATE);
          break;
        case "delete":
          clientBuilder.setOperationDelete();
          Onem2mStats.getInstance().inc(Onem2mStats.HTTP_REQUESTS_DELETE);
          break;
        default:
          httpResponse.setStatus(HttpServletResponse.SC_NOT_IMPLEMENTED);
          httpResponse.getWriter().println("Unsupported method type: " + method);
          httpResponse.setContentType("text/json;charset=utf-8");
          baseRequest.setHandled(true);
          Onem2mStats.getInstance().inc(Onem2mStats.HTTP_REQUESTS_ERROR);
          return;
      }

      // invoke the service request
      Onem2mRequestPrimitiveClient onem2mRequest = clientBuilder.build();
      ResponsePrimitive onem2mResponse = Onem2m.serviceOnenm2mRequest(onem2mRequest, onem2mService);

      // now place the fields from the onem2m result response back in the http fields, and send
      sendHttpResponseFromOnem2mResponse(httpResponse, onem2mResponse);

      httpResponse.addHeader("Access-Control-Allow-Origin", "*");
      httpResponse.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS");

      baseRequest.setHandled(true);
    }