Package org.vertx.java.core.http

Examples of org.vertx.java.core.http.HttpServerResponse


  private void formatDate() {
    dateString = DATE_FORMAT.format(new Date());
  }

  private void handlePlainText(HttpServerRequest req) {
    HttpServerResponse resp = req.response();
    setHeaders(resp, "text/plain", helloWorldContentLength);
    resp.end(helloWorldBuffer);
  }
View Full Code Here


    resp.end(helloWorldBuffer);
  }

  private void handleJson(HttpServerRequest req) {
    Buffer buff = new Buffer(Json.encode(Collections.singletonMap("message", "Hello, world!")));
    HttpServerResponse resp = req.response();
    setHeaders(resp, "application/json", String.valueOf(buff.length()));
    resp.end(buff);
  }
View Full Code Here

        handler);
  }

  private void sendResponse(HttpServerRequest req, String result) {
    Buffer buff = new Buffer(result);
    HttpServerResponse resp = req.response();
    setHeaders(resp, "application/json", String.valueOf(buff.length()));
    resp.end(buff);
  }
View Full Code Here

      return;
    }

    final String sessionId = cookieParts[0];
    String username = cookieParts[1];  
    final HttpServerResponse response = req.response;
    vertx.eventBus().send("participants.authorise", new JsonObject().putString(
        "sessionID", sessionId).putString("username", username).putBoolean("createClient", true),
        new Handler<Message<JsonObject>>() {
            @Override
          public void handle(Message<JsonObject> event) {
            if ("ok".equals(event.body.getString("status"))) {
              String activeClientId = event.body.getString("activeClient");
              String username = event.body.getString("username");
              if (activeClientId == null || username == null) {
                sendStatusCode(req, HttpStatus.SC_INTERNAL_SERVER_ERROR);
                return;
              }

              String responseText = getHostPage(sessionId, username, activeClientId);
              response.statusCode = HttpStatus.SC_OK;
              byte[] page = responseText.getBytes(Charset.forName("UTF-8"));
              response.putHeader("Content-Length", page.length);
              response.putHeader("Content-Type", "text/html");
              response.end(new Buffer(page));         
            } else {
              sendRedirect(req, "/static/login.html");
            }
          }
        });
View Full Code Here

     * {@inheritDoc}
     */
    @Override
    public OutputStream writeResponseStatusAndHeaders(long contentLength, ContainerResponse responseContext) throws ContainerException {
        jerseyResponse = responseContext;
        HttpServerResponse response = vertxRequest.response();

        // Write the status
        response.setStatusCode(responseContext.getStatus());
        response.setStatusMessage(responseContext.getStatusInfo().getReasonPhrase());

        // Set the content length header
        if (contentLength != -1) {
            response.putHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(contentLength));
        }

        for (final Map.Entry<String, List<String>> e : responseContext.getStringHeaders().entrySet()) {
            for (final String value : e.getValue()) {
                response.putHeader(e.getKey(), value);
            }
        }

        // Run any response processors
        if (!responseProcessors.isEmpty()) {
            for (VertxResponseProcessor processor : responseProcessors) {
                processor.process(response, responseContext);
            }
        }

        // Return output stream based on whether entity is chunked
        if (responseContext.isChunked()) {
            response.setChunked(true);
            return new VertxChunkedOutputStream(response);
        } else if (responseContext.hasEntity() && WriteStreamOutput.class.isAssignableFrom(responseContext.getEntityClass())) {
            WriteStreamOutput writeStreamOutput = (WriteStreamOutput) responseContext.getEntity();
            writeStreamOutput.init(response, new Handler<Void>() {
                @Override
View Full Code Here

     */
    @Override
    public void failure(Throwable error) {

        container.logger().error(error.getMessage(), error);
        HttpServerResponse response = vertxRequest.response();

        // Set error status and end
        Response.Status status = Response.Status.INTERNAL_SERVER_ERROR;
        response.setStatusCode(status.getStatusCode());
        response.setStatusMessage(status.getReasonPhrase());
        response.end();

    }
View Full Code Here

        HttpServer server = vertx.createHttpServer();

        server.requestHandler(new Handler<HttpServerRequest>() {
            public void handle(HttpServerRequest request) {
                String output = null;
                HttpServerResponse response = request.response;

                if(request.path.equals("/translate")) {
                    Map<String,String> params = request.params();

                    String input = params.get("word");
                    String language = params.get("language");


                    logger.info(format("Service received a request for '%s' in '%s'", input, language));

                    Properties words = dictionary.get(language);

                    if(words != null) {
                        output = words.getProperty(input);
                    }
                    if(output == null) output = UNKNOWN_WORD;

                    response.statusCode = 200;
                    response.end(output);
                } else {
                    response.statusCode = 404;
                    response.statusMessage = "Unrecognised";
                    response.end("Not found!");
                }
            }
        });

        server.listen(8090, "localhost");
View Full Code Here

                                        // the middleware was not mounted on this uri, skip to the next entry
                                        handle(null);
                                    }
                                }
                            } else {
                                HttpServerResponse response = request.response();
                                // reached the end and no handler was able to answer the request
                                response.setStatusCode(404);
                                response.setStatusMessage(HttpResponseStatus.valueOf(404).reasonPhrase());
                                if (errorHandler != null) {
                                    errorHandler.handle(request, null);
                                } else {
                                    response.end(HttpResponseStatus.valueOf(404).reasonPhrase());
                                }
                            }
                        } else {
                            request.put("error", error);
                            if (errorHandler != null) {
                                errorHandler.handle(request, null);
                            } else {
                                HttpServerResponse response = request.response();

                                int errorCode;
                                // if the error was set on the response use it
                                if (response.getStatusCode() >= 400) {
                                    errorCode = response.getStatusCode();
                                } else {
                                    // if it was set as the error object use it
                                    if (error instanceof Number) {
                                        errorCode = ((Number) error).intValue();
                                    } else if (error instanceof YokeException) {
                                        errorCode = ((YokeException) error).getErrorCode().intValue();
                                    } else if (error instanceof JsonObject) {
                                        errorCode = ((JsonObject) error).getInteger("errorCode", 500);
                                    } else if (error instanceof Map) {
                                        Integer tmp = (Integer) ((Map) error).get("errorCode");
                                        errorCode = tmp != null ? tmp : 500;
                                    } else {
                                        // default error code
                                        errorCode = 500;
                                    }
                                }

                                response.setStatusCode(errorCode);
                                response.setStatusMessage(HttpResponseStatus.valueOf(errorCode).reasonPhrase());
                                response.end(HttpResponseStatus.valueOf(errorCode).reasonPhrase());
                            }
                        }
                    }
                }.handle(null);
            }
