private boolean shouldShowCommandForm( Method interactionMethod ) { // Show form on GET/HEAD if( Request.getCurrent().getMethod().isSafe() ) { return true; } if( interactionMethod.getParameterTypes().length > 0 ) { return !( interactionMethod.getParameterTypes()[ 0 ].equals( Response.class ) || Request.getCurrent() .getEntity() .isAvailable() || Request.getCurrent().getEntityAsText() != null || Request.getCurrent() .getResourceRef() .getQuery() != null ); } return false; }
public Representation post(Representation entity) { try { final String topicName = (String) getRequest().getAttributes().get("topicName"); String jsonRequest = entity.getText(); TopicPartition topicPartitionInfo = null; if ((jsonRequest == null || jsonRequest.isEmpty()) && topicName != null getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND); return new StringRepresentation(String.format( "Failed to add new topic: %s, it is already existed!", topicPartitionInfo.getTopic())); getResponse().setStatus(Status.SERVER_ERROR_INTERNAL); return new StringRepresentation( String.format("Failed to add new topic, with exception: %s", e));
Filter preferencesFilter = new Filter(getContext()) { protected int beforeHandle(Request request, Response response) { if (request.getClientInfo().getAcceptedMediaTypes().isEmpty()) { request.getClientInfo().accept(MediaType.APPLICATION_JSON); } else if ((request.getClientInfo().getAcceptedMediaTypes().size() == 1) && (request.getClientInfo().getAcceptedMediaTypes().get(0).getMetadata().equals(MediaType.ALL))) { request.getClientInfo().accept(MediaType.APPLICATION_JSON); } return super.beforeHandle(request, response); } }
public Request getTopicCreationRequestUrl(String topic, int numPartitions) { Request request = new Request(Method.POST, _baseUrl + "/topics/"); TopicPartition topicPartitionInfo = new TopicPartition(topic, numPartitions); request.setEntity(topicPartitionInfo.toJSON().toJSONString(), MediaType.APPLICATION_JSON); return request; }
/** * @see org.restlet.ext.jaxrs.internal.wrappers.params.ParameterList.ParamGetter#getValue() */ public Object getValue() throws InvocationTargetException, ConvertRepresentationException, WebApplicationException { return Request.getCurrent().getEntity(); }
/** * Indicates if the request entity should be chunked. * * @return True if the request should be chunked */ protected boolean shouldRequestBeChunked(Request request) { return request.isEntityAvailable() && (request.getEntity() != null) && !request.getEntity().hasKnownSize(); } }
private void addHeaderAttribute(ClientResource clientResource, String attrName, String attrValue) { ConcurrentMap<String, Object> attrs = clientResource.getRequest().getAttributes(); Series<Header> headers = (Series<Header>) attrs.get(HeaderConstants.ATTRIBUTE_HEADERS); if (headers == null) { headers = new Series<>(Header.class); Series<Header> prev = (Series<Header>) attrs.putIfAbsent(HeaderConstants.ATTRIBUTE_HEADERS, headers); if (prev != null) { headers = prev; } } headers.add(attrName, attrValue); }
@Override @Delete public Representation delete() { final String topicName = (String) getRequest().getAttributes().get("topicName"); if (_autoTopicWhitelistingManager != null) { _autoTopicWhitelistingManager.addIntoBlacklist(topicName); } if (!_helixMirrorMakerManager.isTopicExisted(topicName)) { getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND); return new StringRepresentation( String.format("Failed to delete not existed topic: %s", topicName)); } try { _helixMirrorMakerManager.deleteTopicInMirrorMaker(topicName); return new StringRepresentation( String.format("Successfully finished delete topic: %s", topicName)); } catch (Exception e) { getResponse().setStatus(Status.SERVER_ERROR_INTERNAL); LOGGER.error("Failed to delete topic: {}, with exception: {}", topicName, e); return new StringRepresentation( String.format("Failed to delete topic: %s, with exception: %s", topicName, e)); } }
@Override public void handle(Request request, Response response) { // Print the user name of the requested orders String message = "Order \"" + request.getAttributes().get("order") + "\" for user \"" + request.getAttributes().get("user") + "\""; response.setEntity(message, MediaType.TEXT_PLAIN); } };
@Override @Get public Representation get() { final String opt = (String) getRequest().getAttributes().get("opt"); if ("disable_autobalancing".equalsIgnoreCase(opt)) { _helixMirrorMakerManager.disableAutoBalancing(); LOGGER.info("Disabled autobalancing!"); return new StringRepresentation("Disabled autobalancing!\n"); } else if ("enable_autobalancing".equalsIgnoreCase(opt)) { _helixMirrorMakerManager.enableAutoBalancing(); LOGGER.info("Enabled autobalancing!"); return new StringRepresentation("Enabled autobalancing!\n"); } LOGGER.info("No valid input!"); return new StringRepresentation("No valid input!\n"); }
@SuppressWarnings("unchecked") private void writeRequestInfoToLog(ClientResource clientResource) { ConcurrentMap<String, Object> attrs = clientResource.getRequest().getAttributes(); Series<Header> headers = (Series<Header>) attrs.get(HeaderConstants.ATTRIBUTE_HEADERS); logger.info("Request to Poulpe: requested URI - {}, request headers - {}, request body - {}", new Object[]{clientResource.getRequest().getResourceRef(), headers, clientResource.getRequest()}); }
@Override protected void handleLocal(Request request, Response response, String decodedPath) { String scheme = request.getResourceRef().getScheme(); if (scheme.equalsIgnoreCase(Protocol.CLAP.getSchemeName())) { LocalReference cr = new LocalReference(request.getResourceRef()); ClassLoader classLoader = null; Object classLoaderAttribute = request.getAttributes().get( "org.restlet.clap.classLoader");
public static void main(String[] args) throws Exception { // Prepare the request Request request = new Request(Method.GET, "http://s3.amazonaws.com/quotes/nelson"); request.setChallengeResponse(new ChallengeResponse( ChallengeScheme.HTTP_AWS_S3, "44CF9590006BF252F707", "OtxrzxIsfpFjA7SwPzILwy8Bw21TLhquhboDYROV")); // Add some extra headers Series<Header> extraHeaders = new Series<Header>(Header.class); extraHeaders.add("X-Amz-Meta-Author", "foo@bar.com"); extraHeaders.add("X-Amz-Magic", "abracadabra"); // For the test we hard coded a special date header. Normally you don't // need this as the // HTTP client connector will automatically provide an accurate Date // header and use it // for authentication. // extraHeaders.add("X-Amz-Date", "Thu, 17 Nov 2005 18:49:58 GMT"); request.getAttributes().put(HeaderConstants.ATTRIBUTE_HEADERS, extraHeaders); // Handle it using an HTTP client connector Client client = new Client(Protocol.HTTP); Response response = client.handle(request); // Write the response entity on the console Representation output = response.getEntity(); output.write(System.out); }
@Override public void handle(Request request, Response response) { try { if (Protocol.FTP.equals(request.getProtocol())) { if (Method.GET.equals(request.getMethod())) { Reference ftpRef = request.getResourceRef(); String userInfo = null; if ((request.getChallengeResponse() != null) && ChallengeScheme.FTP_PLAIN.equals(request .getChallengeResponse().getScheme()) && (request.getChallengeResponse().getIdentifier() != null)) { userInfo = request.getChallengeResponse() .getIdentifier(); if (request.getChallengeResponse().getSecret() != null) { userInfo += ":" + new String(request.getChallengeResponse() .getSecret()); ftpRef.setUserInfo(userInfo); Entity.updateMetadata(request.getResourceRef().getPath(), response.getEntity(), true, getMetadataService()); } else { getLogger() response.setStatus(Status.CONNECTOR_ERROR_INTERNAL, e.getMessage());
/** * Generates a CallLog for the request and adds it to the buffer. * * @param request * The Request object associated with the request. * @param response * The Response object associated with the request. * @param duration * The duration of the request in milliseconds. * @param startTime * The time at which the request arrived to the agent as an * epoch. */ public void addCallLogToBuffer(Request request, Response response, int duration, long startTime) { CallLog callLog = new CallLog(); callLog.setDate(new Date(startTime)); callLog.setDuration(duration); callLog.setMethod(request.getMethod().getName()); callLog.setPath(request.getResourceRef().getPath()); callLog.setRemoteIp(request.getClientInfo().getUpstreamAddress()); callLog.setStatusCode(response.getStatus().getCode()); callLog.setUserAgent(request.getClientInfo().getAgent()); callLog.setUserToken((request.getClientInfo().getUser() == null) ? "" : request.getClientInfo().getUser().getIdentifier()); callLogs.add(callLog); if (callLogs.size() >= bufferSize) { flushLogs(); } }
/** * Assumes that there is a "callback" query parameter available in the URI * query string, containing the name of the JavaScript callback method. */ @Override public void afterHandle(Request request, Response response) { // Check the presence of the callback parameter String callback = request.getResourceRef().getQueryAsForm() .getFirstValue("callback"); if (callback != null) { Representation entity = response.getEntity(); if (entity != null && ("text".equals(entity.getMediaType().getMainType()) || MediaType.APPLICATION_JSON .equals(entity.getMediaType()))) { response.setEntity(new JsonpRepresentation(callback, response .getStatus(), response.getEntity())); response.setStatus(Status.SUCCESS_OK); } } }
@Override protected void afterHandle(Request request, Response response) { if (getRangeService().isEnabled()) { response.getServerInfo().setAcceptingRanges(true); if (request.getMethod().isSafe() && response.isEntityAvailable()) { Range responseRange = response.getEntity().getRange(); boolean rangedEntity = responseRange != null && isBytesRange(responseRange); if (request.getRanges().size() == 1 && (!request.getConditions().hasSomeRange() || request.getConditions().getRangeStatus(response.getEntity()).isSuccess())) { Range requestedRange = request.getRanges().get(0); if ((!response.getEntity().hasKnownSize()) && ((requestedRange.getIndex() == Range.INDEX_LAST || requestedRange.getSize() == Range.SIZE_MAX) if (response.getEntity().hasKnownSize() && requestedRange.getSize() > response.getEntity().getAvailableSize()) { requestedRange.setSize(Range.SIZE_MAX); response.setStatus(Status.SUCCESS_PARTIAL_CONTENT); } else if (request.getRanges().size() > 1) { response.setStatus(Status.SERVER_ERROR_NOT_IMPLEMENTED);
ClientCall httpCall) { response.setStatus(status); response.getServerInfo().setAddress(httpCall.getServerAddress()); response.getServerInfo().setPort(httpCall.getServerPort()); if (response.getEntity() != null) { if (response.getEntity().isEmpty()) { response.getEntity().release(); } else if (response.getRequest().getMethod().equals(Method.HEAD)) { response.getEntity().release(); } else if (response.getStatus().equals(Status.SUCCESS_NO_CONTENT)) { response.getEntity().release(); } else if (response.getStatus() .equals(Status.SUCCESS_RESET_CONTENT)) { response.getEntity().release(); response.setEntity(null); } else if (response.getStatus().equals( Status.REDIRECTION_NOT_MODIFIED)) { response.getEntity().release();
@Override public void challenge(Response response, boolean stale) { // Load the FreeMarker template Representation ftl = new ClientResource( LocalReference.createClapReference(getClass().getPackage()) + "/Login.ftl").get(); // Wraps the bean with a FreeMarker representation response.setEntity(new TemplateRepresentation(ftl, response .getRequest().getResourceRef(), MediaType.TEXT_HTML)); response.setStatus(Status.CLIENT_ERROR_UNAUTHORIZED); }
private String getUsecaseName( Request request ) { if( request.getMethod().equals( org.restlet.data.Method.DELETE ) ) { return "delete"; } else { return request.getResourceRef().getLastSegment(); } }