Пример #1
0
  private Response doSyncUpdate(final Request request) {
    loggerContext.init(getRequestId(request.getRemoteAddress()));

    try {
      if (!sourceMatchesContext(request.getSource())) {
        return Response.status(Response.Status.BAD_REQUEST)
            .entity("Invalid source specified: " + request.getSource())
            .build();
      }

      boolean notificationsEnabled = true;
      if (request.isParam(Command.REDIRECT)) {
        if (!ipRanges.isTrusted(IpInterval.parse(request.getRemoteAddress()))) {
          return Response.status(Response.Status.FORBIDDEN)
              .entity("Not allowed to disable notifications: " + request.getRemoteAddress())
              .build();
        }

        notificationsEnabled = false;
      }

      if (!request.hasParam(Command.DATA) && request.isParam(Command.NEW)) {
        return Response.status(Response.Status.BAD_REQUEST)
            .entity("DATA parameter is missing")
            .build();
      }

      if (!request.hasParam(Command.DATA) && !request.isParam(Command.HELP)) {
        return Response.status(Response.Status.BAD_REQUEST).entity("Invalid request").build();
      }

      loggerContext.log("msg-in.txt", new SyncUpdateLogCallback(request.toString()));

      final UpdateContext updateContext = new UpdateContext(loggerContext);

      final String content = request.hasParam("DATA") ? request.getParam("DATA") : "";

      final UpdateRequest updateRequest =
          new UpdateRequest(
              new SyncUpdate(dateTimeProvider, request.getRemoteAddress()),
              getKeyword(request),
              content,
              updatesParser.parse(
                  updateContext, Lists.newArrayList(new ContentWithCredentials(content))),
              notificationsEnabled);

      final UpdateResponse updateResponse =
          updateRequestHandler.handle(updateRequest, updateContext);
      loggerContext.log("msg-out.txt", new SyncUpdateLogCallback(updateResponse.getResponse()));
      return getResponse(updateResponse);

    } finally {
      loggerContext.remove();
    }
  }
Пример #2
0
  private Response getResponse(final UpdateResponse updateResponse) {
    final Response.Status status = getResponseStatus(updateResponse);
    if (status != Response.Status.OK) {
      return Response.status(status).entity(updateResponse.getResponse()).build();
    }

    return Response.ok()
        .header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN)
        .header(HttpHeaders.CONTENT_LENGTH, updateResponse.getResponse().length())
        .entity(updateResponse.getResponse())
        .build();
  }
Пример #3
0
 private Response.Status getResponseStatus(final UpdateResponse updateResponse) {
   switch (updateResponse.getStatus()) {
     case EXCEPTION:
       return Response.Status.INTERNAL_SERVER_ERROR;
     case FAILED_AUTHENTICATION:
       return Response.Status.UNAUTHORIZED;
     default:
       return Response.Status.OK;
   }
 }