Ejemplo n.º 1
0
  // Try to add twice
  @Test
  public void add_delete_dataset_2() {
    checkNotThere(dsTest);

    File f = new File("testing/config-ds-1.ttl");
    {
      org.apache.http.entity.ContentType ct =
          org.apache.http.entity.ContentType.parse(
              WebContent.contentTypeTurtle + "; charset=" + WebContent.charsetUTF8);
      HttpEntity e = new FileEntity(f, ct);
      execHttpPost(ServerTest.urlRoot + "$/" + opDatasets, e);
    }
    // Check exists.
    checkExists(dsTest);
    try {
      org.apache.http.entity.ContentType ct =
          org.apache.http.entity.ContentType.parse(
              WebContent.contentTypeTurtle + "; charset=" + WebContent.charsetUTF8);
      HttpEntity e = new FileEntity(f, ct);
      execHttpPost(ServerTest.urlRoot + "$/" + opDatasets, e);
    } catch (HttpException ex) {
      assertEquals(HttpSC.CONFLICT_409, ex.getResponseCode());
    }
    // Check exists.
    checkExists(dsTest);
    deleteDataset(dsTest);
  }
Ejemplo n.º 2
0
  private String getContentCharset(byte[] content, Metadata metadata) {
    String charset = null;

    // check if the server specified a charset
    String specifiedContentType = metadata.getFirstValue(HttpHeaders.CONTENT_TYPE);
    try {
      if (specifiedContentType != null) {
        ContentType parsedContentType = ContentType.parse(specifiedContentType);
        charset = parsedContentType.getCharset().name();
      }
    } catch (Exception e) {
      charset = null;
    }

    // filter HTML tags
    CharsetDetector detector = new CharsetDetector();
    detector.enableInputFilter(true);
    // give it a hint
    detector.setDeclaredEncoding(charset);
    detector.setText(content);
    try {
      CharsetMatch charsetMatch = detector.detect();
      if (charsetMatch != null) {
        charset = charsetMatch.getName();
      }
    } catch (Exception e) {
      // ignore and leave the charset as-is
    }
    return charset;
  }
Ejemplo n.º 3
0
 @Override
 protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
   Integer streamId =
       msg.headers().getInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text());
   if (streamId == null) {
     logger.error("HttpResponseHandler unexpected message received: " + msg);
     return;
   }
   onResponseReceived(ctx, streamIdUrlMap.get(streamId));
   Map.Entry<ChannelFuture, ChannelPromise> entry = streamIdPromiseMap.get(streamId);
   if (entry == null) {
     logger.error("Message received for unknown stream id " + streamId);
   } else {
     // Do stuff with the message (for now just print it)
     ByteBuf content = msg.content();
     ContentType contentType = ContentType.parse(msg.headers().get(HttpHeaderNames.CONTENT_TYPE));
     if (content.isReadable()) {
       int contentLength = content.readableBytes();
       byte[] arr = new byte[contentLength];
       content.readBytes(arr);
       handleContent(arr, ctx, contentType);
     }
     entry.getValue().setSuccess();
   }
 }
