List of usage examples for org.apache.http.client.methods HttpPatch HttpPatch
public HttpPatch(final String uri)
From source file:com.adobe.acs.commons.adobeio.service.impl.EndpointServiceImpl.java
private JsonObject processPatch(@NotNull final URI uri, @NotNull final JsonObject payload, String[] headers) throws IOException { HttpPatch patch = new HttpPatch(uri); return (payload != null) && isNotBlank(payload.toString()) ? processRequestWithBody(patch, payload, headers) : new JsonObject(); }
From source file:org.fcrepo.apix.jena.impl.JenaServiceRegistry.java
@Override public ServiceInstanceRegistry instancesOf(final Service service) { init.await();/* w ww . ja va 2 s.c o m*/ final URI registryURI = objectResourceOf(service.uri().toString(), PROP_HAS_SERVICE_INSTANCE_REGISTRY, parse(service)); if (registryURI == null) { throw new ResourceNotFoundException("No service instance registry found for service " + service.uri()); } // TODO: Allow this to be pluggable to different service instance registry implementations final Model registry = getRegistry(registryURI, service); return new ServiceInstanceRegistry() { @Override public List<ServiceInstance> instances() { return objectResourcesOf(registryURI.toString(), PROP_HAS_SERVICE_INSTANCE, registry).stream() .map(uri -> new LdpServiceInstanceImpl(uri, service)).collect(Collectors.toList()); } @Override public URI addEndpoint(final URI endpoint) { LOG.debug("PATCH: Adding endpoint <{}> to <{}>", endpoint, registryURI); final HttpPatch patch = new HttpPatch(registryURI); patch.setHeader(HttpHeaders.CONTENT_TYPE, SPARQL_UPDATE); patch.setEntity( new StringEntity(String.format("INSERT {?instance <%s> <%s> .} WHERE {?instance a <%s> .}", PROP_HAS_ENDPOINT, endpoint, CLASS_SERVICE_INSTANCE), UTF_8)); try (CloseableHttpResponse resp = execute(patch)) { LOG.info("Adding endpoint <{}> to <{}>", endpoint, registryURI); } catch (final Exception e) { throw new RuntimeException( String.format("Failed adding endpoint <%s> to <%s>", endpoint, registryURI), e); } return registryURI; } }; }
From source file:de.devbliss.apitester.PatcherIntegrationTest.java
private PatchFactory getCustomFactoryWithHeaders() { return new PatchFactory() { public HttpPatch createPatchRequest(URI uri, Object payload) throws IOException { HttpPatch request = new HttpPatch(uri); request.setHeader(HEADER_NAME1, HEADER_VALUE1); request.setHeader(HEADER_NAME2, HEADER_VALUE2); return request; }/* w w w. j a va2s . c om*/ }; }
From source file:com.clickntap.vimeo.Vimeo.java
private VimeoResponse apiRequest(String endpoint, String methodName, Map<String, String> params, File file) throws IOException { CloseableHttpClient client = HttpClientBuilder.create().build(); HttpRequestBase request = null;/* w w w . j a va 2 s . c o m*/ String url = null; if (endpoint.startsWith("http")) { url = endpoint; } else { url = new StringBuffer(VIMEO_SERVER).append(endpoint).toString(); } if (methodName.equals(HttpGet.METHOD_NAME)) { request = new HttpGet(url); } else if (methodName.equals(HttpPost.METHOD_NAME)) { request = new HttpPost(url); } else if (methodName.equals(HttpPut.METHOD_NAME)) { request = new HttpPut(url); } else if (methodName.equals(HttpDelete.METHOD_NAME)) { request = new HttpDelete(url); } else if (methodName.equals(HttpPatch.METHOD_NAME)) { request = new HttpPatch(url); } request.addHeader("Accept", "application/vnd.vimeo.*+json; version=3.2"); request.addHeader("Authorization", new StringBuffer(tokenType).append(" ").append(token).toString()); HttpEntity entity = null; if (params != null) { ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); for (String key : params.keySet()) { postParameters.add(new BasicNameValuePair(key, params.get(key))); } entity = new UrlEncodedFormEntity(postParameters); } else if (file != null) { entity = new FileEntity(file, ContentType.MULTIPART_FORM_DATA); } if (entity != null) { if (request instanceof HttpPost) { ((HttpPost) request).setEntity(entity); } else if (request instanceof HttpPatch) { ((HttpPatch) request).setEntity(entity); } else if (request instanceof HttpPut) { ((HttpPut) request).setEntity(entity); } } CloseableHttpResponse response = client.execute(request); String responseAsString = null; int statusCode = response.getStatusLine().getStatusCode(); if (methodName.equals(HttpPut.METHOD_NAME) || methodName.equals(HttpDelete.METHOD_NAME)) { JSONObject out = new JSONObject(); for (Header header : response.getAllHeaders()) { out.put(header.getName(), header.getValue()); } responseAsString = out.toString(); } else if (statusCode != 204) { ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); responseAsString = out.toString("UTF-8"); out.close(); } JSONObject json = null; try { json = new JSONObject(responseAsString); } catch (Exception e) { json = new JSONObject(); } VimeoResponse vimeoResponse = new VimeoResponse(json, statusCode); response.close(); client.close(); return vimeoResponse; }
From source file:org.apache.camel.component.olingo2.api.impl.Olingo2AppImpl.java
@Override public <T> void patch(Edm edm, String resourcePath, Object data, Olingo2ResponseHandler<T> responseHandler) { final UriInfoWithType uriInfo = parseUri(edm, resourcePath, null); writeContent(edm, new HttpPatch(createUri(resourcePath, null)), uriInfo, data, responseHandler); }
From source file:org.fcrepo.integration.http.api.html.FedoraHtmlResponsesIT.java
License:asdf
private void postSparqlUpdateUsingHttpClient(final String sparql, final String pid) throws IOException { final HttpPatch method = new HttpPatch(serverAddress + pid); method.addHeader("Content-Type", "application/sparql-update"); final BasicHttpEntity entity = new BasicHttpEntity(); entity.setContent(new ByteArrayInputStream(sparql.getBytes())); method.setEntity(entity);/*from w w w. j a v a 2s . c o m*/ final HttpResponse response = client.execute(method); assertEquals("Expected successful response.", 204, response.getStatusLine().getStatusCode()); }
From source file:com.cisco.oss.foundation.http.apache.ApacheHttpClient.java
private org.apache.http.HttpRequest buildHttpUriRequest(HttpRequest request, Joiner joiner, URI requestUri) { org.apache.http.HttpRequest httpRequest; if (autoEncodeUri) { switch (request.getHttpMethod()) { case GET: httpRequest = new HttpGet(requestUri); break; case POST: httpRequest = new HttpPost(requestUri); break; case PUT: httpRequest = new HttpPut(requestUri); break; case DELETE: httpRequest = new HttpDelete(requestUri); break; case HEAD: httpRequest = new HttpHead(requestUri); break; case OPTIONS: httpRequest = new HttpOptions(requestUri); break; case PATCH: httpRequest = new HttpPatch(requestUri); break; default:/*from ww w.j a v a 2s. c om*/ throw new ClientException("You have to one of the REST verbs such as GET, POST etc."); } } else { switch (request.getHttpMethod()) { case POST: case PUT: case DELETE: case PATCH: httpRequest = new BasicHttpEntityEnclosingRequest(request.getHttpMethod().method(), requestUri.toString()); break; default: httpRequest = new BasicHttpRequest(request.getHttpMethod().method(), requestUri.toString()); } } byte[] entity = request.getEntity(); if (entity != null) { if (httpRequest instanceof HttpEntityEnclosingRequest) { HttpEntityEnclosingRequest httpEntityEnclosingRequestBase = (HttpEntityEnclosingRequest) httpRequest; httpEntityEnclosingRequestBase .setEntity(new ByteArrayEntity(entity, ContentType.create(request.getContentType()))); } else { throw new ClientException( "sending content for request type " + request.getHttpMethod() + " is not supported!"); } } else { if (request instanceof ApacheHttpRequest && httpRequest instanceof HttpEntityEnclosingRequest) { HttpEntityEnclosingRequest httpEntityEnclosingRequestBase = (HttpEntityEnclosingRequest) httpRequest; ApacheHttpRequest apacheHttpRequest = (ApacheHttpRequest) request; httpEntityEnclosingRequestBase.setEntity(apacheHttpRequest.getApacheHttpEntity()); } } Map<String, Collection<String>> headers = request.getHeaders(); for (Map.Entry<String, Collection<String>> stringCollectionEntry : headers.entrySet()) { String key = stringCollectionEntry.getKey(); Collection<String> stringCollection = stringCollectionEntry.getValue(); String value = joiner.join(stringCollection); httpRequest.setHeader(key, value); } return httpRequest; }
From source file:org.callimachusproject.test.WebResource.java
public void patch(String type, byte[] body) throws IOException { ByteArrayEntity entity = new ByteArrayEntity(body); entity.setContentType(type);//w w w .ja v a2s . co m HttpPatch req = new HttpPatch(uri); req.setEntity(entity); DefaultHttpClient client = new DefaultHttpClient(); URL url = req.getURI().toURL(); int port = url.getPort(); String host = url.getHost(); PasswordAuthentication passAuth = Authenticator.requestPasswordAuthentication(url.getHost(), InetAddress.getByName(url.getHost()), port, url.getProtocol(), "", "digest", url, RequestorType.SERVER); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(passAuth.getUserName(), new String(passAuth.getPassword())); client.getCredentialsProvider().setCredentials(new AuthScope(host, port), credentials); client.execute(req, new ResponseHandler<Void>() { public Void handleResponse(HttpResponse response) throws ClientProtocolException, IOException { StatusLine status = response.getStatusLine(); int code = status.getStatusCode(); if (code != 200 && code != 204) { Assert.assertEquals(status.getReasonPhrase(), 204, code); } return null; } }); }
From source file:com.uploader.Vimeo.java
private VimeoResponse apiRequest(String endpoint, String methodName, Map<String, String> params, File file) throws IOException { // try(CloseableHttpClient client = HttpClientBuilder.create().build()) { CloseableHttpClient client = HttpClientBuilder.create().build(); // CloseableHttpClient client = HttpClients.createDefault(); System.out.println("Building HTTP Client"); // try {//from w ww . j a v a2 s. c o m // client = // } catch(Exception e) { // System.out.println("Err in CloseableHttpClient"); // } // System.out.println(client); HttpRequestBase request = null; String url = null; if (endpoint.startsWith("http")) { url = endpoint; } else { url = new StringBuffer(VIMEO_SERVER).append(endpoint).toString(); } System.out.println(url); if (methodName.equals(HttpGet.METHOD_NAME)) { request = new HttpGet(url); } else if (methodName.equals(HttpPost.METHOD_NAME)) { request = new HttpPost(url); } else if (methodName.equals(HttpPut.METHOD_NAME)) { request = new HttpPut(url); } else if (methodName.equals(HttpDelete.METHOD_NAME)) { request = new HttpDelete(url); } else if (methodName.equals(HttpPatch.METHOD_NAME)) { request = new HttpPatch(url); } request.addHeader("Accept", "application/vnd.vimeo.*+json; version=3.2"); request.addHeader("Authorization", new StringBuffer(tokenType).append(" ").append(token).toString()); HttpEntity entity = null; if (params != null) { ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>(); for (String key : params.keySet()) { postParameters.add(new BasicNameValuePair(key, params.get(key))); } entity = new UrlEncodedFormEntity(postParameters); } else { if (file != null) { entity = new FileEntity(file, ContentType.MULTIPART_FORM_DATA); } } if (entity != null) { if (request instanceof HttpPost) { ((HttpPost) request).setEntity(entity); } else if (request instanceof HttpPatch) { ((HttpPatch) request).setEntity(entity); } else if (request instanceof HttpPut) { ((HttpPut) request).setEntity(entity); } } CloseableHttpResponse response = client.execute(request); String responseAsString = null; int statusCode = response.getStatusLine().getStatusCode(); if (methodName.equals(HttpPut.METHOD_NAME) || methodName.equals(HttpDelete.METHOD_NAME)) { JSONObject out = new JSONObject(); for (Header header : response.getAllHeaders()) { try { out.put(header.getName(), header.getValue()); } catch (Exception e) { System.out.println("Err in out.put"); } } responseAsString = out.toString(); } else if (statusCode != 204) { ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); responseAsString = out.toString("UTF-8"); out.close(); } JSONObject json = null; try { json = new JSONObject(responseAsString); } catch (Exception e) { json = new JSONObject(); } VimeoResponse vimeoResponse = new VimeoResponse(json, statusCode); response.close(); client.close(); return vimeoResponse; // } catch(IOException e) { // System.out.println("Error Building HTTP Client"); // } }