private void handleJson(HttpServerRequest req) {
   String result = Json.encode(Collections.singletonMap("message", "Hello, world!"));
   int contentLength = result.getBytes(StandardCharsets.UTF_8).length;
   req.response().putHeader("Content-Type", "application/json; charset=UTF-8");
   req.response().putHeader("Content-Length", String.valueOf(contentLength));
   req.response().end(result);
 }
 private void replyForError(JsonObject message) {
   if (message != null) {
     log.error(message.getString("message"));
     request.response().end("{}");
   } else {
     log.error("Empty Request");
     request.response().setStatusCode(400);
   }
   cleanup(requestId);
 }
  private boolean basicAuth(HttpServerRequest request, String encodedAuth) {
    byte[] authBytes = Base64.decodeBase64(encodedAuth);
    String decodedString = new String(authBytes);
    String[] splitAuth = StringUtils.split(StringUtils.trim(decodedString), ":"); // $NON-NLS-1$

    if (splitAuth.length != 2) return false;

    if (fileBasicAuthData.containsKey(splitAuth[0])) {
      String storedHash = new String(Base64.decodeBase64(fileBasicAuthData.get(splitAuth[0])));

      MessageDigest digest;
      try {
        digest = MessageDigest.getInstance("SHA-256"); // $NON-NLS-1$
        digest.update(splitAuth[1].getBytes());

        String receivedHash = new String(digest.digest());

        if (storedHash.equals(receivedHash)) {
          return true;
        }
      } catch (NoSuchAlgorithmException e) {
        logger.error(e.getMessage(), e.getCause());
      }
    }

    request
        .response()
        .headers()
        .add(
            "WWW-Authenticate",
            "Basic realm=\"" + config.getRealm() + "\""); // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

    return false;
  }
 @Override
 public void handle(HttpServerRequest request) {
   if (!config.isAuthenticationEnabled() || authenticate(request)) {
     super.handle(request);
   } else {
     notAuthorised(request.response());
   }
 }
 @Override
 public void handle(HttpServerRequest req) {
   Path p = Paths.get(assetRoot, req.path().substring(1));
   if (fileSystem.existsSync(p.toString())) {
     super.handle(req);
   } else {
     req.response().end("File Not Found!");
   }
 }
 @Override
 public void handle(HttpServerRequest request) {
   request.response().setStatusCode(200);
   request.response().putHeader(WebStrings.CONTENT_TYPE, WebStrings.APP_JSON);
   log.info("Put Object Request");
   String reqId = UUID.randomUUID().toString();
   UploadEncObjectReplyHandler replyHandler = new UploadEncObjectReplyHandler(reqId, request);
   UploadEncObjectBodyHandler bodyHanlder = new UploadEncObjectBodyHandler(reqId, replyHandler);
   replyHandlers.put(reqId, replyHandler);
   bodyHandlers.put(reqId, bodyHanlder);
   request.bodyHandler(bodyHanlder);
 }
 @Override
 public void handle(JsonObject message) {
   if (message.containsField("error")) {
     replyForError(message);
   }
   message.removeField(MessageUtils.FROM);
   message.removeField(MessageUtils.TO);
   message.removeField(MessageUtils.COMTYPE);
   message.removeField(MessageUtils.MSGID);
   message.removeField(MessageUtils.MSGTYPE);
   request.response().end(message.toString());
   cleanup(requestId);
 }
 @Override
 public void handle(HttpServerRequest req) {
   String path = req.path();
   switch (path.charAt(1)) {
     case 'p':
       handlePlainText(req);
       break;
     case 'j':
       handleJson(req);
       break;
     case 'd':
       handleDbMongo(req);
       break;
     default:
       req.response().setStatusCode(404);
       req.response().end();
   }
 }
  /** text/javascript text/css text/html; charset=GBK */
  @Override
  public void handle(final HttpServerRequest req) {

    final HttpServerResponse res = req.response();

    String uri = req.path().substring(1);

    if (uri.startsWith("web")) {
      uri = uri.substring(4);
    }
    if (uri.isEmpty()) {
      uri = indexPage;
    }
    String ext = Utils.getFileExtWithDot(uri);
    if (".js".equals(ext) || ".css".equals(ext) || ".html".equals(ext)) {
      int i = 0;
      if (".css".equals(ext)) {
        i = 1;
      } else {
        i = 2;
      }
      res.headers().set("content-type", ctary[i]);
      String finalFile = webRootPath.resolve(uri).toString();

      //      if(fileSystem.existsSync(finalFile)){
      //        fileSystem.readFile(finalFile, new AsyncResultHandler<Buffer>() {
      //          public void handle(AsyncResult<Buffer> ar) {
      //            if (ar.succeeded()) {
      //              res.headers().set("content-length", ar.result().length() + "");
      //              res.end(ar.result());
      //            } else {
      //              logger.error("Failed to open file", ar.cause());
      //              res.end("File Read Error!");
      //            }
      //          }
      //        });
      //      } else {
      //        res.end("File Not Found!");
      //      }

      fileSystem.open(
          finalFile,
          new AsyncResultHandler<AsyncFile>() {
            public void handle(AsyncResult<AsyncFile> ar) {
              if (ar.succeeded()) {
                AsyncFile asyncFile = ar.result();
                res.setChunked(true);
                Pump.createPump(asyncFile, res).start();
                asyncFile.endHandler(
                    new VoidHandler() {
                      public void handle() {
                        res.end();
                      }
                    });
              } else {
                logger.error("Failed to open file", ar.cause());
                res.end("File Not Found!");
              }
            }
          });
    } else {
      res.sendFile(webRootPath.resolve(uri).toString());
    }
  }
 @Override
 public void setContentType(String contentType) {
   request.response().putHeader("Content-Type", contentType);
 }
 public VertxHttpRequestWrapper(HttpServerRequest request) {
   this.request = request;
   this.response = request.response();
 }
Example #12
0
 private void processRequest(String targetID, HttpServerRequest req) {
   String result = retrieveDetails(targetID);
   if (result != null) req.response().end(result);
   else req.response().end("No response received");
 }
 private void handlePlainText(HttpServerRequest req) {
   req.response().putHeader("Content-Type", "application/json; charset=UTF-8");
   req.response().putHeader("Content-Length", helloWorldContentLength);
   req.response().end(helloWorldBuffer);
 }