Ejemplo n.º 4
0
 private static void addTestDataset() {
   File f = new File("testing/config-ds-1.ttl");
   org.apache.http.entity.ContentType ct =
       org.apache.http.entity.ContentType.parse(
           WebContent.contentTypeTurtle + "; charset=" + WebContent.charsetUTF8);
   HttpEntity e = new FileEntity(f, ct);
   execHttpPost(ServerTest.urlRoot + "$/" + opDatasets, e);
 }
  private Document getHttpMessageContent(HttpMessage message) {
    HttpEntity entity = null;

    if (message instanceof HttpEntityEnclosingRequest) {
      entity = ((HttpEntityEnclosingRequest) message).getEntity();
    } else if (message instanceof HttpResponse) {
      entity = ((HttpResponse) message).getEntity();
    }

    Document doc = null;

    // redirecting reply to and fault to nodes if present
    if (entity != null) {
      try (InputStream is = entity.getContent()) {
        ContentType contentType = null;
        for (Header header : message.getHeaders("Content-Type")) {
          try {
            contentType = ContentType.parse(header.getValue());
          } catch (ParseException | UnsupportedCharsetException ex) {
          }
        }

        if (contentType != null) {
          Charset charset = contentType.getCharset();
          if (charset == null) {
            charset = Charset.forName("UTF-8");
          }
          String mimeType = contentType.getMimeType();

          if (mimeType.contains("xml")) {
            String content = IOUtils.toString(is, charset.name());

            Document xmlDocument = XMLUtils.parseXML(content);
            if (xmlDocument != null) {
              XPath xpath = XPathFactory.newInstance().newXPath();
              xpath.setNamespaceContext(new FimsNamespaceContext());

              NodeList nodes =
                  (NodeList)
                      xpath.evaluate(
                          "/S:Envelope/S:Body/S:Fault/detail/*",
                          xmlDocument.getDocumentElement(),
                          XPathConstants.NODESET);

              if (nodes.getLength() == 0) {
                nodes =
                    (NodeList)
                        xpath.evaluate(
                            "/S:Envelope/S:Body/*",
                            xmlDocument.getDocumentElement(),
                            XPathConstants.NODESET);
              }

              doc = XMLUtils.newEmptyDocument();

              if (nodes.getLength() > 0) {
                Node node = nodes.item(0);
                Node newNode = doc.importNode(node, true);
                doc.appendChild(newNode);
              }
            }
          }
        }
      } catch (IOException | UnsupportedOperationException | XPathExpressionException ex) {
        Logger.getLogger(ValidationModuleImpl.class.getName()).log(Level.SEVERE, null, ex);
      }
    }

    return doc;
  }
Ejemplo n.º 6
0
  private Olingo2BatchResponse parseResponse(
      Edm edm,
      Map<String, String> contentIdLocationMap,
      Olingo2BatchRequest request,
      BatchSingleResponse response)
      throws EntityProviderException, ODataApplicationException {

    // validate HTTP status
    final int statusCode = Integer.parseInt(response.getStatusCode());
    final String statusInfo = response.getStatusInfo();

    final BasicHttpResponse httpResponse =
        new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, statusCode, statusInfo));
    final Map<String, String> headers = response.getHeaders();
    for (Map.Entry<String, String> entry : headers.entrySet()) {
      httpResponse.setHeader(entry.getKey(), entry.getValue());
    }

    ByteArrayInputStream content = null;
    try {
      if (response.getBody() != null) {
        final ContentType partContentType =
            receiveWithCharsetParameter(
                ContentType.parse(headers.get(HttpHeaders.CONTENT_TYPE)), Consts.UTF_8);
        final String charset = partContentType.getCharset().toString();

        final String body = response.getBody();
        content = body != null ? new ByteArrayInputStream(body.getBytes(charset)) : null;

        httpResponse.setEntity(new StringEntity(body, charset));
      }

      AbstractFutureCallback.checkStatus(httpResponse);
    } catch (ODataApplicationException e) {
      return new Olingo2BatchResponse(
          statusCode, statusInfo, response.getContentId(), response.getHeaders(), e);
    } catch (UnsupportedEncodingException e) {
      return new Olingo2BatchResponse(
          statusCode, statusInfo, response.getContentId(), response.getHeaders(), e);
    }

    // resolve resource path and query params and parse batch part uri
    final String resourcePath = request.getResourcePath();
    final String resolvedResourcePath;
    if (resourcePath.startsWith("$")
        && !(METADATA.equals(resourcePath) || BATCH.equals(resourcePath))) {
      resolvedResourcePath = findLocation(resourcePath, contentIdLocationMap);
    } else {
      final String resourceLocation = response.getHeader(HttpHeaders.LOCATION);
      resolvedResourcePath =
          resourceLocation != null ? resourceLocation.substring(serviceUri.length()) : resourcePath;
    }
    final Map<String, String> resolvedQueryParams =
        request instanceof Olingo2BatchQueryRequest
            ? ((Olingo2BatchQueryRequest) request).getQueryParams()
            : null;
    final UriInfoWithType uriInfo = parseUri(edm, resolvedResourcePath, resolvedQueryParams);

    // resolve response content
    final Object resolvedContent = content != null ? readContent(uriInfo, content) : null;

    return new Olingo2BatchResponse(
        statusCode, statusInfo, response.getContentId(), response.getHeaders(), resolvedContent);
  }
