Java Code Examples for org.restlet.Response#setStatus()

The following examples show how to use org.restlet.Response#setStatus() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractRestApplication.java    From FoxBPM with Apache License 2.0 6 votes vote down vote up
public void initializeAuthentication() {
	if(verifier == null){
		verifier = new DefaultSecretVerifier();
	}
	authenticator = new ChallengeAuthenticator(null, false, ChallengeScheme.HTTP_BASIC, "Foxbpm Realm") {
		protected boolean authenticate(Request request, Response response) {
			if (restAuthenticator != null && !restAuthenticator.requestRequiresAuthentication(request)) {
				return true;
			}
			if (request.getChallengeResponse() == null) {
				response.setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
				return false;
			} else {
				boolean authenticated = super.authenticate(request, response);
				if (authenticated && restAuthenticator != null) {
					authenticated = restAuthenticator.isRequestAuthorized(request);
				}
				return authenticated;
			}
		}
	};
	authenticator.setVerifier(verifier);
}
 
Example 2
Source File: ExceptionProcessor.java    From container with Apache License 2.0 6 votes vote down vote up
@Override
public void process(final Exchange exchange) throws Exception {

    ExceptionProcessor.LOG.debug("Exception handling...");

    final Response response = exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);

    if (exchange.getIn().getBody() instanceof ParseException) {
        response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        final String body = exchange.getIn().getBody(String.class);
        response.setEntity("JSON is not valid: " + body, MediaType.TEXT_ALL);
        ExceptionProcessor.LOG.warn("JSON is not valid: {}", body);
    } else if (exchange.getIn().getBody() instanceof NullPointerException) {
        response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        response.setEntity("Needed information not specified.", MediaType.TEXT_ALL);
        ExceptionProcessor.LOG.warn("Needed information not specified.");
    } else if (exchange.getIn().getBody() instanceof Exception) {
        response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        response.setEntity("Invocation failed! " + exchange.getIn().getBody().toString(), MediaType.TEXT_ALL);
        ExceptionProcessor.LOG.warn("Invocation failed! " + exchange.getIn().getBody().toString());
    }

    exchange.getOut().setBody(response);
}
 
Example 3
Source File: ExceptionProcessor.java    From container with Apache License 2.0 6 votes vote down vote up
@Override
public void process(final Exchange exchange) throws Exception {

    ExceptionProcessor.LOG.debug("Exception handling...");

    final Response response = exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);

    if (exchange.getIn().getBody() instanceof ParseException) {
        response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        response.setEntity("JSON is not valid: " + exchange.getIn().getBody(String.class), MediaType.TEXT_ALL);
    } else if (exchange.getIn().getBody() instanceof NullPointerException) {
        response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        response.setEntity("Needed information not specified.", MediaType.TEXT_ALL);
    } else if (exchange.getIn().getBody() instanceof ApplicationBusExternalException) {
        response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        response.setEntity(exchange.getIn().getBody(String.class), MediaType.TEXT_ALL);
    } else if (exchange.getIn().getBody() instanceof ApplicationBusInternalException) {
        response.setStatus(Status.SERVER_ERROR_INTERNAL);
        response.setEntity(exchange.getIn().getBody(String.class), MediaType.TEXT_ALL);
    }

    exchange.getOut().setBody(response);
}
 
Example 4
Source File: IsFinishedResponseProcessor.java    From container with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void process(final Exchange exchange) throws Exception {

    IsFinishedResponseProcessor.LOG.debug("Processing IsFinished response....");

    final String requestID = exchange.getIn().getHeader(InvocationRoute.ID, String.class);

    IsFinishedResponseProcessor.LOG.debug("RequestID: {}", requestID);

    final Response response = exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);

    if (exchange.getIn().getBody() instanceof Exception) {

        response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
        response.setEntity(exchange.getIn().getBody(String.class), MediaType.TEXT_ALL);
    } else {

        final Boolean isFinished = exchange.getIn().getBody(Boolean.class);

        if (isFinished) {
            IsFinishedResponseProcessor.LOG.debug("Invocation has finished, send location of result.");

            response.setStatus(Status.REDIRECTION_SEE_OTHER);
            response.setLocationRef(InvocationRoute.GET_RESULT_ENDPOINT.replace(InvocationRoute.ID_PLACEHODLER,
                requestID));
        } else {
            IsFinishedResponseProcessor.LOG.debug("Invocation has not finished yet.");

            final JSONObject obj = new JSONObject();
            obj.put("status", "PENDING");

            response.setStatus(Status.SUCCESS_OK);
            response.setEntity(obj.toJSONString(), MediaType.APPLICATION_JSON);
        }
        exchange.getOut().setBody(response);
    }
}
 
