List of usage examples for org.apache.commons.httpclient HttpMethod releaseConnection
public abstract void releaseConnection();
From source file:org.mule.transport.as2.As2MessageDispatcher.java
@Override protected void doDispatch(MuleEvent event) throws Exception { logger.debug("DBG: inside " + getClass() + ".doDispatch()"); HttpMethod httpMethod = getMethod(event); as2Connector.setupClientAuthorizationLocal(event, httpMethod, client, endpoint); try {//ww w . ja v a2 s . c o m execute(event, httpMethod); if (returnException(event, httpMethod)) { logger.error(httpMethod.getResponseBodyAsString()); Exception cause = new Exception(String.format("Http call returned a status of: %1d %1s", httpMethod.getStatusCode(), httpMethod.getStatusText())); throw new DispatchException(event, getEndpoint(), cause); } else if (httpMethod.getStatusCode() >= REDIRECT_STATUS_CODE_RANGE_START) { if (logger.isInfoEnabled()) { logger.info("Received a redirect response code: " + httpMethod.getStatusCode() + " " + httpMethod.getStatusText()); } } else { logger.debug("DBG: response Body is: " + httpMethod.getResponseBodyAsString()); /* Check the incoming synch MDN */ MimeMultipart mdn = MDNBuilder.createMDNFromResponse(httpMethod.getResponseBodyAsStream(), "multipart/report"); if (MDNBuilder.identifyMdnType(mdn) != MdnType.PROCESSED) { throw new Exception("MDN is not of type PROCESSED"); } } } finally { httpMethod.releaseConnection(); } }
From source file:org.mule.transport.http.HttpClientMessageDispatcher.java
@Override protected void doDispatch(MuleEvent event) throws Exception { HttpMethod httpMethod = getMethod(event); httpConnector.setupClientAuthorization(event, httpMethod, client, endpoint); try {/* ww w. j a va 2s . c o m*/ execute(event, httpMethod); if (returnException(event, httpMethod)) { logger.error(httpMethod.getResponseBodyAsString()); Exception cause = new Exception(String.format("Http call returned a status of: %1d %1s", httpMethod.getStatusCode(), httpMethod.getStatusText())); throw new DispatchException(event, getEndpoint(), cause); } else if (httpMethod.getStatusCode() >= REDIRECT_STATUS_CODE_RANGE_START) { if (logger.isInfoEnabled()) { logger.info("Received a redirect response code: " + httpMethod.getStatusCode() + " " + httpMethod.getStatusText()); } } } finally { httpMethod.releaseConnection(); } }
From source file:org.mule.transport.http.HttpClientMessageDispatcher.java
@Override protected MuleMessage doSend(MuleEvent event) throws Exception { HttpMethod httpMethod = getMethod(event); httpConnector.setupClientAuthorization(event, httpMethod, client, endpoint); httpMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new MuleHttpMethodRetryHandler()); boolean releaseConn = false; try {/*from www . j a v a 2 s.c o m*/ httpMethod = execute(event, httpMethod); DefaultExceptionPayload ep = null; if (returnException(event, httpMethod)) { ep = new DefaultExceptionPayload(new DispatchException(event, getEndpoint(), new HttpResponseException(httpMethod.getStatusText(), httpMethod.getStatusCode()))); } else if (httpMethod.getStatusCode() >= REDIRECT_STATUS_CODE_RANGE_START) { try { return handleRedirect(httpMethod, event); } catch (Exception e) { ep = new DefaultExceptionPayload(new DispatchException(event, getEndpoint(), e)); return getResponseFromMethod(httpMethod, ep); } } releaseConn = httpMethod.getResponseBodyAsStream() == null; return getResponseFromMethod(httpMethod, ep); } catch (Exception e) { releaseConn = true; if (e instanceof DispatchException) { throw (DispatchException) e; } throw new DispatchException(event, getEndpoint(), e); } finally { if (releaseConn) { httpMethod.releaseConnection(); } } }
From source file:org.mule.transport.http.HttpClientMessageRequester.java
/** * Make a specific request to the underlying transport * * @param timeout the maximum time the operation should block before returning. * The call should return immediately if there is data available. If * no data becomes available before the timeout elapses, null will be * returned/*from w ww. java 2s. c o m*/ * @return the result of the request wrapped in a MuleMessage object. Null will be * returned if no data was avaialable * @throws Exception if the call to the underlying protocal cuases an exception */ protected MuleMessage doRequest(long timeout) throws Exception { HttpMethod httpMethod = new GetMethod(endpoint.getEndpointURI().getAddress()); connector.setupClientAuthorization(null, httpMethod, client, endpoint); boolean releaseConn = false; try { HttpClient client = new HttpClient(); client.executeMethod(httpMethod); if (httpMethod.getStatusCode() == HttpStatus.SC_OK) { MuleMessage res = (MuleMessage) receiveTransformer.transform(httpMethod); if (StringUtils.EMPTY.equals(res.getPayload())) { releaseConn = true; } return res; } else { releaseConn = true; throw new ReceiveException( HttpMessages.requestFailedWithStatus(httpMethod.getStatusLine().toString()), endpoint, timeout); } } catch (ReceiveException e) { releaseConn = true; throw e; } catch (Exception e) { releaseConn = true; throw new ReceiveException(endpoint, timeout, e); } finally { if (releaseConn) { httpMethod.releaseConnection(); } } }
From source file:org.mulgara.resolver.http.HttpContent.java
/** * Obtain a valid connection and follow redirects if necessary. * /* www. j a v a 2s .c o m*/ * @param methodType request the headders (HEAD) or body (GET) * @return valid connection method. Can be null. * @throws NotModifiedException if the content validates against the cache * @throws IOException if there's difficulty communicating with the web site */ private HttpMethod establishConnection(int methodType) throws IOException, NotModifiedException { if (logger.isDebugEnabled()) logger.debug("Establishing connection"); HttpMethod method = getConnectionMethod(methodType); assert method != null; Header header = null; /* // Add cache validation headers to the request if (lastModifiedMap.containsKey(httpUri)) { String lastModified = (String) lastModifiedMap.get(httpUri); assert lastModified != null; method.addRequestHeader("If-Modified-Since", lastModified); } if (eTagMap.containsKey(httpUri)) { String eTag = (String) eTagMap.get(httpUri); assert eTag != null; method.addRequestHeader("If-None-Match", eTag); } */ // Make the request if (logger.isDebugEnabled()) logger.debug("Executing HTTP request"); connection.open(); method.execute(state, connection); if (logger.isDebugEnabled()) { logger.debug("Executed HTTP request, response code " + method.getStatusCode()); } // Interpret the response header if (method.getStatusCode() == HttpStatus.SC_NOT_MODIFIED) { // cache has been validated throw new NotModifiedException(httpUri); } else if (!isValidStatusCode(method.getStatusCode())) { throw new UnknownHostException("Unable to obtain connection to " + httpUri + ". Returned status code " + method.getStatusCode()); } else { // has a redirection been issued int numberOfRedirection = 0; while (isRedirected(method.getStatusCode()) && numberOfRedirection <= MAX_NO_REDIRECTS) { // release the existing connection method.releaseConnection(); //attempt to follow the redirects numberOfRedirection++; // obtain the new location header = method.getResponseHeader("location"); if (header != null) { try { initialiseSettings(new URL(header.getValue())); if (logger.isInfoEnabled()) { logger.info("Redirecting to " + header.getValue()); } // attempt a new connection to this location method = getConnectionMethod(methodType); connection.open(); method.execute(state, connection); if (!isValidStatusCode(method.getStatusCode())) { throw new UnknownHostException( "Unable to obtain connection to " + " the redirected site " + httpUri + ". Returned status code " + method.getStatusCode()); } } catch (URISyntaxException ex) { throw new IOException( "Unable to follow redirection to " + header.getValue() + " Not a valid URI"); } } else { throw new IOException("Unable to obtain redirecting detaild from " + httpUri); } } } // Update metadata about the cached document Header lastModifiedHeader = method.getResponseHeader("Last-Modified"); if (lastModifiedHeader != null) { logger.debug(lastModifiedHeader.toString()); assert lastModifiedHeader.getElements().length >= 1; assert lastModifiedHeader.getElements()[0].getName() != null; assert lastModifiedHeader.getElements()[0].getName() instanceof String; // previous code: added to cache } Header eTagHeader = method.getResponseHeader("Etag"); if (eTagHeader != null) { logger.debug(eTagHeader.toString()); assert eTagHeader.getElements().length >= 1; assert eTagHeader.getElements()[0].getName() != null; assert eTagHeader.getElements()[0].getName() instanceof String; // previous code: added to cache } return method; }
From source file:org.mulgara.resolver.http.HttpContent.java
/** * {@inheritDoc}//from w ww.j a va 2 s . c o m * * This particular implementation tries to read the content type directly * from the HTTP <code>Content-Type</code> header. */ public MimeType getContentType() throws NotModifiedException { // if we don't have the type from the connection already, then establish one if (contentType == null) { HttpMethod method = null; try { method = establishConnection(HEAD); contentType = readMimeType(method); } catch (IOException e) { logger.info("Unable to obtain content type for " + httpUri); } finally { // we're the only one to have needed this connection, so drop it if (method != null) method.releaseConnection(); if (connection != null) connection.close(); } } return contentType; }
From source file:org.nuxeo.ecm.core.opencmis.impl.client.sso.CasGreeter.java
public String fetchServiceTicket(HttpMethod page) throws HttpException, IOException { client.executeMethod(page);//from w w w . ja va 2 s . c o m try { if (page.getStatusCode() != HttpStatus.SC_OK) { throw new Error("Cannot get login form"); } return extractLoginTicket(page.getResponseBodyAsString()); } finally { page.releaseConnection(); } }
From source file:org.nuxeo.ecm.core.opencmis.impl.client.sso.CasGreeter.java
public String fetchServiceLocation(HttpMethod page) throws HttpException, IOException { client.executeMethod(page);/*from w w w . ja va2 s.c o m*/ try { if (page.getStatusCode() != HttpStatus.SC_OK) { throw new Error("Cannot authenticate"); } return extractRedirectLink(page.getResponseBodyAsString()); } finally { page.releaseConnection(); } }
From source file:org.nuxeo.ecm.platform.web.common.ajax.AjaxProxyServlet.java
protected static String doRequest(String method, String targetURL, HttpServletRequest req) throws IOException { HttpClient client = new HttpClient(); HttpMethod httpMethod; if ("GET".equals(method)) { httpMethod = new GetMethod(targetURL); } else if ("POST".equals(method)) { httpMethod = new PostMethod(targetURL); ((PostMethod) httpMethod).setRequestEntity(new InputStreamRequestEntity(req.getInputStream())); } else if ("PUT".equals(method)) { httpMethod = new PutMethod(targetURL); ((PutMethod) httpMethod).setRequestEntity(new InputStreamRequestEntity(req.getInputStream())); } else {//from w w w . j a va 2 s . c o m throw new IllegalStateException("Unknown HTTP method: " + method); } Map<String, String[]> params = req.getParameterMap(); for (String paramName : params.keySet()) { httpMethod.getParams().setParameter(paramName, params.get(paramName)); } client.executeMethod(httpMethod); String body = httpMethod.getResponseBodyAsString(); httpMethod.releaseConnection(); return body; }
From source file:org.nuxeo.ecm.tokenauth.TestAnonymousTokenAuthenticationServlet.java
@Test public void testServletAsAnonymous() throws Exception { HttpClient httpClient = new HttpClient(); HttpMethod getMethod = null; try {/*from w ww . ja v a 2s . co m*/ // ------------ Test anonymous user not allowed ---------------- getMethod = new GetMethod( "http://localhost:18080/authentication/token?applicationName=myFavoriteApp&deviceId=dead-beaf-cafe-babe&permission=rw"); int status = httpClient.executeMethod(getMethod); assertEquals(401, status); // ------------ Test anonymous user allowed ---------------- harness.deployContrib("org.nuxeo.ecm.platform.login.token.test", "OSGI-INF/test-token-authentication-allow-anonymous-token-contrib.xml"); status = httpClient.executeMethod(getMethod); assertEquals(201, status); String token = getMethod.getResponseBodyAsString(); assertNotNull(token); assertNotNull(tokenAuthenticationService.getUserName(token)); assertEquals(1, tokenAuthenticationService.getTokenBindings("Guest").size()); harness.undeployContrib("org.nuxeo.ecm.platform.login.token.test", "OSGI-INF/test-token-authentication-allow-anonymous-token-contrib.xml"); } finally { getMethod.releaseConnection(); } }