List of usage examples for org.apache.commons.httpclient.methods PutMethod setRequestEntity
public void setRequestEntity(RequestEntity paramRequestEntity)
From source file:eu.learnpad.cw.rest.internal.DefaultXWikiPackage.java
private void putPage(File indexFile, String spaceName, String pageName) throws XWikiRestException, JAXBException { HttpClient httpClient = new HttpClient(); httpClient.getParams().setAuthenticationPreemptive(true); Credentials defaultcreds = new UsernamePasswordCredentials("Admin", "admin"); httpClient.getState().setCredentials(new AuthScope("localhost", 8080, AuthScope.ANY_REALM), defaultcreds); PutMethod putMethod = new PutMethod( "http://localhost:8080/xwiki/rest/wikis/xwiki/spaces/" + spaceName + "/pages/" + pageName); putMethod.addRequestHeader("Accept", "application/xml"); putMethod.addRequestHeader("Accept-Ranges", "bytes"); RequestEntity fileRequestEntity = new FileRequestEntity(indexFile, "application/xml"); putMethod.setRequestEntity(fileRequestEntity); try {/* w w w . j av a 2 s.c o m*/ httpClient.executeMethod(putMethod); } catch (HttpException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:eu.learnpad.core.rest.XWikiRestUtils.java
public boolean putPage(String wikiName, String spaceName, String pageName, InputStream pageXML) { HttpClient httpClient = restResource.getClient(); String uri = String.format("%s/wikis/%s/spaces/%s/pages/%s", DefaultRestResource.REST_URI, wikiName, spaceName, pageName);/* w ww. j av a 2 s .c o m*/ PutMethod putMethod = new PutMethod(uri); putMethod.addRequestHeader("Accept", "application/xml"); putMethod.addRequestHeader("Accept-Ranges", "bytes"); RequestEntity pageRequestEntity = new InputStreamRequestEntity(pageXML, "application/xml"); putMethod.setRequestEntity(pageRequestEntity); try { httpClient.executeMethod(putMethod); return true; } catch (HttpException e) { String message = String.format("Unable to process the PUT request on page '%s:%s.%s'.", wikiName, spaceName, pageName); logger.error(message, e); return false; } catch (IOException e) { String message = String.format("Unable to PUT the page '%s:%s.%s'.", wikiName, spaceName, pageName); logger.error(message, e); return false; } }
From source file:com.atlassian.jira.web.action.setup.SetupProductBundleHelper.java
public boolean saveLicense() { int status = 0; final String licenseUrl; final String licenseKey = sharedVariables.getBundleLicenseKey(); if (licenseKey == null) { sharedVariables.setBundleHasLicenseError(true); return false; }//from w ww .ja v a 2 s .c om try { licenseUrl = getLicenseUrl(); } catch (final IllegalStateException e) { sharedVariables.setBundleHasLicenseError(true); return false; } final HttpClient httpClient = prepareClient(); final PutMethod method = new PutMethod(licenseUrl); try { final StringRequestEntity requestEntity = new StringRequestEntity( prepareJSON(ImmutableMap.of("rawLicense", licenseKey)), "application/vnd.atl.plugins+json", "UTF-8"); method.setRequestEntity(requestEntity); status = httpClient.executeMethod(method); } catch (final IOException e) { log.warn("Problem with saving licence during product bundle license installation", e); } finally { method.releaseConnection(); } if (status != 200) { log.warn("Problem with saving licence during product bundle license installation, status code: " + status); sharedVariables.setBundleHasLicenseError(true); return false; } return true; }
From source file:eu.learnpad.core.rest.XWikiRestUtils.java
public boolean putAttachment(String wikiName, String spaceName, String pageName, String attachmentName, InputStream attachment) { HttpClient httpClient = restResource.getClient(); String uri = String.format("%s/wikis/%s/spaces/%s/pages/%s/attachments/%s", DefaultRestResource.REST_URI, wikiName, spaceName, pageName, attachmentName); PutMethod putMethod = new PutMethod(uri); putMethod.addRequestHeader("Accept", "application/xml"); putMethod.addRequestHeader("Accept-Ranges", "bytes"); RequestEntity fileRequestEntity = new InputStreamRequestEntity(attachment, "application/xml"); putMethod.setRequestEntity(fileRequestEntity); try {/*from w ww . ja v a 2 s . c o m*/ httpClient.executeMethod(putMethod); return true; } catch (HttpException e) { String message = String.format("Unable to process PUT request for the attachment '%s:%s.%s@%s'.", wikiName, spaceName, pageName, attachmentName); logger.error(message, e); return false; } catch (IOException e) { String message = String.format("Unable to PUT the attachment '%s:%s.%s@%s'.", wikiName, spaceName, pageName, attachmentName); logger.error(message, e); return false; } }
From source file:net.sf.sail.webapp.domain.webservice.http.impl.HttpRestTransportImpl.java
/** * @see net.sf.sail.webapp.domain.webservice.http.HttpRestTransport#put(net.sf.sail.webapp.domain.webservice.http.HttpPutRequest) *//*from w ww . ja v a 2s . com*/ public Map<String, String> put(final HttpPutRequest httpPutRequestData) throws HttpStatusCodeException { final PutMethod method = new PutMethod(this.baseUrl + httpPutRequestData.getRelativeUrl()); this.setHeaders(httpPutRequestData, method); // set body data final String bodyData = httpPutRequestData.getBodyData(); if (StringUtils.hasText(bodyData)) { method.setRequestEntity(new StringRequestEntity(bodyData)); } final Map<String, String> responseHeaders = new HashMap<String, String>(); try { // Execute the method. logRequest(method, bodyData); final int statusCode = this.client.executeMethod(method); httpPutRequestData.isValidResponseStatus(method, statusCode); final Header[] headers = method.getResponseHeaders(); for (int i = 0; i < headers.length; i++) { responseHeaders.put(headers[i].getName(), headers[i].getValue()); } } catch (HttpException e) { logAndThrowRuntimeException(e); } catch (IOException e) { logAndThrowRuntimeException(e); } finally { method.releaseConnection(); } return responseHeaders; }
From source file:eu.learnpad.core.impl.qm.XwikiCoreFacadeRestResource.java
@Override public void publish(String questionnairesId, String type, byte[] questionnairesFile) throws LpRestException { // Now actually notifying the CP via REST HttpClient httpClient = this.getClient(); String uri = String.format("%s/learnpad/qm/corefacade/publish/%s", DefaultRestResource.REST_URI, questionnairesId);//from w w w.ja v a2s . com PutMethod putMethod = new PutMethod(uri); putMethod.addRequestHeader("Content-Type", MediaType.APPLICATION_OCTET_STREAM); NameValuePair[] queryString = new NameValuePair[1]; queryString[0] = new NameValuePair("type", type); putMethod.setQueryString(queryString); InputStream stream = new ByteArrayInputStream(questionnairesFile); RequestEntity requestEntity = new InputStreamRequestEntity(stream); putMethod.setRequestEntity(requestEntity); try { httpClient.executeMethod(putMethod); } catch (IOException e) { LpRestExceptionXWikiImpl e1 = new LpRestExceptionXWikiImpl(e.getMessage(), e.getCause()); throw e1; } }
From source file:com.cloud.utils.rest.RESTServiceConnector.java
public <T> void executeUpdateObject(final T newObject, final String uri, final Map<String, String> parameters) throws CloudstackRESTException { final PutMethod pm = (PutMethod) createMethod(PUT_METHOD_TYPE, uri); pm.setRequestHeader(CONTENT_TYPE, JSON_CONTENT_TYPE); try {/*from w ww. j a va2 s .c o m*/ pm.setRequestEntity(new StringRequestEntity(gson.toJson(newObject), JSON_CONTENT_TYPE, null)); } catch (final UnsupportedEncodingException e) { throw new CloudstackRESTException("Failed to encode json request body", e); } executeMethod(pm); if (pm.getStatusCode() != HttpStatus.SC_OK) { final String errorMessage = responseToErrorMessage(pm); pm.releaseConnection(); s_logger.error("Failed to update object : " + errorMessage); throw new CloudstackRESTException("Failed to update object : " + errorMessage); } pm.releaseConnection(); }
From source file:com.sa.npopa.samples.hbase.rest.client.Client.java
/** * Send a PUT request/* w w w. ja v a 2 s . c o m*/ * @param cluster the cluster definition * @param path the path or URI * @param headers the HTTP headers to include, <tt>Content-Type</tt> must be * supplied * @param content the content bytes * @return a Response object with response detail * @throws IOException */ public Response put(Cluster cluster, String path, Header[] headers, byte[] content) throws IOException { PutMethod method = new PutMethod(); try { method.setRequestEntity(new ByteArrayRequestEntity(content)); int code = execute(cluster, method, headers, path); headers = method.getResponseHeaders(); content = method.getResponseBody(); return new Response(code, headers, content); } finally { method.releaseConnection(); } }
From source file:eu.learnpad.core.impl.dash.XwikiBridgeInterfaceRestResource.java
@Override public void loadKPIValues(String modelSetId, KPIValuesFormat format, String businessActorId, InputStream cockpitContent) throws LpRestException { String contentType = "application/xml"; HttpClient httpClient = this.getAnonymousClient(); String uri = String.format("%s/learnpad/dash/bridge/loadkpivalues/%s", this.restPrefix, modelSetId); PutMethod putMethod = new PutMethod(uri); putMethod.addRequestHeader("Content-Type", contentType); NameValuePair[] queryString = new NameValuePair[2]; queryString[0] = new NameValuePair("format", format.toString()); queryString[1] = new NameValuePair("businessactor", businessActorId); putMethod.setQueryString(queryString); RequestEntity requestEntity = new InputStreamRequestEntity(cockpitContent); putMethod.setRequestEntity(requestEntity); try {/* www .j a v a2 s .c o m*/ httpClient.executeMethod(putMethod); } catch (IOException e) { throw new LpRestExceptionXWikiImpl(e.getMessage(), e); } }
From source file:com.cloud.network.bigswitch.BigSwitchBcfApi.java
protected <T> String executeUpdateObject(final T newObject, final String uri, final Map<String, String> parameters) throws BigSwitchBcfApiException, IllegalArgumentException { checkInvariants();/*from w w w.j a v a2 s.c o m*/ PutMethod pm = (PutMethod) createMethod("put", uri, _port); setHttpHeader(pm); try { pm.setRequestEntity(new StringRequestEntity(gson.toJson(newObject), CONTENT_JSON, null)); } catch (UnsupportedEncodingException e) { throw new BigSwitchBcfApiException("Failed to encode json request body", e); } executeMethod(pm); String hash = checkResponse(pm, "BigSwitch HTTP update failed: "); pm.releaseConnection(); return hash; }