コード例 #1
0
        @Override
        public void handle(ServerHttpRequest request, ServerHttpResponse response)
            throws IOException {

          if (!HttpMethod.GET.equals(request.getMethod())) {
            sendMethodNotAllowed(response, Arrays.asList(HttpMethod.GET));
            return;
          }

          String content = String.format(IFRAME_CONTENT, getSockJsClientLibraryUrl());
          byte[] contentBytes = content.getBytes(Charset.forName("UTF-8"));
          StringBuilder builder = new StringBuilder("\"0");
          DigestUtils.appendMd5DigestAsHex(contentBytes, builder);
          builder.append('"');
          String etagValue = builder.toString();

          List<String> ifNoneMatch = request.getHeaders().getIfNoneMatch();
          if (!CollectionUtils.isEmpty(ifNoneMatch) && ifNoneMatch.get(0).equals(etagValue)) {
            response.setStatusCode(HttpStatus.NOT_MODIFIED);
            return;
          }

          response
              .getHeaders()
              .setContentType(new MediaType("text", "html", Charset.forName("UTF-8")));
          response.getHeaders().setContentLength(contentBytes.length);

          addCacheHeaders(response);
          response.getHeaders().setETag(etagValue);
          response.getBody().write(contentBytes);
        }
コード例 #2
0
        @Override
        public void handle(ServerHttpRequest request, ServerHttpResponse response)
            throws IOException {

          if (HttpMethod.GET.equals(request.getMethod())) {

            response
                .getHeaders()
                .setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));

            addCorsHeaders(request, response);
            addNoCacheHeaders(response);

            String content =
                String.format(
                    INFO_CONTENT,
                    random.nextInt(),
                    isJsessionIdCookieRequired(),
                    isWebSocketEnabled());
            response.getBody().write(content.getBytes());
          } else if (HttpMethod.OPTIONS.equals(request.getMethod())) {

            response.setStatusCode(HttpStatus.NO_CONTENT);

            addCorsHeaders(request, response, HttpMethod.OPTIONS, HttpMethod.GET);
            addCacheHeaders(response);
          } else {
            sendMethodNotAllowed(response, Arrays.asList(HttpMethod.OPTIONS, HttpMethod.GET));
          }
        }
コード例 #3
0
  @Test
  public void testHttpMethod() {
    MockTestDesigner builder =
        new MockTestDesigner(applicationContext) {
          @Override
          public void configure() {
            send(httpClient)
                .http()
                .method(HttpMethod.GET)
                .payload("<TestRequest><Message>Hello World!</Message></TestRequest>");
          }
        };

    builder.configure();

    TestCase test = builder.getTestCase();
    Assert.assertEquals(test.getActionCount(), 1);
    Assert.assertEquals(test.getActions().get(0).getClass(), SendMessageAction.class);

    SendMessageAction action = ((SendMessageAction) test.getActions().get(0));
    Assert.assertEquals(action.getName(), "send");

    Assert.assertEquals(action.getEndpoint(), httpClient);
    Assert.assertEquals(action.getMessageBuilder().getClass(), PayloadTemplateMessageBuilder.class);

    PayloadTemplateMessageBuilder messageBuilder =
        (PayloadTemplateMessageBuilder) action.getMessageBuilder();
    Assert.assertEquals(
        messageBuilder.getPayloadData(),
        "<TestRequest><Message>Hello World!</Message></TestRequest>");
    Assert.assertEquals(messageBuilder.getMessageHeaders().size(), 1L);
    Assert.assertEquals(
        messageBuilder.getMessageHeaders().get(HttpMessageHeaders.HTTP_REQUEST_METHOD),
        HttpMethod.GET.name());
  }
コード例 #4
0
  @Override
  public final boolean doHandshake(
      ServerHttpRequest request,
      ServerHttpResponse response,
      WebSocketHandler wsHandler,
      Map<String, Object> attributes)
      throws HandshakeFailureException {

    if (logger.isDebugEnabled()) {
      logger.debug(
          "Initiating handshake for " + request.getURI() + ", headers=" + request.getHeaders());
    }

    try {
      if (!HttpMethod.GET.equals(request.getMethod())) {
        response.setStatusCode(HttpStatus.METHOD_NOT_ALLOWED);
        response.getHeaders().setAllow(Collections.singleton(HttpMethod.GET));
        logger.debug("Only HTTP GET is allowed, current method is " + request.getMethod());
        return false;
      }
      if (!"WebSocket".equalsIgnoreCase(request.getHeaders().getUpgrade())) {
        handleInvalidUpgradeHeader(request, response);
        return false;
      }
      if (!request.getHeaders().getConnection().contains("Upgrade")
          && !request.getHeaders().getConnection().contains("upgrade")) {
        handleInvalidConnectHeader(request, response);
        return false;
      }
      if (!isWebSocketVersionSupported(request)) {
        handleWebSocketVersionNotSupported(request, response);
        return false;
      }
      if (!isValidOrigin(request)) {
        response.setStatusCode(HttpStatus.FORBIDDEN);
        return false;
      }
      String wsKey = request.getHeaders().getSecWebSocketKey();
      if (wsKey == null) {
        logger.debug("Missing \"Sec-WebSocket-Key\" header");
        response.setStatusCode(HttpStatus.BAD_REQUEST);
        return false;
      }
    } catch (IOException ex) {
      throw new HandshakeFailureException(
          "Response update failed during upgrade to WebSocket, uri=" + request.getURI(), ex);
    }

    String subProtocol = selectProtocol(request.getHeaders().getSecWebSocketProtocol());

    if (logger.isDebugEnabled()) {
      logger.debug("Upgrading request, sub-protocol=" + subProtocol);
    }

    this.requestUpgradeStrategy.upgrade(request, response, subProtocol, wsHandler, attributes);

    return true;
  }