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:muki.tool.JavaCompilationDeploymentTestCase.java
@Test public void testPostOperation7Json() throws Exception { String url = URL_RESOURCE1 + "/pathPostOperation7Json"; PostMethod method = new PostMethod(url); String param = this.getJsonCd(); method.setRequestEntity(new StringRequestEntity(param, "application/json", null)); int statusCode = this.getHttpClient().executeMethod(method); assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK); byte[] responseBody = method.getResponseBody(); String jsonResponse = new String(responseBody); assertTrue(jsonResponse.indexOf("\"title\":\"My Track 1\"") > -1); }
From source file:com.wso2telco.openidtokenbuilder.MIFEOpenIDTokenBuilder.java
/** * Retrieve acr.//from w ww . j ava2s.c o m * * @param msisdn the msisdn * @return the string * @throws IdentityOAuth2Exception the identity o auth2 exception */ public String retrieveACR(String msisdn) throws IdentityOAuth2Exception { StringBuilder requestURLBuilder = new StringBuilder(); requestURLBuilder.append(ACR_HOST_URI); requestURLBuilder.append("/"); //$NON-NLS-1$ requestURLBuilder.append(RETRIEVE_SERVICE); requestURLBuilder.append("/"); //$NON-NLS-1$ requestURLBuilder.append(SERVICE_PROVIDER); requestURLBuilder.append("/"); //$NON-NLS-1$ requestURLBuilder.append(acrAppID); String requestURL = requestURLBuilder.toString(); JSONObject requestBody = new JSONObject(); JSONObject retrieveRequest = null; try { requestBody.put("MSISDN", msisdn); //$NON-NLS-1$ retrieveRequest = new JSONObject().put("retriveAcrRequest", requestBody); //$NON-NLS-1$ StringRequestEntity requestEntity = new StringRequestEntity(retrieveRequest.toString(), "application/json", "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$ PostMethod postMethod = new PostMethod(requestURL); postMethod.addRequestHeader(new Header("Authorization-ACR", "ServiceKey " + SERVICE_KEY)); //$NON-NLS-1$ // $NON-NLS-2$ postMethod.addRequestHeader("Authorization", "Bearer " + ACR_ACCESS_TOKEN); //$NON-NLS-1$ //$NON-NLS-2$ postMethod.setRequestEntity(requestEntity); if (DEBUG) { log.debug("Connecting to ACR engine @ " + ACR_HOST_URI); //$NON-NLS-1$ log.debug("Service name : " + RETRIEVE_SERVICE); //$NON-NLS-1$ log.debug("Service provider : " + SERVICE_PROVIDER); //$NON-NLS-1$ log.debug("App key : " + acrAppID); //$NON-NLS-1$ log.debug("Request - retrieve ACR : " + requestEntity.getContent()); //$NON-NLS-1$ } httpClient = new HttpClient(); httpClient.executeMethod(postMethod); return new String(postMethod.getResponseBody(), "UTF-8"); //$NON-NLS-1$ } catch (UnsupportedEncodingException e) { log.error("Error occured while creating request", e); //$NON-NLS-1$ throw new IdentityOAuth2Exception("Error occured while creating request", e); //$NON-NLS-1$ } catch (JSONException e) { log.error("Error occured while creating request", e); //$NON-NLS-1$ throw new IdentityOAuth2Exception("Error occured while creating request", e); //$NON-NLS-1$ } catch (HttpException e) { log.error("Error occured while retrieving ACR", e); //$NON-NLS-1$ throw new IdentityOAuth2Exception("Error occured while retrieving ACR", e); //$NON-NLS-1$ } catch (IOException e) { log.error("Error occured while retrieving ACR", e); //$NON-NLS-1$ throw new IdentityOAuth2Exception("Error occured while retrieving ACR", e); //$NON-NLS-1$ } }
From source file:com.liferay.portal.util.HttpImpl.java
protected byte[] URLtoByteArray(String location, Http.Method method, Map<String, String> headers, Cookie[] cookies, Http.Auth auth, Http.Body body, List<Http.FilePart> fileParts, Map<String, String> parts, Http.Response response, boolean followRedirects) throws IOException { byte[] bytes = null; HttpMethod httpMethod = null;/*from w ww.java2 s .c om*/ HttpState httpState = null; try { _cookies.set(null); if (location == null) { return null; } else if (!location.startsWith(Http.HTTP_WITH_SLASH) && !location.startsWith(Http.HTTPS_WITH_SLASH)) { location = Http.HTTP_WITH_SLASH + location; } HostConfiguration hostConfiguration = getHostConfiguration(location); HttpClient httpClient = getClient(hostConfiguration); if (method.equals(Http.Method.POST) || method.equals(Http.Method.PUT)) { if (method.equals(Http.Method.POST)) { httpMethod = new PostMethod(location); } else { httpMethod = new PutMethod(location); } if (body != null) { RequestEntity requestEntity = new StringRequestEntity(body.getContent(), body.getContentType(), body.getCharset()); EntityEnclosingMethod entityEnclosingMethod = (EntityEnclosingMethod) httpMethod; entityEnclosingMethod.setRequestEntity(requestEntity); } else if (method.equals(Http.Method.POST)) { PostMethod postMethod = (PostMethod) httpMethod; processPostMethod(postMethod, fileParts, parts); } } else if (method.equals(Http.Method.DELETE)) { httpMethod = new DeleteMethod(location); } else if (method.equals(Http.Method.HEAD)) { httpMethod = new HeadMethod(location); } else { httpMethod = new GetMethod(location); } if (headers != null) { for (Map.Entry<String, String> header : headers.entrySet()) { httpMethod.addRequestHeader(header.getKey(), header.getValue()); } } if ((method.equals(Http.Method.POST) || method.equals(Http.Method.PUT)) && ((body != null) || ((fileParts != null) && !fileParts.isEmpty()) | ((parts != null) && !parts.isEmpty()))) { } else if (!hasRequestHeader(httpMethod, HttpHeaders.CONTENT_TYPE)) { httpMethod.addRequestHeader(HttpHeaders.CONTENT_TYPE, ContentTypes.APPLICATION_X_WWW_FORM_URLENCODED); } if (!hasRequestHeader(httpMethod, HttpHeaders.USER_AGENT)) { httpMethod.addRequestHeader(HttpHeaders.USER_AGENT, _DEFAULT_USER_AGENT); } httpState = new HttpState(); if ((cookies != null) && (cookies.length > 0)) { org.apache.commons.httpclient.Cookie[] commonsCookies = toCommonsCookies(cookies); httpState.addCookies(commonsCookies); HttpMethodParams httpMethodParams = httpMethod.getParams(); httpMethodParams.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); } if (auth != null) { httpMethod.setDoAuthentication(true); httpState.setCredentials(new AuthScope(auth.getHost(), auth.getPort(), auth.getRealm()), new UsernamePasswordCredentials(auth.getUsername(), auth.getPassword())); } proxifyState(httpState, hostConfiguration); httpClient.executeMethod(hostConfiguration, httpMethod, httpState); Header locationHeader = httpMethod.getResponseHeader("location"); if ((locationHeader != null) && !locationHeader.equals(location)) { String redirect = locationHeader.getValue(); if (followRedirects) { return URLtoByteArray(redirect, Http.Method.GET, headers, cookies, auth, body, fileParts, parts, response, followRedirects); } else { response.setRedirect(redirect); } } InputStream inputStream = httpMethod.getResponseBodyAsStream(); if (inputStream != null) { Header contentLength = httpMethod.getResponseHeader(HttpHeaders.CONTENT_LENGTH); if (contentLength != null) { response.setContentLength(GetterUtil.getInteger(contentLength.getValue())); } Header contentType = httpMethod.getResponseHeader(HttpHeaders.CONTENT_TYPE); if (contentType != null) { response.setContentType(contentType.getValue()); } bytes = FileUtil.getBytes(inputStream); } for (Header header : httpMethod.getResponseHeaders()) { response.addHeader(header.getName(), header.getValue()); } return bytes; } finally { try { if (httpState != null) { _cookies.set(toServletCookies(httpState.getCookies())); } } catch (Exception e) { _log.error(e, e); } try { if (httpMethod != null) { httpMethod.releaseConnection(); } } catch (Exception e) { _log.error(e, e); } } }
From source file:com.zimbra.qa.unittest.TestJaxb.java
private Object sendReq(String requestBody, String requestCommand) throws HttpException, IOException, ServiceException { HttpClient client = new HttpClient(); PostMethod post = new PostMethod(TestUtil.getSoapUrl() + requestCommand); post.setRequestEntity(new StringRequestEntity(requestBody, "application/soap+xml", "UTF-8")); int respCode = HttpClientUtil.executeMethod(client, post); Assert.assertEquals(200, respCode);//from ww w.j a v a 2 s .c om Element envelope = W3cDomUtil.parseXML(post.getResponseBodyAsStream()); SoapProtocol proto = SoapProtocol.determineProtocol(envelope); Element doc = proto.getBodyElement(envelope); return JaxbUtil.elementToJaxb(doc); }
From source file:muki.tool.JavaCompilationDeploymentTestCase.java
@Test public void testPostOperation10Xml() throws Exception { String url = URL_RESOURCE1 + "/pathPostOperation10Xml"; PostMethod method = new PostMethod(url); String param = this.getXmlCd(); method.setRequestEntity(new StringRequestEntity(param, "application/xml", null)); int statusCode = this.getHttpClient().executeMethod(method); assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK); byte[] responseBody = method.getResponseBody(); String xmlResponse = new String(responseBody); assertTrue(xmlResponse.indexOf("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>") > -1); assertTrue(xmlResponse.indexOf("<title>My Track 1</title>") > -1); }
From source file:com.zimbra.qa.unittest.TestJaxb.java
private Element sendReqExpectingFail(String requestBody, String requestCommand, int expectedRespCode) throws HttpException, IOException, ServiceException { HttpClient client = new HttpClient(); PostMethod post = new PostMethod(TestUtil.getSoapUrl() + requestCommand); post.setRequestEntity(new StringRequestEntity(requestBody, "application/soap+xml", "UTF-8")); int respCode = HttpClientUtil.executeMethod(client, post); Assert.assertEquals(expectedRespCode, respCode); return W3cDomUtil.parseXML(post.getResponseBodyAsStream()); }
From source file:muki.tool.JavaCompilationDeploymentTestCase.java
@Test public void testPostOperation10Json() throws Exception { String url = URL_RESOURCE1 + "/pathPostOperation10Json"; PostMethod method = new PostMethod(url); String param = this.getJsonCd(); method.setRequestEntity(new StringRequestEntity(param, "application/json", null)); int statusCode = this.getHttpClient().executeMethod(method); assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK); byte[] responseBody = method.getResponseBody(); String jsonResponse = new String(responseBody); assertTrue(jsonResponse.indexOf("\"title\":\"My Track 1\"") > -1); }
From source file:com.googlecode.fascinator.redbox.plugins.curation.external.ExternalCurationTransactionManager.java
private JsonSimple createJobInExternalCurationManager(JsonSimple requestJson) throws IOException { PostMethod post;//from www . j ava 2s. c om try { String url = systemConfig.getString(null, "curation", "curation-manager-url"); url = url + "/job"; BasicHttpClient client = new BasicHttpClient(url); post = new PostMethod(url); StringRequestEntity requestEntity = new StringRequestEntity(requestJson.toString(), "application/json", "UTF-8"); post.setRequestEntity(requestEntity); client.executeMethod(post); int status = post.getStatusCode(); if (status != 200) { String text = post.getStatusText(); log.error(String.format( "Error accessing Curation Manager, status code '%d' returned with message: %s", status, text)); log.error(String.format("Request message was: %s", requestJson.toString())); return null; } } catch (IOException ex) { log.error("Error during search: ", ex); return null; } // Return our results body String response = null; try { response = post.getResponseBodyAsString(); } catch (IOException ex) { log.error("Error accessing response body: ", ex); return null; } JsonSimple curationManagerResponseJson = new JsonSimple(response); return curationManagerResponseJson; }
From source file:com.wso2telco.openidtokenbuilder.MIFEOpenIDTokenBuilder.java
/** * Creates the acr.//from w w w . jav a 2 s . c om * * @param msisdn the msisdn * @return the string * @throws IdentityOAuth2Exception the identity o auth2 exception */ public String createACR(String msisdn) throws IdentityOAuth2Exception { StringBuilder requestURLBuilder = new StringBuilder(); requestURLBuilder.append(ACR_HOST_URI); requestURLBuilder.append("/"); //$NON-NLS-1$ requestURLBuilder.append(CREATE_SERVICE); requestURLBuilder.append("/"); //$NON-NLS-1$ requestURLBuilder.append(SERVICE_PROVIDER); requestURLBuilder.append("/"); //$NON-NLS-1$ requestURLBuilder.append(acrAppID); String requestURL = requestURLBuilder.toString(); JSONObject requestBody = new JSONObject(); JSONObject createRequest = null; try { requestBody.put("MSISDN", (Object) new JSONArray().put(msisdn)); //$NON-NLS-1$ createRequest = new JSONObject().put("createAcrRequest", requestBody); //$NON-NLS-1$ StringRequestEntity requestEntity = new StringRequestEntity(createRequest.toString(), "application/json", "UTF-8"); //$NON-NLS-1$ //$NON-NLS-2$ PostMethod postMethod = new PostMethod(requestURL); postMethod.addRequestHeader("Authorization-ACR", "ServiceKey " + SERVICE_KEY); //$NON-NLS-1$ //$NON-NLS-2$ postMethod.addRequestHeader("Authorization", "Bearer " + ACR_ACCESS_TOKEN); //$NON-NLS-1$ //$NON-NLS-2$ postMethod.setRequestEntity(requestEntity); if (DEBUG) { log.debug("Connecting to ACR engine @ " + ACR_HOST_URI); //$NON-NLS-1$ log.debug("Service name : " + CREATE_SERVICE); //$NON-NLS-1$ log.debug("Service provider : " + SERVICE_PROVIDER); //$NON-NLS-1$ log.debug("App key : " + acrAppID); //$NON-NLS-1$ log.debug("Request - create ACR : " + requestEntity.getContent()); //$NON-NLS-1$ } httpClient = new HttpClient(); httpClient.executeMethod(postMethod); return new String(postMethod.getResponseBody(), "UTF-8"); //$NON-NLS-1$ } catch (UnsupportedEncodingException e) { log.error("Error occured while creating request", e); //$NON-NLS-1$ throw new IdentityOAuth2Exception("Error occured while creating request", e); //$NON-NLS-1$ } catch (JSONException e) { log.error("Error occured while creating request", e); //$NON-NLS-1$ throw new IdentityOAuth2Exception("Error occured while creating request", e); //$NON-NLS-1$ } catch (HttpException e) { log.error("Error occured while creating ACR", e); //$NON-NLS-1$ throw new IdentityOAuth2Exception("Error occured while creating ACR", e); //$NON-NLS-1$ } catch (IOException e) { log.error("Error occured while creating ACR", e); //$NON-NLS-1$ throw new IdentityOAuth2Exception("Error occured while creating ACR", e); //$NON-NLS-1$ } }
From source file:muki.tool.JavaCompilationDeploymentTestCase.java
@Test public void testPostOperation11Xml() throws Exception { String url = URL_RESOURCE1 + "/pathPostOperation11Xml/21?page=31"; PostMethod method = new PostMethod(url); String param = this.getXmlCd(); method.setRequestEntity(new StringRequestEntity(param, "application/xml", null)); int statusCode = this.getHttpClient().executeMethod(method); assertTrue("Method failed: " + method.getStatusLine(), statusCode == HttpStatus.SC_OK); byte[] responseBody = method.getResponseBody(); String xmlResponse = new String(responseBody); assertTrue(xmlResponse.indexOf("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>") > -1); assertTrue(xmlResponse.indexOf("<title>postOperation11-21-31</title>") > -1); }