/** * Create a Commons HttpMethodBase object for the given HTTP method * and URI specification. * @param httpMethod the HTTP method * @param uri the URI * @return the Commons HttpMethodBase object */ protected HttpMethodBase createCommonsHttpMethod(HttpMethod httpMethod, String uri) { switch (httpMethod) { case GET: return new GetMethod(uri); case DELETE: return new DeleteMethod(uri); case HEAD: return new HeadMethod(uri); case OPTIONS: return new OptionsMethod(uri); case POST: return new PostMethod(uri); case PUT: return new PutMethod(uri); case TRACE: return new TraceMethod(uri); case PATCH: throw new IllegalArgumentException( "HTTP method PATCH not available before Apache HttpComponents HttpClient 4.2"); default: throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod); } }
/** * Checks the method being received and created a * suitable ResponseHandler for this method. * * @param method Method to handle * @return The handler for this response * @throws MethodNotAllowedException If no method could be choose this exception is thrown */ public static ResponseHandler createResponseHandler(HttpMethod method) throws MethodNotAllowedException { if (!AllowedMethodHandler.methodAllowed(method)) { throw new MethodNotAllowedException("The method " + method.getName() + " is not in the AllowedHeaderHandler's list of allowed methods.", AllowedMethodHandler.getAllowHeader()); } ResponseHandler handler = null; if (method.getName().equals("OPTIONS")) { handler = new OptionsResponseHandler((OptionsMethod) method); } else if (method.getName().equals("GET")) { handler = new GetResponseHandler((GetMethod) method); } else if (method.getName().equals("HEAD")) { handler = new HeadResponseHandler((HeadMethod) method); } else if (method.getName().equals("POST")) { handler = new PostResponseHandler((PostMethod) method); } else if (method.getName().equals("PUT")) { handler = new PutResponseHandler((PutMethod) method); } else if (method.getName().equals("DELETE")) { handler = new DeleteResponseHandler((DeleteMethod) method); } else if (method.getName().equals("TRACE")) { handler = new TraceResponseHandler((TraceMethod) method); } else { throw new MethodNotAllowedException("The method " + method.getName() + " was allowed by the AllowedMethodHandler, not by the factory.", handledMethods); } return handler; }
public HttpResponse trace(final RequestContext rq, Binding cmisBinding, String version, String cmisOperation) throws IOException { RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), cmisBinding, version, cmisOperation, null); String url = endpoint.getUrl(); TraceMethod req = new TraceMethod(url.toString()); return submitRequest(req, rq); }
@Test public void testTraceDisabled() throws Exception { HttpClient httpclient = new HttpClient(); TraceMethod trace = new TraceMethod("http://localhost:" + portTraceOff + "/myservice"); httpclient.executeMethod(trace); // TRACE shouldn't be allowed by default assertTrue(trace.getStatusCode() == 405); trace.releaseConnection(); }
@Test public void testTraceEnabled() throws Exception { HttpClient httpclient = new HttpClient(); TraceMethod trace = new TraceMethod("http://localhost:" + portTraceOn + "/myservice"); httpclient.executeMethod(trace); // TRACE is now allowed assertTrue(trace.getStatusCode() == 200); trace.releaseConnection(); }
/** * Create a Commons HttpMethodBase object for the given HTTP method and URI specification. * @param httpMethod the HTTP method * @param uri the URI * @return the Commons HttpMethodBase object */ protected HttpMethodBase createCommonsHttpMethod(HttpMethod httpMethod, String uri) { switch (httpMethod) { case GET: return new GetMethod(uri); case DELETE: return new DeleteMethod(uri); case HEAD: return new HeadMethod(uri); case OPTIONS: return new OptionsMethod(uri); case POST: return new PostMethod(uri); case PUT: return new PutMethod(uri); case TRACE: return new TraceMethod(uri); default: throw new IllegalArgumentException("Invalid HTTP method: " + httpMethod); } }
/** * Basic constructor setting the method, and also checks if * the request is targeted to the proxy or the underlying server. * @param method The http method */ public TraceResponseHandler(TraceMethod method) { super(method); proxyTargeted = !method.hasBeenUsed(); }