Example 5
Source File: IsFinishedResponseProcessor.java    From container with Apache License 2.0 5 votes vote down vote up
@Override
public void process(final Exchange exchange) throws Exception {

    IsFinishedResponseProcessor.LOG.debug("Processing IsFinished response....");

    final String requestID = exchange.getIn().getHeader(Route.ID, String.class);

    IsFinishedResponseProcessor.LOG.debug("RequestID: {}", requestID);

    final Response response = exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);

    if (exchange.getIn().getBody() instanceof Exception) {

        response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
        response.setEntity(exchange.getIn().getBody(String.class), MediaType.TEXT_ALL);
    } else {

        final Boolean isFinished = exchange.getIn().getBody(Boolean.class);

        if (isFinished) {
            IsFinishedResponseProcessor.LOG.debug("Invocation has finished, send location of result.");

            response.setStatus(Status.REDIRECTION_SEE_OTHER);
            response.setLocationRef(Route.GET_RESULT_ENDPOINT.replace(Route.ID_PLACEHODLER, requestID));
        } else {
            IsFinishedResponseProcessor.LOG.debug("Invocation has not finished yet.");

            final JSONObject obj = new JSONObject();
            obj.put("status", "PENDING");

            response.setStatus(Status.SUCCESS_OK);
            response.setEntity(obj.toJSONString(), MediaType.APPLICATION_JSON);
        }
        exchange.getOut().setBody(response);
    }
}
 
Example 6
Source File: InvocationResponseProcessor.java    From container with Apache License 2.0 5 votes vote down vote up
@Override
public void process(final Exchange exchange) throws Exception {

    InvocationResponseProcessor.LOG.debug("Processing Invocation response....");

    final String requestID = exchange.getIn().getBody(String.class);

    InvocationResponseProcessor.LOG.debug("RequestID: {}", requestID);

    final Response response = exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);
    response.setStatus(Status.SUCCESS_ACCEPTED);
    response.setLocationRef(Route.POLL_ENDPOINT.replace(Route.ID_PLACEHODLER, requestID));
    exchange.getOut().setBody(response);
}
 
Example 7
Source File: WorkspaceServerResource.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void doCatch(final Throwable throwable) {
    super.doCatch(throwable);
    final Response response = getResponse();
    if (response != null) {
        final Throwable cause = throwable.getCause();
        if (cause instanceof ResourceNotFoundException) {
            response.setStatus(Status.CLIENT_ERROR_NOT_FOUND, cause);
        }
        if (cause instanceof LockedResourceException) {
            response.setStatus(Status.CLIENT_ERROR_LOCKED, cause);
        }
    }
    logException("WorkspaceServerResource internal error", throwable);
}
 
Example 8
Source File: TaskResource.java    From spring-5-examples with MIT License 4 votes vote down vote up
@Override
public void handle(Request request, Response response) {
  super.handle(request, response);

  try {
    final Identifier identifier = new Identifier(Long.parseLong(request.getAttributes().get("task").toString()));

    if (request.getMethod().isSafe()) {
      final InboxModel.InboxTask value = model.getTasks().get(identifier);

      if (value == null) {
        response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
        return;
      }

      response.setEntity(new WriterRepresentation(MediaType.TEXT_HTML) {
        @Override
        public void write(Writer writer) throws IOException {
          VelocityContext context = new VelocityContext();
          context.put("task", value);
          velocity.getTemplate("inbox/task/task.html").merge(context, writer);
        }
      });
    } else {
      Inbox inbox = (Inbox) request.getAttributes().get("inbox");

      repository.<Task>update().apply("task").apply(identifier).apply(
          task ->
          {
            inbox.select(task);

            Form form = new Form(request.getEntity());

            String command = request.getAttributes().get("command").toString();
            switch (command) {
              case "changedescription": {
                Inbox.ChangeDescription changeDescription = new Inbox.ChangeDescription();
                changeDescription.description = form.getFirstValue("description");

                Inbox.changeDescription().
                    apply(inbox).
                    apply(changeDescription);
                break;
              }

              case "done": {
                Inbox.TaskDone taskDone = new Inbox.TaskDone();
                taskDone.done = form.getFirstValue("done") != null;

                Inbox.done().
                    apply(inbox).
                    apply(taskDone);
                break;
              }

              default: {
                response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST,
                    "Unknown command:" + command);
              }
            }
          }
      );


      response.redirectSeeOther(request.getReferrerRef());
    }
  } catch (Exception e) {
    response.setStatus(Status.SERVER_ERROR_INTERNAL, e);
  }
}
 
