/** * Factory method to create a {@link HttpMethod}-object according to the * given String <code>httpMethod</codde> * * @param httpMethodString the name of the {@link HttpMethod} to create * @param url * * @return an object of type {@link GetMethod}, {@link PutMethod}, * {@link PostMethod} or {@link DeleteMethod} * @throws IllegalArgumentException if <code>httpMethod</code> is none of * <code>GET</code>, <code>PUT</code>, <code>POST</POST> or <code>DELETE</code> */ public static HttpMethod createHttpMethod(String httpMethodString, String url) { if ("GET".equals(httpMethodString)) { return new GetMethod(url); } else if ("PUT".equals(httpMethodString)) { return new PutMethod(url); } else if ("POST".equals(httpMethodString)) { return new PostMethod(url); } else if ("DELETE".equals(httpMethodString)) { return new DeleteMethod(url); } else { throw new IllegalArgumentException("given httpMethod '" + httpMethodString + "' is unknown"); } }
HttpMethod firstPutReq = new PutMethod(firstPutReqString); try { LOGGER.info("Trying to send request: {}.", firstPutReqString); PutMethod redirectedReq = new PutMethod(redirectedReqString); File localFile = new File(localFilePath); RequestEntity requestEntity = new FileRequestEntity(localFile, "application/binary");
public static PutMethod sendMultipartPutRequest(String url, String body) throws IOException { HttpClient httpClient = new HttpClient(); PutMethod putMethod = new PutMethod(url); // our handlers ignore key...so we can put anything here Part[] parts = {new StringPart("body", body)}; putMethod.setRequestEntity(new MultipartRequestEntity(parts, putMethod.getParams())); httpClient.executeMethod(putMethod); return putMethod; } }
break; case PUT: http = new PutMethod(); break;
public void wipeCache(String type, String action, String name) throws IOException { String url = baseUrl + "/cache/" + type + "/" + name + "/" + action; HttpMethod request = new PutMethod(url); try { int code = client.executeMethod(request); String msg = Bytes.toString(request.getResponseBody()); if (code != 200) throw new IOException("Invalid response " + code + " with cache wipe url " + url + "\n" + msg); } catch (HttpException ex) { throw new IOException(ex); } finally { request.releaseConnection(); } }
protected HttpMethod createMethod(final String type, final String uri, final int port) throws BigSwitchBcfApiException { String url; try { url = new URL(S_PROTOCOL, host, port, uri).toString(); } catch (MalformedURLException e) { S_LOGGER.error("Unable to build Big Switch API URL", e); throw new BigSwitchBcfApiException("Unable to build Big Switch API URL", e); } if ("post".equalsIgnoreCase(type)) { return new PostMethod(url); } else if ("get".equalsIgnoreCase(type)) { return new GetMethod(url); } else if ("delete".equalsIgnoreCase(type)) { return new DeleteMethod(url); } else if ("put".equalsIgnoreCase(type)) { return new PutMethod(url); } else { throw new BigSwitchBcfApiException("Requesting unknown method type"); } }
httpMethod = new PostMethod(); } else if (method == HttpMethod.PUT) { return new PutMethod(); } else if (method == HttpMethod.DELETE) { httpMethod = new DeleteMethod();
@Ignore @Test public void testRestAPI() throws IOException { HttpClient client = new HttpClient(); // To be avoided unless in debug mode Credentials defaultcreds = new UsernamePasswordCredentials("admin", "111111"); client.getParams().setAuthenticationPreemptive(true); client.getState().setCredentials(AuthScope.ANY, defaultcreds); PutMethod method = new PutMethod("http://localhost:8080/agent/api/36"); final HttpMethodParams params = new HttpMethodParams(); params.setParameter("action", "approve"); method.setParams(params); final int i = client.executeMethod(method); System.out.println(method.getResponseBodyAsString()); } }
/** * Performs a PUT to the given URL. * * @param url The URL where to connect to. * @param requestEntity The request to be sent. * @return The HTTP response as a String if the HTTP response code was 200 (OK). * @throws MalformedURLException * @return the HTTP response or <TT>null</TT> on errors. */ public String put(String url, RequestEntity requestEntity) { return send(new PutMethod(url), url, requestEntity); }
PutMethod put = new PutMethod("http://jakarta.apache.org"); put.setRequestBody(new FileInputStream("UploadMe.gif")); // execute the method and handle any error responses. ... // Handle the response. Note that a successful response may not be // 200, but may also be 201 Created, 204 No Content or any of the other // 2xx range responses.
protected PutMethod executePut(String uri, InputStream content, String mediaType, int... expectedCodes) throws Exception { PutMethod putMethod = new PutMethod(uri); RequestEntity entity = new InputStreamRequestEntity(content, mediaType); putMethod.setRequestEntity(entity); int code = this.adminHTTPClient.executeMethod(putMethod); if (!ArrayUtils.contains(expectedCodes, code)) { throw new Exception("Failed to execute put [" + uri + "] with code [" + code + "]"); } return putMethod; } }
/** Upload a file to the Sling repository * @return the HTTP status code */ public int upload(String toUrl, InputStream is) throws IOException { final PutMethod put = new PutMethod(toUrl); put.setRequestEntity(new InputStreamRequestEntity(is)); return httpClient.executeMethod(put); }
private PutMethod createPutMethod(String uriPrefix) { putMethod = new PutMethod(uriPrefix); putMethod.setRequestEntity(entity); if (cancellationRequested.get()) { putMethod.abort(); // next method will throw an exception } return putMethod; } }
/** * @since 7.3M1 */ protected PutMethod executePut(String uri, InputStream content, String mediaType) throws Exception { PutMethod putMethod = new PutMethod(uri); RequestEntity entity = new InputStreamRequestEntity(content, mediaType); putMethod.setRequestEntity(entity); this.httpClient.executeMethod(putMethod); return putMethod; }
protected HttpMethod createPutMethod(MuleMessage msg, String outputEncoding) throws Exception { URI uri = getURI(msg); PutMethod putMethod = new PutMethod(uri.toString()); Object payload = msg.getPayload(); setupEntityMethod(payload, outputEncoding, msg, putMethod); checkForContentType(msg, putMethod); return putMethod; }
private HttpMethodBase newPutRequest(UpdateQuery<?> query) { PutMethod method = new PutMethod(server.getHost() + query.getUrl()); initRequest(method, query); setRequestEntity(method, query); return method; }
private void writeCurrencyViaJson(String key, String description, int rank) throws IOException { EntityEnclosingMethod put = new PutMethod(restEndpoint + "/" + key); ObjectNode currency = MAPPER.createObjectNode(); currency.put(TYPE, getEntityName()); currency.put("description", description); currency.put("rank", rank); put.setRequestEntity(new StringRequestEntity(currency.toString(), "application/json", "UTF-8")); restClient.executeMethod(put); System.out.println(put.getResponseBodyAsString()); assertEquals(put.getStatusCode(), HttpStatus.SC_OK); }
protected void put(String path, NameValuePair[] data) { PutMethod req = new PutMethod(path); req.setRequestBody(EncodingUtil.formUrlEncode(data, "utf-8")); req.getParams().setContentCharset("utf-8"); send(req); }
@Test public void testShouldExecutePutMethod() throws IOException { when(mockHttpClientAdaptor.executeMethod(any(PutMethod.class))) .thenReturn(HttpStatus.SC_OK); when(mockHttpMethodProvider.putMethod()).thenReturn(new PutMethod()); HttpMethodExecutor httpMethodExecutor = new HttpMethodExecutor( mockHttpClientAdaptor, mockHttpMethodProvider, mockCache, mockCacheManager); httpMethodExecutor.put(CONTENT, URL, CONTENT_TYPE); }
public void upload(File src, URL dest, CopyProgressListener l) throws IOException { HttpClient client = getClient(); PutMethod put = new PutMethod(normalizeToString(dest)); put.setDoAuthentication(useAuthentication(dest) || useProxyAuthentication()); put.getParams().setBooleanParameter("http.protocol.expect-continue", true); try { put.setRequestEntity(new FileRequestEntity(src)); int statusCode = client.executeMethod(put); validatePutStatusCode(dest, statusCode, null); } finally { put.releaseConnection(); } }