Пример #1
0
  /**
   * Tests method setRequestContent of HttpClient.
   *
   * @throws IOException I/O Exception
   */
  @Test
  public void writeMultipartMessage() throws IOException {
    final HttpRequest req = new HttpRequest();
    req.isMultipart = true;
    req.payloadAttrs.put("media-type", "multipart/alternative");
    req.payloadAttrs.put("boundary", "boundary42");
    final Part p1 = new Part();
    p1.headers.put("Content-Type", "text/plain; charset=us-ascii");
    p1.bodyAttrs.put("media-type", "text/plain");
    final String plain = "...plain text....";
    p1.bodyContent.add(Str.get(plain + '\n'));

    final Part p2 = new Part();
    p2.headers.put("Content-Type", "text/richtext");
    p2.bodyAttrs.put("media-type", "text/richtext");
    final String rich = ".... richtext version...";
    p2.bodyContent.add(Str.get(rich));

    final Part p3 = new Part();
    p3.headers.put("Content-Type", "text/x-whatever");
    p3.bodyAttrs.put("media-type", "text/x-whatever");
    final String fancy = ".... fanciest formatted version...";
    p3.bodyContent.add(Str.get(fancy));

    req.parts.add(p1);
    req.parts.add(p2);
    req.parts.add(p3);

    final FakeHttpConnection fakeConn = new FakeHttpConnection(new URL("http://www.test.com"));
    HttpClient.setRequestContent(fakeConn.getOutputStream(), req);
    final String expResult =
        "--boundary42"
            + CRLF
            + "Content-Type: text/plain; charset=us-ascii"
            + CRLF
            + CRLF
            + plain
            + Prop.NL
            + CRLF
            + "--boundary42"
            + CRLF
            + "Content-Type: text/richtext"
            + CRLF
            + CRLF
            + rich
            + CRLF
            + "--boundary42"
            + CRLF
            + "Content-Type: text/x-whatever"
            + CRLF
            + CRLF
            + fancy
            + CRLF
            + "--boundary42--"
            + CRLF;

    // Compare results
    assertEquals(expResult, fakeConn.getOutputStream().toString());
  }
  private void prepareExecution(
      String uri, String responseBody, String identifier, Snippet... snippets) throws IOException {
    when(execution.execute(any(HttpRequest.class), any(byte[].class))).thenReturn(response);
    when(request.getURI()).thenReturn(URI.create(uri));
    when(request.getMethod()).thenReturn(HttpMethod.GET);
    when(request.getHeaders()).thenReturn(new HttpHeaders());

    when(response.getHeaders()).thenReturn(new HttpHeaders());
    when(response.getStatusCode()).thenReturn(HttpStatus.OK);
    when(response.getBody()).thenReturn(new ByteArrayInputStream(responseBody.getBytes()));

    this.interceptor = CitrusRestDocsSupport.restDocsInterceptor(identifier, snippets);
  }
Пример #3
0
  /**
   * Builds request content string from request and body.
   *
   * @param request
   * @param body
   * @return
   */
  private String getRequestContent(HttpRequest request, String body) {
    StringBuilder builder = new StringBuilder();

    builder.append(request.getMethod());
    builder.append(" ");
    builder.append(request.getURI());
    builder.append(NEWLINE);

    appendHeaders(request.getHeaders(), builder);

    builder.append(NEWLINE);
    builder.append(body);

    return builder.toString();
  }
 @Override
 protected void setCommonHeaders(HttpRequest req) {
   super.setCommonHeaders(req);
   if (filter != null) {
     req.setHeader(X_MOBC3_FILTER, filter);
     filter = null;
   }
 }
Пример #5
0
 /**
  * Creates an new request for the app server from an HTTP request.
  *
  * @param request HTTP request to create
  * @return the request
  */
 public AppRequest createRequest(HttpRequest request) {
   return createRequest(request, request.getHost(), request.getURI());
 }
  @Override
  public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    HttpRequest request = (HttpRequest) e.getMessage();
    if (request.getMethod() != GET) {
      sendError(ctx, METHOD_NOT_ALLOWED);
      return;
    }

    String base =
        ContainerLocalizer.USERCACHE
            + "/"
            + userName
            + "/"
            + ContainerLocalizer.APPCACHE
            + "/"
            + appId
            + "/output"
            + "/";

    final Map<String, List<String>> params =
        new QueryStringDecoder(request.getUri()).getParameters();

    List<FileChunk> chunks = Lists.newArrayList();
    List<String> taskIds = splitMaps(params.get("ta"));
    int sid = Integer.valueOf(params.get("sid").get(0));
    int partitionId = Integer.valueOf(params.get("p").get(0));
    for (String ta : taskIds) {

      File file = new File(base + "/" + sid + "/" + ta + "/output/" + partitionId);
      FileChunk chunk = new FileChunk(file, 0, file.length());
      chunks.add(chunk);
    }

    FileChunk[] file = chunks.toArray(new FileChunk[chunks.size()]);
    //    try {
    //      file = retriever.handle(ctx, request);
    //    } catch (FileNotFoundException fnf) {
    //      LOG.error(fnf);
    //      sendError(ctx, NOT_FOUND);
    //      return;
    //    } catch (IllegalArgumentException iae) {
    //      LOG.error(iae);
    //      sendError(ctx, BAD_REQUEST);
    //      return;
    //    } catch (FileAccessForbiddenException fafe) {
    //      LOG.error(fafe);
    //      sendError(ctx, FORBIDDEN);
    //      return;
    //    } catch (IOException ioe) {
    //      LOG.error(ioe);
    //      sendError(ctx, INTERNAL_SERVER_ERROR);
    //      return;
    //    }

    // Write the content.
    Channel ch = e.getChannel();
    if (file == null) {
      HttpResponse response = new DefaultHttpResponse(HTTP_1_1, NO_CONTENT);
      ch.write(response);
      if (!isKeepAlive(request)) {
        ch.close();
      }
    } else {
      HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
      long totalSize = 0;
      for (FileChunk chunk : file) {
        totalSize += chunk.length();
      }
      setContentLength(response, totalSize);

      // Write the initial line and the header.
      ch.write(response);

      ChannelFuture writeFuture = null;

      for (FileChunk chunk : file) {
        writeFuture = sendFile(ctx, ch, chunk);
        if (writeFuture == null) {
          sendError(ctx, NOT_FOUND);
          return;
        }
      }

      // Decide whether to close the connection or not.
      if (!isKeepAlive(request)) {
        // Close the connection when the whole content is written out.
        writeFuture.addListener(ChannelFutureListener.CLOSE);
      }
    }
  }