List of usage examples for org.apache.commons.httpclient.methods StringRequestEntity StringRequestEntity
public StringRequestEntity(String paramString)
From source file:de.mpg.escidoc.services.edoc.PubManImport.java
private String assignObjectPid(String id, String lastModificationDate) throws Exception { HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod(CORESERVICES_URL + "/ir/item/" + id + "/assign-object-pid"); postMethod.addRequestHeader("Cookie", "escidocCookie=" + userHandle); String body = "<param last-modification-date=\"" + lastModificationDate + "\"><url>http://localhost:8080/album/core/ir/item/" + id + "</url></param>"; postMethod.setRequestEntity(new StringRequestEntity(body)); ProxyHelper.executeMethod(httpClient, postMethod); String response = postMethod.getResponseBodyAsString(); if (postMethod.getStatusCode() != 200) { throw new RuntimeException(response); }//from w w w .j av a 2 s . c om return response; }
From source file:com.cloudfoundry.bae.cloudpush.RequestCore.java
private void executeRequest() { HttpClient client = new HttpClient(); HttpConnectionManagerParams hcManagerParams = client.getHttpConnectionManager().getParams(); hcManagerParams.setConnectionTimeout(connectionOption.connectionTimeout); hcManagerParams.setSoTimeout(connectionOption.optTimeout); PostMethod post = new PostMethod(requestUrl); if (requestHeaders != null && !requestHeaders.isEmpty()) { Set<String> headerKeySet = requestHeaders.keySet(); for (String headerKey : headerKeySet) { String headerValue = requestHeaders.get(headerKey); post.addRequestHeader(headerKey, headerValue); }/*from w w w . j a v a2 s. c o m*/ } post.setRequestEntity(new StringRequestEntity(requestBody)); try { responseCode = client.executeMethod(post); responseBody = post.getResponseBodyAsString(); logger.info(responseBody); Header[] rspHeaders = post.getResponseHeaders(); responseHeaders = new HashMap<String, String>(); for (Header rspHeader : rspHeaders) { responseHeaders.put(rspHeader.getName(), rspHeader.getValue()); } } catch (IOException ex) { logger.error(ex); } }
From source file:de.mpg.escidoc.services.edoc.PubManImport.java
private String assignVersionPid(String id, String lastModificationDate) throws Exception { HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod(CORESERVICES_URL + "/ir/item/" + id + "/assign-version-pid"); postMethod.addRequestHeader("Cookie", "escidocCookie=" + userHandle); String body = "<param last-modification-date=\"" + lastModificationDate + "\"><url>http://qa-pubman.mpdl.mpg.de:8080/faces/item/" + id + "</url></param>"; postMethod.setRequestEntity(new StringRequestEntity(body)); ProxyHelper.executeMethod(httpClient, postMethod); String response = postMethod.getResponseBodyAsString(); if (postMethod.getStatusCode() != 200) { throw new RuntimeException(response); }//from w w w. j a v a2s.c o m return response; }
From source file:com.sun.syndication.propono.atom.client.ClientEntry.java
void addToCollection(ClientCollection col) throws ProponoException { setCollection(col);/*from ww w . j av a 2s .c o m*/ EntityEnclosingMethod method = new PostMethod(getCollection().getHrefResolved()); addAuthentication(method); StringWriter sw = new StringWriter(); int code = -1; try { Atom10Generator.serializeEntry(this, sw); method.setRequestEntity(new StringRequestEntity(sw.toString())); method.setRequestHeader("Content-type", "application/atom+xml; charset=utf-8"); getHttpClient().executeMethod(method); InputStream is = method.getResponseBodyAsStream(); code = method.getStatusCode(); if (code != 200 && code != 201) { throw new ProponoException("ERROR HTTP status=" + code + " : " + Utilities.streamToString(is)); } Entry romeEntry = Atom10Parser.parseEntry(new InputStreamReader(is), getCollection().getHrefResolved()); BeanUtils.copyProperties(this, romeEntry); } catch (Exception e) { String msg = "ERROR: saving entry, HTTP code: " + code; logger.debug(msg, e); throw new ProponoException(msg, e); } finally { method.releaseConnection(); } Header locationHeader = method.getResponseHeader("Location"); if (locationHeader == null) { logger.warn("WARNING added entry, but no location header returned"); } else if (getEditURI() == null) { List links = getOtherLinks(); Link link = new Link(); link.setHref(locationHeader.getValue()); link.setRel("edit"); links.add(link); setOtherLinks(links); } }
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 w w. ja va2s. c o m*/ 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:com.cloud.baremetal.networkservice.SecurityGroupHttpClient.java
public SecurityGroupRuleAnswer call(String agentIp, SecurityGroupRulesCmd cmd) { PostMethod post = new PostMethod(String.format("http://%s:%s", agentIp, getPort())); try {/* w ww . j a va 2s .c o m*/ SecurityGroupVmRuleSet rset = new SecurityGroupVmRuleSet(); rset.getEgressRules().addAll(generateRules(cmd.getEgressRuleSet())); rset.getIngressRules().addAll(generateRules(cmd.getIngressRuleSet())); rset.setVmName(cmd.getVmName()); rset.setVmIp(cmd.getGuestIp()); rset.setVmMac(cmd.getGuestMac()); rset.setVmId(cmd.getVmId()); rset.setSignature(cmd.getSignature()); rset.setSequenceNumber(cmd.getSeqNum()); Marshaller marshaller = context.createMarshaller(); StringWriter writer = new StringWriter(); marshaller.marshal(rset, writer); String xmlContents = writer.toString(); logger.debug(xmlContents); post.addRequestHeader("command", "set_rules"); StringRequestEntity entity = new StringRequestEntity(xmlContents); post.setRequestEntity(entity); if (httpClient.executeMethod(post) != 200) { return new SecurityGroupRuleAnswer(cmd, false, post.getResponseBodyAsString()); } else { return new SecurityGroupRuleAnswer(cmd); } } catch (Exception e) { return new SecurityGroupRuleAnswer(cmd, false, e.getMessage()); } finally { if (post != null) { post.releaseConnection(); } } }
From source file:com.buglabs.dragonfly.util.WSHelper.java
/** * Refer to/* www . ja v a 2s .c o m*/ * http://lurcher/wiki/index.php/IDE_Web_Service_Interface#Using_a_Token * * @param url * @param token * @param payload * @return * @throws IOException */ protected static String post(String url, String token, String payload) throws IOException { HttpClient c = new HttpClient(); PostMethod m = new PostMethod(url); m.setRequestHeader("Cookie", "token=" + token); m.setRequestEntity(new StringRequestEntity(payload)); c.executeMethod(m); return m.getResponseBodyAsString(); }
From source file:com.sun.syndication.propono.atom.client.ClientMediaEntry.java
/** * Update entry on server. //from w w w . j a va 2 s . co m */ public void update() throws ProponoException { if (partial) throw new ProponoException("ERROR: attempt to update partial entry"); EntityEnclosingMethod method = null; Content updateContent = (Content) getContents().get(0); try { if (getMediaLinkURI() != null && getBytes() != null) { // existing media entry and new file, so PUT file to edit-media URI method = new PutMethod(getMediaLinkURI()); if (inputStream != null) { method.setRequestEntity(new InputStreamRequestEntity(inputStream)); } else { method.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(getBytes()))); } method.setRequestHeader("Content-type", updateContent.getType()); } else if (getEditURI() != null) { // existing media entry and NO new file, so PUT entry to edit URI method = new PutMethod(getEditURI()); StringWriter sw = new StringWriter(); Atom10Generator.serializeEntry(this, sw); method.setRequestEntity(new StringRequestEntity(sw.toString())); method.setRequestHeader("Content-type", "application/atom+xml; charset=utf8"); } else { throw new ProponoException("ERROR: media entry has no edit URI or media-link URI"); } this.getCollection().addAuthentication(method); method.addRequestHeader("Title", getTitle()); getCollection().getHttpClient().executeMethod(method); if (inputStream != null) inputStream.close(); InputStream is = method.getResponseBodyAsStream(); if (method.getStatusCode() != 200 && method.getStatusCode() != 201) { throw new ProponoException( "ERROR HTTP status=" + method.getStatusCode() + " : " + Utilities.streamToString(is)); } } catch (Exception e) { throw new ProponoException("ERROR: saving media entry"); } if (method.getStatusCode() != 201) { throw new ProponoException("ERROR HTTP status=" + method.getStatusCode()); } }
From source file:com.funambol.json.dao.JsonDAOImpl.java
public JsonResponse addItem(String token, String jsonObject, long since) throws HttpException, IOException { String request = Utility.getUrl(jsonServerUrl, resourceType, ADD_ITEM_URL); if (log.isTraceEnabled()) { log.trace("JsonDAOImpl: start addItem; since=" + since); }// ww w . j ava 2s .c o m PostMethod post = new PostMethod(request); post.setRequestHeader(Utility.TOKEN_HEADER_NAME, token); post.setRequestEntity(new StringRequestEntity(jsonObject)); if (since != 0) { NameValuePair nvp_since = new NameValuePair(); nvp_since.setName("since"); nvp_since.setValue("" + since); NameValuePair[] nvp = { nvp_since }; post.setQueryString(nvp); } if (log.isTraceEnabled()) { log.trace("addItem Request: " + request); } if (Configuration.getConfiguration().isDebugMode()) { if (log.isTraceEnabled()) { log.trace("JSON to add " + jsonObject); } } int statusCode = 0; String responseBody = null; try { statusCode = httpClient.executeMethod(post); responseBody = post.getResponseBodyAsString(); } finally { post.releaseConnection(); } if (log.isTraceEnabled()) { log.trace("JsonDAOImpl: statusCode " + statusCode + "; added item" + responseBody); log.trace("JsonDAOImpl: item added"); } JsonResponse response = new JsonResponse(statusCode, responseBody); return response; }
From source file:com.gisgraphy.rest.RestClient.java
private HttpMethod createPutMethod(String url, Map<String, Object> map) throws RestClientException { PutMethod httpMethod = new PutMethod(url); StringBuilder responseBody = new StringBuilder(); if (map != null) { for (String key : map.keySet()) { if (map.get(key) != null) { responseBody.append(format("%s=%s\n", key, map.get(key).toString())); }/*from w w w . j a v a2 s . co m*/ } } String responseBodyAsString = responseBody.toString(); httpMethod.setRequestEntity( new StringRequestEntity(responseBodyAsString.substring(0, responseBody.length()))); httpMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); return httpMethod; }