Ejemplo n.º 7
0
  private <T> void writeContent(
      final Edm edm,
      HttpEntityEnclosingRequestBase httpEntityRequest,
      final UriInfoWithType uriInfo,
      final Object content,
      final Olingo2ResponseHandler<T> responseHandler) {

    try {
      // process resource by UriType
      final ODataResponse response = writeContent(edm, uriInfo, content);

      // copy all response headers
      for (String header : response.getHeaderNames()) {
        httpEntityRequest.setHeader(header, response.getHeader(header));
      }

      // get (http) entity which is for default Olingo2 implementation an InputStream
      if (response.getEntity() instanceof InputStream) {
        httpEntityRequest.setEntity(new InputStreamEntity((InputStream) response.getEntity()));
        /*
                        // avoid sending it without a header field set
                        if (!httpEntityRequest.containsHeader(HttpHeaders.CONTENT_TYPE)) {
                            httpEntityRequest.addHeader(HttpHeaders.CONTENT_TYPE, getContentType());
                        }
        */
      }

      // execute HTTP request
      final Header requestContentTypeHeader =
          httpEntityRequest.getFirstHeader(HttpHeaders.CONTENT_TYPE);
      final ContentType requestContentType =
          requestContentTypeHeader != null
              ? ContentType.parse(requestContentTypeHeader.getValue())
              : contentType;
      execute(
          httpEntityRequest,
          requestContentType,
          new AbstractFutureCallback<T>(responseHandler) {
            @SuppressWarnings("unchecked")
            @Override
            public void onCompleted(HttpResponse result)
                throws IOException, EntityProviderException, BatchException,
                    ODataApplicationException {

              // if a entity is created (via POST request) the response body contains the new
              // created entity
              HttpStatusCodes statusCode =
                  HttpStatusCodes.fromStatusCode(result.getStatusLine().getStatusCode());

              // look for no content, or no response body!!!
              final boolean noEntity =
                  result.getEntity() == null || result.getEntity().getContentLength() == 0;
              if (statusCode == HttpStatusCodes.NO_CONTENT || noEntity) {
                responseHandler.onResponse(
                    (T) HttpStatusCodes.fromStatusCode(result.getStatusLine().getStatusCode()));
              } else {

                switch (uriInfo.getUriType()) {
                  case URI9:
                    // $batch
                    final List<BatchSingleResponse> singleResponses =
                        EntityProvider.parseBatchResponse(
                            result.getEntity().getContent(),
                            result.getFirstHeader(HttpHeaders.CONTENT_TYPE).getValue());

                    // parse batch response bodies
                    final List<Olingo2BatchResponse> responses =
                        new ArrayList<Olingo2BatchResponse>();
                    Map<String, String> contentIdLocationMap = new HashMap<String, String>();

                    final List<Olingo2BatchRequest> batchRequests =
                        (List<Olingo2BatchRequest>) content;
                    final Iterator<Olingo2BatchRequest> iterator = batchRequests.iterator();

                    for (BatchSingleResponse response : singleResponses) {
                      final Olingo2BatchRequest request = iterator.next();

                      if (request instanceof Olingo2BatchChangeRequest
                          && ((Olingo2BatchChangeRequest) request).getContentId() != null) {

                        contentIdLocationMap.put(
                            "$" + ((Olingo2BatchChangeRequest) request).getContentId(),
                            response.getHeader(HttpHeaders.LOCATION));
                      }

                      try {
                        responses.add(parseResponse(edm, contentIdLocationMap, request, response));
                      } catch (Exception e) {
                        // report any parsing errors as error response
                        responses.add(
                            new Olingo2BatchResponse(
                                Integer.parseInt(response.getStatusCode()),
                                response.getStatusInfo(),
                                response.getContentId(),
                                response.getHeaders(),
                                new ODataApplicationException(
                                    "Error parsing response for " + request + ": " + e.getMessage(),
                                    Locale.ENGLISH,
                                    e)));
                      }
                    }
                    responseHandler.onResponse((T) responses);
                    break;

                  case URI4:
                  case URI5:
                    // simple property
                    // get the response content as Object for $value or Map<String, Object>
                    // otherwise
                    final List<EdmProperty> simplePropertyPath = uriInfo.getPropertyPath();
                    final EdmProperty simpleProperty =
                        simplePropertyPath.get(simplePropertyPath.size() - 1);
                    if (uriInfo.isValue()) {
                      responseHandler.onResponse(
                          (T)
                              EntityProvider.readPropertyValue(
                                  simpleProperty, result.getEntity().getContent()));
                    } else {
                      responseHandler.onResponse(
                          (T)
                              EntityProvider.readProperty(
                                  getContentType(),
                                  simpleProperty,
                                  result.getEntity().getContent(),
                                  EntityProviderReadProperties.init().build()));
                    }
                    break;

                  case URI3:
                    // complex property
                    // get the response content as Map<String, Object>
                    final List<EdmProperty> complexPropertyPath = uriInfo.getPropertyPath();
                    final EdmProperty complexProperty =
                        complexPropertyPath.get(complexPropertyPath.size() - 1);
                    responseHandler.onResponse(
                        (T)
                            EntityProvider.readProperty(
                                getContentType(),
                                complexProperty,
                                result.getEntity().getContent(),
                                EntityProviderReadProperties.init().build()));
                    break;

                  case URI7A:
                    // $links with 0..1 cardinality property
                    // get the response content as String
                    final EdmEntitySet targetLinkEntitySet = uriInfo.getTargetEntitySet();
                    responseHandler.onResponse(
                        (T)
                            EntityProvider.readLink(
                                getContentType(),
                                targetLinkEntitySet,
                                result.getEntity().getContent()));
                    break;

                  case URI7B:
                    // $links with * cardinality property
                    // get the response content as java.util.List<String>
                    final EdmEntitySet targetLinksEntitySet = uriInfo.getTargetEntitySet();
                    responseHandler.onResponse(
                        (T)
                            EntityProvider.readLinks(
                                getContentType(),
                                targetLinksEntitySet,
                                result.getEntity().getContent()));
                    break;

                  case URI1:
                  case URI2:
                  case URI6A:
                  case URI6B:
                    // Entity
                    // get the response content as an ODataEntry object
                    responseHandler.onResponse(
                        (T)
                            EntityProvider.readEntry(
                                response.getContentHeader(),
                                uriInfo.getTargetEntitySet(),
                                result.getEntity().getContent(),
                                EntityProviderReadProperties.init().build()));
                    break;

                  default:
                    throw new ODataApplicationException(
                        "Unsupported resource type " + uriInfo.getTargetType(), Locale.ENGLISH);
                }
              }
            }
          });
    } catch (ODataException e) {
      responseHandler.onException(e);
    } catch (URISyntaxException e) {
      responseHandler.onException(e);
    } catch (UnsupportedEncodingException e) {
      responseHandler.onException(e);
    } catch (IOException e) {
      responseHandler.onException(e);
    }
  }
