Java tutorial
/******************************************************************************* * Copyright (c) 2008, 2009 SOPERA GmbH. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * SOPERA GmbH - initial API and implementation *******************************************************************************/ package org.eclipse.swordfish.internal.resolver.backend.base.impl; import java.io.IOException; import java.io.Reader; import java.io.StringReader; import java.util.Map; import javax.xml.bind.JAXB; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpURL; import org.apache.commons.httpclient.methods.DeleteMethod; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.PutMethod; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.eclipse.swordfish.core.SwordfishException; import org.eclipse.swordfish.core.resolver.backend.ClientRequest; import org.eclipse.swordfish.core.resolver.backend.ClientResponse; import org.eclipse.swordfish.core.resolver.backend.ProxyConstants.Method; import org.eclipse.swordfish.core.resolver.backend.ProxyConstants.Status; import org.eclipse.swordfish.internal.resolver.backend.base.AbstractProxy; import org.eclipse.swordfish.internal.resolver.backend.base.RegistryProxyFactory; import org.springframework.util.Assert; /** * */ public class HttpClientProxy extends AbstractProxy { private static final Log LOG = LogFactory.getLog(HttpClientProxy.class); private HttpClient client; public HttpClientProxy() { client = new HttpClient(); } @Override protected ClientResponse invoke(ClientRequest request) { ClientResponse response = null; Assert.notNull(request, "Unexpected object state: ClientRequest cannot be null."); Assert.notNull(request.getMethod(), "Unexpected object state: request method cannot be null."); Assert.notNull(request.getURI(), "Unexpected object state: request URI cannot be null."); try { response = doInvoke(request); } catch (Exception e) { LOG.error("Couldn't execute " + request.getMethod() + " request: ", e); throw new SwordfishException("Couldn't execute " + request.getMethod() + " method: ", e); } return response; } private ClientResponse doInvoke(ClientRequest request) { ClientResponse response = RegistryProxyFactory.getInstance().createResponse(); HttpMethod method = getMethod(request.getMethod()); try { String escapedRequestUrl = request.getURI().toASCIIString(); HttpURL requestUrl = new HttpURL(escapedRequestUrl.toCharArray()); if (request.getProperties() != null) { Map<String, String> properties = request.getProperties(); String[] queryNames = properties.keySet().toArray(new String[0]); String[] queryValues = properties.values().toArray(new String[0]); requestUrl.setQuery(queryNames, queryValues); } method.setURI(requestUrl); int statusCode = getClient().executeMethod(method); response.setStatus(Status.get(statusCode)); String responseBody = method.getResponseBodyAsString(); if (request.getEntityType() != null) { response.setEntity(mapResponse(responseBody, request.getEntityType())); } else { response.setEntity(responseBody); } } catch (HttpException e) { LOG.error("Couldn't perform call to registry: ", e); response.setStatus(Status.ERROR); response.setEntity(e); } catch (IOException e) { LOG.error("Couldn't perform call to registry: ", e); response.setStatus(Status.ERROR); response.setEntity(e); } finally { if (method != null) { method.releaseConnection(); } } return response; } protected HttpMethod getMethod(Method method) { HttpMethod result = null; if (method.equals(Method.GET)) { result = new GetMethod(); } else if (method.equals(Method.POST)) { result = new PostMethod(); } else if (method.equals(Method.DELETE)) { result = new DeleteMethod(); } else if (method.equals(Method.PUT)) { result = new PutMethod(); } return result; } private Object mapResponse(String response, Class<? extends Object> etnityType) { Object responseObject = null; Reader responseReader = new StringReader(response); ClassLoader cl = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); responseObject = JAXB.unmarshal(responseReader, etnityType); } finally { Thread.currentThread().setContextClassLoader(cl); } return responseObject; } public HttpClient getClient() { return client; } public void setClient(HttpClient client) { this.client = client; } }