List of usage examples for org.apache.http.client.methods HttpPatch HttpPatch
public HttpPatch(final String uri)
From source file:ru.histone.resourceloaders.DefaultResourceLoader.java
private StreamResource loadHttpResource(URI location, Node[] args) { URI newLocation = URI.create(location.toString().replace("#fragment", "")); final Map<Object, Node> requestMap = args != null && args.length != 0 && args[0] instanceof ObjectHistoneNode ? ((ObjectHistoneNode) args[0]).getElements() : new HashMap<Object, Node>(); Node methodNode = requestMap.get("method"); final String method = methodNode != null && methodNode.isString() && methodNode.getAsString().getValue().length() != 0 ? requestMap.get("method").getAsString().getValue() : "GET"; final Map<String, String> headers = new HashMap<String, String>(); if (requestMap.containsKey("headers")) { for (Map.Entry<Object, Node> en : requestMap.get("headers").getAsObject().getElements().entrySet()) { String value = null;//www .java 2 s . c o m if (en.getValue().isUndefined()) value = "undefined"; else value = en.getValue().getAsString().getValue(); headers.put(en.getKey().toString(), value); } } final Map<String, String> filteredHeaders = filterRequestHeaders(headers); final Node data = requestMap.containsKey("data") ? (Node) requestMap.get("data") : null; // Prepare request HttpRequestBase request = new HttpGet(newLocation); if ("POST".equalsIgnoreCase(method)) { request = new HttpPost(newLocation); } else if ("PUT".equalsIgnoreCase(method)) { request = new HttpPut(newLocation); } else if ("DELETE".equalsIgnoreCase(method)) { request = new HttpDelete(newLocation); } else if ("TRACE".equalsIgnoreCase(method)) { request = new HttpTrace(newLocation); } else if ("OPTIONS".equalsIgnoreCase(method)) { request = new HttpOptions(newLocation); } else if ("PATCH".equalsIgnoreCase(method)) { request = new HttpPatch(newLocation); } else if ("HEAD".equalsIgnoreCase(method)) { request = new HttpHead(newLocation); } else if (method != null && !"GET".equalsIgnoreCase(method)) { return new StreamResource(null, location.toString(), ContentType.TEXT); } for (Map.Entry<String, String> en : filteredHeaders.entrySet()) { request.setHeader(en.getKey(), en.getValue()); } if (("POST".equalsIgnoreCase(method) || "PUT".equalsIgnoreCase(method)) && data != null) { String stringData = null; String contentType = filteredHeaders.get("content-type") == null ? "" : filteredHeaders.get("content-type"); if (data.isObject()) { stringData = ToQueryString.toQueryString(data.getAsObject(), null, "&"); contentType = "application/x-www-form-urlencoded"; } else { stringData = data.getAsString().getValue(); } if (stringData != null) { StringEntity se; try { se = new StringEntity(stringData); } catch (UnsupportedEncodingException e) { throw new ResourceLoadException(String.format("Can't encode data '%s'", stringData)); } ((HttpEntityEnclosingRequestBase) request).setEntity(se); } request.setHeader("Content-Type", contentType); } if (request.getHeaders("content-Type").length == 0) { request.setHeader("Content-Type", ""); } // Execute request HttpClient client = new DefaultHttpClient(httpClientConnectionManager); ((AbstractHttpClient) client).setRedirectStrategy(new RedirectStrategy()); InputStream input = null; try { HttpResponse response = client.execute(request); input = response.getEntity() == null ? null : response.getEntity().getContent(); } catch (IOException e) { throw new ResourceLoadException(String.format("Can't load resource '%s'", location.toString())); } finally { } return new StreamResource(input, location.toString(), ContentType.TEXT); }