Author: tchemit Date: 2014-04-17 00:37:55 +0200 (Thu, 17 Apr 2014) New Revision: 459 Url: http://forge.nuiton.org/projects/jredmine/repository/revisions/459 Log: fixes #3162 Modified: trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClient.java Modified: trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClient.java =================================================================== --- trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClient.java 2014-04-16 20:08:22 UTC (rev 458) +++ trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClient.java 2014-04-16 22:37:55 UTC (rev 459) @@ -26,18 +26,17 @@ import com.google.common.base.Preconditions; import com.google.common.collect.Lists; -import com.google.common.collect.Maps; import org.apache.commons.collections.MapUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.StatusLine; -import org.apache.http.client.CookieStore; import org.apache.http.client.HttpClient; import org.apache.http.client.ResponseHandler; import org.apache.http.client.entity.UrlEncodedFormEntity; @@ -47,17 +46,13 @@ import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpRequestBase; -import org.apache.http.client.protocol.ClientContext; import org.apache.http.client.utils.URIBuilder; -import org.apache.http.entity.mime.MultipartEntity; -import org.apache.http.entity.mime.content.FileBody; -import org.apache.http.entity.mime.content.StringBody; -import org.apache.http.impl.client.BasicCookieStore; -import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.http.conn.HttpClientConnectionManager; +import org.apache.http.entity.mime.MultipartEntityBuilder; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.conn.BasicHttpClientConnectionManager; +import org.apache.http.message.BasicHeader; import org.apache.http.message.BasicNameValuePair; -import org.apache.http.params.CoreProtocolPNames; -import org.apache.http.protocol.BasicHttpContext; -import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import org.nuiton.jredmine.model.io.xpp3.RedmineXpp3Helper; @@ -68,6 +63,8 @@ import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Map; @@ -91,53 +88,47 @@ protected final URI serverURI; - protected final HttpContext clientContext; + protected final HttpClientConnectionManager connectionManager; protected boolean showRequest; protected boolean open; - protected Map<String, String> defaultHeaders = Maps.newHashMap(); - public RedmineClient(RedmineClientConfiguration configuration) { this.configuration = configuration; xpp3Helper = new RedmineXpp3Helper(); showRequest = configuration.isVerbose(); - client = new DefaultHttpClient(); - clientContext = new BasicHttpContext(); -// // Create a local instance of cookie store -// CookieStore cookieStore = new BasicCookieStore(); -// -// // Bind custom cookie store to the local context -// clientContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); + Collection<Header> headers = new ArrayList<Header>(); + RedmineClientAuthConfiguration authConfiguration = + configuration.getAuthConfiguration(); + if (authConfiguration.isUseApiKey()) { + + if (log.isDebugEnabled()) { + log.debug("Will use api key for authentication"); + } + + // keep apiKey to put to every request + headers.add(new BasicHeader("X-Redmine-API-Key", authConfiguration.getApiKey())); + } + + connectionManager = new BasicHttpClientConnectionManager(); + client = HttpClientBuilder. + create(). + setDefaultHeaders(headers). + setConnectionManager(connectionManager). + disableRedirectHandling(). + disableAuthCaching(). + disableCookieManagement(). + disableConnectionState(). + build(); // Get server uri try { serverURI = configuration.getUrl().toURI(); } catch (URISyntaxException e) { throw new IllegalStateException("Could not get uri from " + configuration.getUrl()); } - - // set encoding (will then encode parameters fine) - client.getParams().setParameter( - CoreProtocolPNames.HTTP_CONTENT_CHARSET, - configuration.getEncoding()); - -// if (!configuration.isAnonymous()) { - RedmineClientAuthConfiguration authConfiguration = - configuration.getAuthConfiguration(); - if (authConfiguration.isUseApiKey()) { - - if (log.isDebugEnabled()) { - log.debug("Will use api key for authentication"); - } - - // keep apiKey to put to every request - defaultHeaders.put("X-Redmine-API-Key", - authConfiguration.getApiKey()); - } -// } } public boolean isOpen() { @@ -189,7 +180,7 @@ ResponseHandler<T> responseHandler = new RedmineSimpleResponseHandler<T>(false, request.getType(), xpp3Helper, gm.getURI().toString()); - T result = client.execute(gm, responseHandler, clientContext); + T result = client.execute(gm, responseHandler); return result; } finally { releaseConnection(); @@ -214,7 +205,7 @@ ResponseHandler<T[]> responseHandler = new RedmineArrayResponseHandler<T>(false, request.getType(), xpp3Helper, gm.getURI().toString()); - T[] result = client.execute(gm, responseHandler, clientContext); + T[] result = client.execute(gm, responseHandler); return result; } finally { releaseConnection(); @@ -231,7 +222,7 @@ try { - client.getConnectionManager().shutdown(); + connectionManager.shutdown(); } finally { open = false; @@ -265,13 +256,8 @@ break; case HEAD: default: - throw new IllegalStateException( - "Can not deal with method " + request.getMethod()); + throw new IllegalStateException("Can not deal with method " + request.getMethod()); } - - for (Map.Entry<String, String> entry : defaultHeaders.entrySet()) { - gm.addHeader(entry.getKey(), entry.getValue()); - } return gm; } @@ -310,9 +296,7 @@ MapUtils.isEmpty(attachments), "Can not do a GET request with multi-parts, use a POST or UPDATE request"); - HttpDelete gm = new HttpDelete(uri); - addParams(gm, parameters); return gm; @@ -420,7 +404,7 @@ Map<String, File> attachments, Map<String, String> parameters) throws IOException { - MultipartEntity entity = new MultipartEntity(); + MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create(); if (MapUtils.isNotEmpty(parameters)) { @@ -435,7 +419,7 @@ } continue; } - entity.addPart(key, new StringBody(value)); + multipartEntityBuilder.addTextBody(key, value); } } @@ -446,14 +430,13 @@ if (log.isDebugEnabled()) { log.debug("add attachment " + key + "=" + file); } - FileBody bin = new FileBody(file); - entity.addPart(key, bin); - + multipartEntityBuilder.addBinaryBody(key, file); } if (attachments.isEmpty()) { log.warn("no attachment in a multi-part request!"); } + HttpEntity entity = multipartEntityBuilder.build(); if (log.isDebugEnabled()) { entity.writeTo(System.out); } @@ -466,7 +449,7 @@ } protected void releaseConnection() { - client.getConnectionManager().closeExpiredConnections(); + connectionManager.closeExpiredConnections(); } protected static abstract class AbstractRedmineResponseHandler<T> { @@ -500,17 +483,13 @@ if (statusCode == HttpStatus.SC_NOT_FOUND) { String responseAsString = EntityUtils.toString(entity); - throw new IOException( - "could not retreave some datas : " + - responseAsString); + throw new IOException("could not retreave some datas : " + responseAsString); } if (statusCode != HttpStatus.SC_OK) { String responseAsString = EntityUtils.toString(entity); log.error("Error = " + responseAsString); - throw new IOException( - "Got error code <" + statusCode + ":" + - sl.getReasonPhrase() + "> on " + uri); + throw new IOException("Got error code <" + statusCode + ":" + sl.getReasonPhrase() + "> on " + uri); } } }