Ejemplo n.º 8
0
 @Override
 public void setContentType(String contentType) {
   this.contentType = ContentType.parse(contentType);
 }
/** Receiver for a callback from application being tested. */
public class HttpServer<T extends HttpResponse> {
  private static final ContentType XML_UTF8_TYPE =
      ContentType.parse(XmlHttpResponse.CONTENT_TYPE_XML_TEXT_UTF8);

  private final T response;
  private final com.sun.net.httpserver.HttpServer server;
  private final AtomicInteger requestsReceived = new AtomicInteger(0);
  private final Object lock = new Object();

  /**
   * Creates new.
   *
   * @param aPath context the server will serve (must start with '/').
   * @param aResponse response to send when request is received, request will be added to it when
   *     this server receives one.
   */
  public HttpServer(String aPath, T aResponse) {
    response = aResponse;
    try {
      server = com.sun.net.httpserver.HttpServer.create();
      bind(server);
      server.createContext(aPath, getHandler(aResponse));
      server.start();
    } catch (IOException ex) {
      throw new RuntimeException(ex);
    }
  }

  /** @return address the server listens on. */
  public InetSocketAddress getAddress() {
    return server.getAddress();
  }

  /**
   * @return response, with request filled (with last received request) if getRequestsReceived() is
   *     larger than 0.
   */
  public T getResponse() {
    return response;
  }