Example 9
Source File: IsFinishedResponseProcessor.java    From container with Apache License 2.0 4 votes vote down vote up
@Override
public void process(final Exchange exchange) throws Exception {

    IsFinishedResponseProcessor.LOG.debug("Processing IsFinished response....");

    final String requestID = exchange.getIn().getHeader(Route.ID, String.class);

    IsFinishedResponseProcessor.LOG.debug("RequestID: {}", requestID);

    final Response response = exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);

    if (exchange.getIn().getBody() instanceof Exception) {

        response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
        response.setEntity(exchange.getIn().getBody(String.class), MediaType.TEXT_ALL);
    } else {

        final Boolean isFinished = exchange.getIn().getBody(Boolean.class);

        if (isFinished) {
            IsFinishedResponseProcessor.LOG.debug("Invocation has finished, send location of result.");

            final String pollingURI = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class);
            final String getResultURI =
                pollingURI + Route.GET_RESULT_ENDPOINT_SUFFIX.replace(Route.ID_PLACEHODLER, requestID);

            IsFinishedResponseProcessor.LOG.debug("GetResult URI: {}", getResultURI);

            response.setStatus(Status.REDIRECTION_SEE_OTHER);
            response.setLocationRef(getResultURI);
        } else {
            IsFinishedResponseProcessor.LOG.debug("Invocation has not finished yet.");

            final String acceptContentType = exchange.getIn().getHeader(Exchange.ACCEPT_CONTENT_TYPE, String.class);

            IsFinishedResponseProcessor.LOG.debug("AcceptContentType: {}", acceptContentType);

            if (acceptContentType.equals(MediaType.APPLICATION_JSON)) {

                final JSONObject obj = new JSONObject();
                obj.put("status", "PENDING");

                response.setStatus(Status.SUCCESS_OK);
                response.setEntity(obj.toJSONString(), MediaType.APPLICATION_JSON);
            } else if (acceptContentType.equals(MediaType.APPLICATION_XML)) {

                response.setStatus(Status.SUCCESS_OK);
                response.setEntity("<status>PENDING</status>", MediaType.APPLICATION_XML);
            } else {
                IsFinishedResponseProcessor.LOG.warn("The requested entity media type is not supported.");
                throw new ApplicationBusExternalException("The requested entity media type is not supported.",
                    Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE.getCode());
            }
        }
        exchange.getOut().setBody(response);
    }
}
 
Example 10
Source File: GetResultResponseProcessor.java    From container with Apache License 2.0 4 votes vote down vote up
@Override
public void process(final Exchange exchange) throws Exception {

    GetResultResponseProcessor.LOG.debug("Processing GetResult response....");

    final String requestID = exchange.getIn().getHeader(Route.ID, String.class);

    GetResultResponseProcessor.LOG.debug("RequestID: {}", requestID);

    final Response response = exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);

    if (exchange.getIn().getBody() instanceof Exception) {

        response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
        response.setEntity(exchange.getIn().getBody(String.class), MediaType.TEXT_ALL);
    } else {

        final Form httpHeaders = (Form) exchange.getIn().getHeader("org.restlet.http.headers");
        final String acceptContentType = httpHeaders.getValues("Accept").toString();

        GetResultResponseProcessor.LOG.debug("AcceptContentType: {}", acceptContentType);

        final String result = exchange.getIn().getBody(String.class);

        if (acceptContentType.equals(MediaType.APPLICATION_JSON.getName())) {

            final JSONObject obj = new JSONObject();
            obj.put("result", result);

            response.setStatus(Status.SUCCESS_OK);
            response.setEntity(obj.toJSONString(), MediaType.APPLICATION_JSON);
        } else if (acceptContentType.equals(MediaType.APPLICATION_XML.getName())) {

            response.setStatus(Status.SUCCESS_OK);
            response.setEntity("<result>" + result + "</result>", MediaType.APPLICATION_XML);
        } else {
            GetResultResponseProcessor.LOG.warn("The requested entity media type (Accept header) is not supported. Supported types are {} and {}",
                MediaType.APPLICATION_JSON.getName(),
                MediaType.APPLICATION_XML.getName());
            throw new ApplicationBusExternalException(
                "The requested request entity media type (Accept header) is not supported. Supported types are "
                    + MediaType.APPLICATION_JSON.getName() + " and " + MediaType.APPLICATION_XML.getName(),
                Status.CLIENT_ERROR_UNSUPPORTED_MEDIA_TYPE.getCode());
        }
    }

    exchange.getOut().setBody(response);
}
 
