List of usage examples for org.apache.commons.httpclient.methods StringRequestEntity StringRequestEntity
public StringRequestEntity(String paramString1, String paramString2, String paramString3) throws UnsupportedEncodingException
From source file:net.jadler.JadlerMockingIntegrationTest.java
@Test public void havingEmptyBody() throws Exception { final PostMethod method = new PostMethod("http://localhost:" + port()); method.setRequestEntity(new StringRequestEntity("", null, null)); this.client.executeMethod(method); verifyThatRequest().havingBodyEqualTo("").havingBody(notNullValue()).havingBody(isEmptyString()) .receivedTimes(1);/*from w ww .j a va 2 s. c om*/ }
From source file:edu.virginia.speclab.juxta.author.view.export.WebServiceClient.java
/** * Create a juxta user account. This will throw if create request failed. * /*from w ww . ja va 2s. co m*/ * @param email * @param pass * @param confirm * @param confirm2 * @throws IOException */ public void createAccount(final String name, final String email, final String pass, final String confirm) throws IOException { PostMethod post = new PostMethod(this.baseUrl + "/login/create"); String json = "{\"user\": {" + "\"name\": \"" + name + "\", \"email\": \"" + email + "\", \"password\": \"" + pass + "\", \"password_confirmation\": \"" + confirm + "\"} }"; post.setRequestEntity(new StringRequestEntity(json, "application/json", "utf-8")); post.setRequestHeader("accept", "application/json"); int respCode = execRequest(post, false); if (respCode != 200) { final String msg = getResponseString(post); post.releaseConnection(); throw new IOException("Unable to create account: " + msg); } String out = getResponseString(post); System.out.println(out); post.releaseConnection(); }
From source file:de.bermuda.arquillian.example.ContentTypeProxyTest.java
private PostMethod postRequest(String body, String localPath) { HttpClient client = new HttpClient(); PostMethod postMethod = new PostMethod(BASE_URL + "/proxy" + localPath); try {/*from w w w .j a va 2 s.c o m*/ StringRequestEntity entity = new StringRequestEntity(body, "application/soap+xml", "UTF-8"); postMethod.setRequestEntity(entity); } catch (IOException e) { Assert.assertTrue("Catched IOException", false); } try { int status = client.executeMethod(postMethod); Assert.assertEquals("Expected OK", status, HttpStatus.SC_OK); } catch (HttpException e) { Assert.assertFalse("HttpException", true); } catch (IOException e) { Assert.assertFalse("IOException", true); } return postMethod; }
From source file:com.assemblade.client.AbstractClient.java
protected <T> T add(String path, T object, TypeReference<T> type) throws ClientException { PostMethod post = new PostMethod(baseUrl + path); try {/* ww w. j av a 2 s . com*/ try { post.setRequestEntity( new StringRequestEntity(mapper.writeValueAsString(object), "application/json", null)); } catch (IOException e) { throw new CallFailedException("Failed to serialize a request object", e); } int statusCode = executeMethod(post); if (statusCode == 200) { try { return mapper.readValue(post.getResponseBodyAsStream(), type); } catch (IOException e) { throw new CallFailedException("Failed to deserialize a response object", e); } } else { throw new InvalidStatusException(200, statusCode); } } finally { post.releaseConnection(); } }
From source file:eu.learnpad.core.impl.ca.XwikiBridgeInterfaceRestResource.java
@Override public String putValidateCollaborativeContent(CollaborativeContentAnalysis contentFile) throws LpRestException { HttpClient httpClient = this.getAnonymousClient(); String uri = String.format("%s/learnpad/ca/bridge/validatecollaborativecontent", this.restPrefix); PostMethod postMethod = new PostMethod(uri); postMethod.addRequestHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_XML); try {//from ww w .java 2s .c o m StringWriter contentWriter = new StringWriter(); JAXBContext context = JAXBContext.newInstance(CollaborativeContentAnalysis.class); Marshaller marshaller = context.createMarshaller(); marshaller.marshal(contentFile, contentWriter); RequestEntity entity = new StringRequestEntity(contentWriter.toString(), MediaType.APPLICATION_XML, null); postMethod.setRequestEntity(entity); } catch (JAXBException | UnsupportedEncodingException e) { throw new LpRestExceptionXWikiImpl(e.getMessage(), e.getCause()); } try { httpClient.executeMethod(postMethod); return postMethod.getResponseBodyAsString(); } catch (IOException e) { throw new LpRestExceptionXWikiImpl(e.getMessage(), e.getCause()); } }
From source file:edu.berkeley.ground.plugins.hive.util.PluginUtil.java
public static StringRequestEntity createRequestEntity(String jsonRecord) throws UnsupportedEncodingException { StringRequestEntity requestEntity = new StringRequestEntity(jsonRecord, "application/json", "UTF-8"); return requestEntity; }
From source file:com.urswolfer.intellij.plugin.gerrit.rest.GerritApiUtil.java
@NotNull private static HttpMethod doREST(@NotNull String host, @Nullable String login, @Nullable String password, @NotNull String path, @Nullable final String requestBody, final boolean post) throws IOException { HttpClient client = getHttpClient(login, password); String uri = host + path;// www . j a va2 s.c om return SslSupport.getInstance().executeSelfSignedCertificateAwareRequest(client, uri, new ThrowableConvertor<String, HttpMethod, IOException>() { @Override public HttpMethod convert(String uri) throws IOException { if (post) { PostMethod method = new PostMethod(uri); if (requestBody != null) { method.setRequestEntity( new StringRequestEntity(requestBody, "application/json", "UTF-8")); } return method; } return new GetMethod(uri); } }); }
From source file:com.boundary.sdk.event.notification.WebhookNotificationTest.java
@Test public void testNotification() throws InterruptedException, IOException { String body = readFile(NOTIFICATION_JSON, Charset.defaultCharset()); out.setExpectedMessageCount(1);//from w w w . j av a 2 s .com CamelContext context = context(); RouteDefinition routeDefinition = context.getRouteDefinition("WEBHOOK-TEST"); assertNotNull("RouteDefinition is null", routeDefinition); List<FromDefinition> inputs = routeDefinition.getInputs(); FromDefinition from = inputs.get(0); String uri = from.getEndpointUri(); uri = uri.replaceFirst("jetty:", ""); LOG.debug("uri: {}", uri); // Send HTTP notification HttpClient httpclient = new HttpClient(); PostMethod httppost = new PostMethod(uri); Header contentHeader = new Header("Content-Type", "application/json"); httppost.setRequestHeader(contentHeader); StringRequestEntity reqEntity = new StringRequestEntity(body, null, null); httppost.setRequestEntity(reqEntity); int status = httpclient.executeMethod(httppost); assertEquals("Received wrong response status", 200, status); out.assertIsSatisfied(); List<Exchange> exchanges = out.getExchanges(); LOG.debug("EXCHANGE COUNT: {}", exchanges.size()); for (Exchange exchange : exchanges) { Message message = exchange.getIn(); String messageBody = message.getBody(String.class); Object o = message.getBody(); LOG.debug("class: " + o.getClass().toString()); LOG.debug("messageBody: " + messageBody); LOG.debug("id: " + exchange.getExchangeId()); //assertEquals("Body not equal",body,messageBody); } }
From source file:de.bermuda.arquillian.example.ContentTypeProxyServlet.java
private void copyRequestBody(HttpServletRequest req, PostMethod post) throws IOException { BufferedReader contentReader = req.getReader(); StringBuilder content = new StringBuilder(); String contentLine = ""; while (contentLine != null) { content.append(contentLine);/*from www . j av a 2 s . c o m*/ contentLine = contentReader.readLine(); } StringRequestEntity requestEntity = new StringRequestEntity(content.toString(), req.getContentType(), req.getCharacterEncoding()); post.setRequestEntity(requestEntity); requestLogger.info("RequestBody: " + content); }
From source file:com.calclab.emite.xtesting.services.HttpConnector.java
private Runnable createSendAction(final String httpBase, final String xml, final ConnectorCallback callback) { return new Runnable() { public void run() { final String id = HttpConnectorID.getNext(); debug("Connector [{0}] send: {1}", id, xml); final HttpClientParams params = new HttpClientParams(); params.setConnectionManagerTimeout(10000); final HttpClient client = new HttpClient(params); int status = 0; String response = null; final PostMethod post = new PostMethod(httpBase); try { post.setRequestEntity(new StringRequestEntity(xml, "text/xml", "utf-8")); System.out.println("SENDING: " + xml); status = client.executeMethod(post); response = post.getResponseBodyAsString(); } catch (final Exception e) { callback.onError(xml, e); e.printStackTrace();//from w w w . ja v a 2 s. com } finally { post.releaseConnection(); } receiveService.execute(createResponseAction(xml, callback, id, status, response)); } }; }