  /** @return number of requests this server received. */
  public int getRequestsReceived() {
    return requestsReceived.get();
  }

  /**
   * @param maxWait ms to wait at most.
   * @return response with last request filled, if at least one was received.
   */
  public T waitForRequest(long maxWait) {
    long start = System.currentTimeMillis();
    try {
      while (requestsReceived.get() < 1 && (System.currentTimeMillis() - start) < maxWait) {
        try {
          Thread.sleep(50);
        } catch (InterruptedException ex) {
          throw new RuntimeException(ex);
        }
      }
    } finally {
      stopServer();
    }

    return getResponse();
  }

  /** Stops server from listening. */
  public void stopServer() {
    synchronized (lock) {
      server.stop(0);
    }
  }

  private void bind(com.sun.net.httpserver.HttpServer server) {
    try {
      InetAddress address = InetAddress.getLocalHost();
      int port = getFreePort();
      InetSocketAddress isa = new InetSocketAddress(address, port);
      server.bind(isa, 1);
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }
  }

  /**
   * Finds free port number.
   *
   * @return first available port number above 8000.
   */
  protected int getFreePort() {
    int port = 0;
    for (int possiblePort = 8000; port == 0; possiblePort++) {
      ServerSocket s = null;
      try {
        s = new ServerSocket(possiblePort);
        port = s.getLocalPort();
      } catch (IOException e) {
        // try next number
        continue;
      } finally {
        if (s != null) {
          try {
            s.close();
          } catch (IOException e) {
            // why would this happen?
            throw new IllegalStateException("Unable to close port: " + possiblePort, e);
          }
        }
      }
    }
    return port;
  }

  protected HttpHandler getHandler(final T aResponse) {
    HttpHandler result =
        new HttpHandler() {
          @Override
          public void handle(HttpExchange he) throws IOException {
            // ensure we never handle multiple requests at the same time
            synchronized (lock) {
              OutputStream os = null;
              try {
                String request;
                if ("POST".equals(he.getRequestMethod()) || "PUT".equals(he.getRequestMethod())) {
                  InputStream is = he.getRequestBody();
                  request =
                      FileUtil.streamToString(
                          is, String.format("http %s request", he.getRequestMethod()));
                } else {
                  request =
                      String.format("%s: %s", he.getRequestMethod(), he.getRequestURI().toString());
                }
                aResponse.setRequest(request);

                ContentType contentType = XML_UTF8_TYPE;
                byte[] responseBytes = aResponse.getResponse().getBytes(contentType.getCharset());
                he.sendResponseHeaders(aResponse.getStatusCode(), responseBytes.length);
                he.getResponseHeaders().add("Content-Type", contentType.toString());
                os = he.getResponseBody();
                os.write(responseBytes);
                os.flush();
              } finally {
                incrementRequestsReceived();
                if (os != null) {
                  os.close();
                }
              }
            }
          }
        };
    return result;
  }

  protected int incrementRequestsReceived() {
    return requestsReceived.incrementAndGet();
  }
}