List of usage examples for org.apache.commons.httpclient.params HttpMethodParams setCredentialCharset
public void setCredentialCharset(String paramString)
From source file:com.cordys.coe.ac.httpconnector.execution.MethodExecutor.java
/** * Send the HTTP request to the web server and returns the response. * // www .j ava 2 s .co m * @param reqNode * Request XML node. * @param serverConnection * Server connection information. * @param methodInfo * Method information information. * @param counters * JMX performance counters * @return Response XML node. * @throws ConnectorException * @throws HandlerException */ public static int sendRequest(int reqNode, IServerConnection serverConnection, IMethodConfiguration methodInfo, PerformanceCounters counters) throws ConnectorException, HandlerException { boolean setJMXInfo = counters == null ? false : true; // Get request and response handlers for converting the data. IRequestHandler requestHandler = methodInfo.getRequestHandler(); IResponseHandler responseHandler = methodInfo.getResponseHandler(); // Create the connection HttpClient client = serverConnection.getHttpClient(); client.getParams().setContentCharset("UTF-8"); client.getParams().setCredentialCharset("UTF-8"); client.getParams().setHttpElementCharset("UTF-8"); HttpMethod httpMethod; int timeout = serverConnection.getTimeout(); // Convert the SOAP request XML into an HTTP request. long startTime = 0; if (setJMXInfo) { startTime = counters.getStartTime(); } httpMethod = requestHandler.process(reqNode, serverConnection, client); if (setJMXInfo) { counters.finishRequestTransformation(startTime); } // Set additional HTTP parameters. HttpMethodParams hmpMethodParams = httpMethod.getParams(); if (hmpMethodParams != null) { // Set the authentication character set to UTF-8, if not set. String sCredCharset = hmpMethodParams.getCredentialCharset(); if ((sCredCharset == null) || (sCredCharset.length() == 0)) { hmpMethodParams.setCredentialCharset("UTF-8"); } if (timeout > 0) { hmpMethodParams.setSoTimeout(timeout); } } // Send the request and handle the response. try { if (setJMXInfo) { startTime = counters.getStartTime(); } int statusCode = client.executeMethod(httpMethod); if (setJMXInfo) { counters.finishHTTP(startTime); } int validStatusCode = methodInfo.getValidResponseCode(); if (statusCode != HttpStatus.SC_OK) { if (LOG.isDebugEnabled()) { LOG.debug("Received HTTP error code: " + statusCode); } } if (setJMXInfo) { startTime = counters.getStartTime(); } // Convert the response into XML. int responseNode = responseHandler.convertResponseToXml(httpMethod, serverConnection, Node.getDocument(reqNode)); if (setJMXInfo) { counters.finishResponseTransformation(startTime); } if ((validStatusCode >= 0) && (statusCode != validStatusCode)) { if (responseNode != 0) { Node.delete(responseNode); responseNode = 0; } throw new ConnectorException(ConnectorExceptionMessages.INVALID_STATUS_CODE_RECEIVED_0_EXPECTED_1, statusCode, validStatusCode); } return responseNode; } catch (ConnectorException e) { throw e; } catch (HttpException e) { throw new ConnectorException(e, ConnectorExceptionMessages.HTTP_REQUEST_FAILED_0, e.getMessage()); } catch (IOException e) { throw new ConnectorException(e, ConnectorExceptionMessages.HTTP_CONNECTION_FAILED_0, e.getMessage()); } catch (XMLException e) { throw new ConnectorException((Throwable) e, ConnectorExceptionMessages.INVALID_RESPONSE_XML_RECEIVED); } finally { // Release the connection. httpMethod.releaseConnection(); } }
From source file:com.zimbra.cs.dav.client.WebDavClient.java
protected HttpMethod executeMethod(HttpMethod m, Depth d, String bodyForLogging) throws IOException { HttpMethodParams p = m.getParams(); if (p != null) p.setCredentialCharset("UTF-8"); m.setDoAuthentication(true);/*from ww w . j a va2 s . c om*/ m.setRequestHeader("User-Agent", mUserAgent); String depth = "0"; switch (d) { case one: depth = "1"; break; case infinity: depth = "infinity"; break; case zero: break; default: break; } m.setRequestHeader("Depth", depth); logRequestInfo(m, bodyForLogging); HttpClientUtil.executeMethod(mClient, m); logResponseInfo(m); return m; }
From source file:com.zimbra.qa.unittest.prov.ldap.TestProvIDN.java
@Test public void testBasicAuth() throws Exception { Names.IDNName domainName = new Names.IDNName(makeTestDomainName("basicAuthTest.")); Domain domain = createDomain(domainName.uName(), domainName.uName()); Names.IDNName acctName = new Names.IDNName("acct", domainName.uName()); Account acct = (Account) createTest(EntryType.ACCOUNT, NameType.UNAME, acctName); HttpState initialState = new HttpState(); /*//from ww w . ja v a2 s . com Cookie authCookie = new Cookie(restURL.getURL().getHost(), "ZM_AUTH_TOKEN", mAuthToken, "/", null, false); Cookie sessionCookie = new Cookie(restURL.getURL().getHost(), "JSESSIONID", mSessionId, "/zimbra", null, false); initialState.addCookie(authCookie); initialState.addCookie(sessionCookie); */ String guestName = acct.getUnicodeName(); String guestPassword = "test123"; Credentials loginCredentials = new UsernamePasswordCredentials(guestName, guestPassword); initialState.setCredentials(AuthScope.ANY, loginCredentials); HttpClient client = new HttpClient(); client.setState(initialState); String url = UserServlet.getRestUrl(acct) + "/Calendar"; System.out.println("REST URL: " + url); HttpMethod method = new GetMethod(url); HttpMethodParams methodParams = method.getParams(); methodParams.setCredentialCharset("UTF-8"); try { int respCode = HttpClientUtil.executeMethod(client, method); if (respCode != HttpStatus.SC_OK) { System.out.println("failed, respCode=" + respCode); } else { boolean chunked = false; boolean textContent = false; /* System.out.println("Headers:"); System.out.println("--------"); for (Header header : method.getRequestHeaders()) { System.out.print(" " + header.toString()); } System.out.println(); System.out.println("Body:"); System.out.println("-----"); String respBody = method.getResponseBodyAsString(); System.out.println(respBody); */ } } finally { // Release the connection. method.releaseConnection(); } }