Example 11
Source File: InvocationResponseProcessor.java    From container with Apache License 2.0 4 votes vote down vote up
@Override
public void process(final Exchange exchange) throws Exception {

    InvocationResponseProcessor.LOG.debug("Processing Invocation response....");

    final String requestID = exchange.getIn().getBody(String.class);

    InvocationResponseProcessor.LOG.debug("RequestID: {}", requestID);

    final String invokeURI = exchange.getIn().getHeader(Exchange.HTTP_URI, String.class);
    final String pollingURI = invokeURI + Route.POLL_ENDPOINT_SUFFIX.replace(Route.ID_PLACEHODLER, requestID);

    InvocationResponseProcessor.LOG.debug("Polling URI: {}", pollingURI);

    final Response response = exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);
    response.setStatus(Status.SUCCESS_ACCEPTED);
    response.setLocationRef(pollingURI);
    exchange.getOut().setBody(response);
}
 
Example 12
Source File: GetResultResponseProcessor.java    From container with Apache License 2.0 3 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void process(final Exchange exchange) throws Exception {

    GetResultResponseProcessor.LOG.debug("Processing GetResult response....");

    final String requestID = exchange.getIn().getHeader(InvocationRoute.ID, String.class);

    GetResultResponseProcessor.LOG.debug("RequestID: {}", requestID);

    final Response response = exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);

    if (exchange.getIn().getBody() instanceof Exception) {

        response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
        response.setEntity(exchange.getIn().getBody(String.class), MediaType.TEXT_ALL);
    } else {

        final HashMap<String, String> responseMap = exchange.getIn().getBody(HashMap.class);

        final JSONObject obj = new JSONObject();
        obj.put("response", responseMap);

        response.setStatus(Status.SUCCESS_OK);
        response.setEntity(obj.toJSONString(), MediaType.APPLICATION_JSON);
    }

    exchange.getOut().setBody(response);
}
 
Example 13
Source File: InvocationResponseProcessor.java    From container with Apache License 2.0 3 votes vote down vote up
@Override
public void process(final Exchange exchange) throws Exception {

    InvocationResponseProcessor.LOG.debug("Processing Invocation response....");

    final String requestID = exchange.getIn().getBody(String.class);

    InvocationResponseProcessor.LOG.debug("RequestID: {}", requestID);

    final Response response = exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);
    response.setStatus(Status.SUCCESS_ACCEPTED);
    response.setLocationRef(InvocationRoute.POLL_ENDPOINT.replace(InvocationRoute.ID_PLACEHODLER, requestID));

    exchange.getOut().setBody(response);
}
 
Example 14
Source File: GetResultResponseProcessor.java    From container with Apache License 2.0 3 votes vote down vote up
@Override
public void process(final Exchange exchange) throws Exception {

    GetResultResponseProcessor.LOG.debug("Processing GetResult response....");

    final String requestID = exchange.getIn().getHeader(Route.ID, String.class);

    GetResultResponseProcessor.LOG.debug("RequestID: {}", requestID);

    final Response response = exchange.getIn().getHeader(RestletConstants.RESTLET_RESPONSE, Response.class);

    if (exchange.getIn().getBody() instanceof Exception) {

        response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
        response.setEntity(exchange.getIn().getBody(String.class), MediaType.TEXT_ALL);
    } else {

        final String result = exchange.getIn().getBody(String.class);

        final JSONObject obj = new JSONObject();
        obj.put("result", result);

        response.setStatus(Status.SUCCESS_OK);
        response.setEntity(obj.toJSONString(), MediaType.APPLICATION_JSON);
    }

    exchange.getOut().setBody(response);
}