View Full Code Here

  }

  public void handle(HttpServerRequest request) {
    System.out.println("Request intercepted: " + request.path);
   
    HttpServerResponse response = request.response;

    // For PUT requests we need an extra round-trip
    // See e.g. http://www.html5rocks.com/en/tutorials/cors/

    String acRequestMethod = request.headers().get(ACCESS_CONTROL_REQUEST_METHOD);
    String acRequestHeaders = request.headers().get(ACCESS_CONTROL_REQUEST_HEADERS);

    // Our REST API is accessible from anywhere
    response.putHeader(ACCESS_CONTROL_ALLOW_ORIGIN, "*");

    if (HttpMethod.OPTIONS.toString().equals(request.method)
        && StringUtils.hasText(acRequestMethod)) {
      // this is a preflight check
      // our API only needs this for PUT requests, anything we can PUT we
      // can also GET
      response.putHeader(ACCESS_CONTROL_ALLOW_METHODS, "GET,POST,PUT");
      response.putHeader(ACCESS_CONTROL_ALLOW_HEADERS, acRequestHeaders);
      response.putHeader(ACCESS_CONTROL_MAX_AGE, CACHE_SECONDS);

      response.end();
      return;
    } else {
      response.putHeader(ACCESS_CONTROL_EXPOSE_HEADERS, LOCATION);
    }

    // Not a preflight check, continue as normal
    this.next.handle(request);
  }
View Full Code Here

    if (!request.method().toUpperCase().equals("POST")) {
      super.handleRequest();
    } else {
      final Buffer buffer = new Buffer(0);
      final HttpServerResponse res = request.response();
      String origin = request.headers().get("ORIGIN");
      MultiMap resHeaders = res.headers();
      resHeaders.add("Content-Length", String.valueOf(1));
      resHeaders.add("Content-Type", "text/plain; charset=UTF-8");

      if (origin != null) {
        // https://developer.mozilla.org/En/HTTP_Access_Control
        resHeaders.add("Access-Control-Allow-Origin", origin);
        resHeaders.add("Access-Control-Allow-Credentials", "true");
      }

      request.dataHandler(new Handler<Buffer>() {
        public void handle(Buffer data) {
          buffer.appendBuffer(data);
          if (buffer.length() >= manager.getSettings().getDestryBufferSize()) {
            resetBuffer(buffer);
            request.response().end();
            request.response().close();
          }
        }
      });

      request.endHandler(new Handler<Void>() {
        public void handle(Void event) {
          res.setStatusCode(200);
                    res.end("1");
          res.close();

          onData(isPostEncoded() ? parseeData(buffer) : buffer);
        }
      });
View Full Code Here

TOP

Related Classes of org.vertx.java.core.http.HttpServerResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.