@Override public void handle(Request request, Response response) { super.handle(request, response); if (Method.GET.equals(request.getMethod())) { response.setEntity(getSwagger()); } else { response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED); } }
/** * Handles a call by invoking the next Restlet if it is available. * * @param request The request to handle. * @param response The response to update. */ @Override public void handle(Request request, Response response) { super.handle(request, response); Restlet next = getNext(request, response); if (next != null) { doHandle(next, request, response); } else { response.setStatus(Status.CLIENT_ERROR_NOT_FOUND); } }
@Override public void handle(Request request, Response response) { super.handle(request, response); try { if (request.getMethod().equals(Method.GET)) { try { if ("users".equalsIgnoreCase(request.getResourceRef().getLastSegment())) { UserListValue users = importer.users(); StringRepresentation result = new StringRepresentation( users.toJSON(), MediaType.APPLICATION_JSON, Language.DEFAULT, CharacterSet.UTF_8); response.setStatus(Status.SUCCESS_OK); response.setEntity(result); } else if ("groups".equalsIgnoreCase(request.getResourceRef().getLastSegment())) { GroupListValue groups = importer.groups(); StringRepresentation result = new StringRepresentation( groups.toJSON(), MediaType.APPLICATION_JSON, Language.DEFAULT, CharacterSet.UTF_8); response.setStatus(Status.SUCCESS_OK); response.setEntity(result); } } catch (ResourceException e) { response.setStatus(Status.CLIENT_ERROR_UNAUTHORIZED); response.setEntity(e.getStatus().getDescription(), MediaType.TEXT_PLAIN); } } else { response.setStatus(Status.CLIENT_ERROR_METHOD_NOT_ALLOWED); } } finally { request.release(); } }
/** * Redirects a given call on the server-side to a next Restlet with a given target reference. In * the default implementation, the request HTTP headers, stored in the request's attributes, are * removed before dispatching. After dispatching, the response HTTP headers are also removed to * prevent conflicts with the main call. * * @param next The next Restlet to forward the call to. * @param targetRef The target reference with URI variables resolved. * @param request The request to handle. * @param response The response to update. */ protected void serverRedirect( Restlet next, Reference targetRef, Request request, Response response) { if (next == null) { getLogger().warning("No next Restlet provided for server redirection to " + targetRef); } else { // Save the base URI if it exists as we might need it for // redirections Reference resourceRef = request.getResourceRef(); Reference baseRef = resourceRef.getBaseRef(); // Reset the protocol and let the dispatcher handle the protocol request.setProtocol(null); // Update the request to cleanly go to the target URI request.setResourceRef(targetRef); request.getAttributes().remove(HeaderConstants.ATTRIBUTE_HEADERS); next.handle(request, response); // Allow for response rewriting and clean the headers response.setEntity(rewrite(response.getEntity())); response.getAttributes().remove(HeaderConstants.ATTRIBUTE_HEADERS); request.setResourceRef(resourceRef); // In case of redirection, we may have to rewrite the redirect URI if (response.getLocationRef() != null) { Template rt = new Template(this.targetTemplate); rt.setLogger(getLogger()); int matched = rt.parse(response.getLocationRef().toString(), request); if (matched > 0) { String remainingPart = (String) request.getAttributes().get("rr"); if (remainingPart != null) { response.setLocationRef(baseRef.toString() + remainingPart); } } } } }
/** * Effectively handles the call using the selected next {@link Restlet}, typically the selected * {@link Route}. By default, it just invokes the next Restlet. * * @param next The next Restlet to invoke. * @param request The request. * @param response The response. */ protected void doHandle(Restlet next, Request request, Response response) { next.handle(request, response); }
/** * Handles a call. * * @param request The request to handle. * @param onReceivedCallback The callback invoked upon request reception. */ public final void handle(Request request, Uniform onReceivedCallback) { Response response = new Response(request); handle(request, response, onReceivedCallback); }
/** * Handles a call. * * @param request The request to handle. * @param response The response to update. * @param onResponseCallback The callback invoked upon response reception. */ public final void handle(Request request, Response response, Uniform onResponseCallback) { request.setOnResponse(onResponseCallback); handle(request, response); }
/** * Handles a call. Creates an empty {@link Response} object and then invokes {@link * #handle(Request, Response)}. * * @param request The request to handle. * @return The returned response. */ public final Response handle(Request request) { Response response = new Response(request); handle(request, response); return response; }