Author: tchemit Date: 2012-07-13 00:58:47 +0200 (Fri, 13 Jul 2012) New Revision: 847 Url: http://nuiton.org/repositories/revision/maven-helper-plugin/847 Log: rest request contains now his method Added: trunk/src/main/java/org/nuiton/io/rest/RestMethod.java Modified: trunk/src/main/java/org/nuiton/io/rest/RestClient.java trunk/src/main/java/org/nuiton/io/rest/RestRequest.java trunk/src/main/java/org/nuiton/io/rest/RestSession.java Modified: trunk/src/main/java/org/nuiton/io/rest/RestClient.java =================================================================== --- trunk/src/main/java/org/nuiton/io/rest/RestClient.java 2012-07-12 17:03:55 UTC (rev 846) +++ trunk/src/main/java/org/nuiton/io/rest/RestClient.java 2012-07-12 22:58:47 UTC (rev 847) @@ -25,9 +25,6 @@ package org.nuiton.io.rest; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - import java.io.IOException; import java.io.InputStream; @@ -39,8 +36,6 @@ */ public abstract class RestClient { - private static final Log log = LogFactory.getLog(RestClient.class); - /** rest client configuration */ protected RestClientConfiguration configuration; @@ -85,15 +80,6 @@ } /** - * Add a request into the client. - * - * @param builder the new request to add - */ - public void addRequestBuilder(RestRequestBuilder builder) { - getRequestFactory().addRequestBuilder(builder); - } - - /** * Obtain a request given his id and the args given. * * @param id id of the request Added: trunk/src/main/java/org/nuiton/io/rest/RestMethod.java =================================================================== --- trunk/src/main/java/org/nuiton/io/rest/RestMethod.java (rev 0) +++ trunk/src/main/java/org/nuiton/io/rest/RestMethod.java 2012-07-12 22:58:47 UTC (rev 847) @@ -0,0 +1,11 @@ +package org.nuiton.io.rest; + +/** + * Possible rest methods. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public enum RestMethod { + GET, POST, PUT, DELETE, HEAD +} Modified: trunk/src/main/java/org/nuiton/io/rest/RestRequest.java =================================================================== --- trunk/src/main/java/org/nuiton/io/rest/RestRequest.java 2012-07-12 17:03:55 UTC (rev 846) +++ trunk/src/main/java/org/nuiton/io/rest/RestRequest.java 2012-07-12 22:58:47 UTC (rev 847) @@ -26,7 +26,6 @@ package org.nuiton.io.rest; import java.io.File; -import java.net.URL; import java.util.Map; /** @@ -36,6 +35,7 @@ * @since 1.0.3 */ public interface RestRequest { + /** @return the splitted path to add to url */ String[] getPath(); @@ -61,5 +61,12 @@ * @return the full url for this request * @since 1.5 */ - String toPath(URL baseUrl); + String toPath(String baseUrl); + + /** + * The method to use for this request. + * + * @return 1.5 + */ + RestMethod getMethod(); } Modified: trunk/src/main/java/org/nuiton/io/rest/RestSession.java =================================================================== --- trunk/src/main/java/org/nuiton/io/rest/RestSession.java 2012-07-12 17:03:55 UTC (rev 846) +++ trunk/src/main/java/org/nuiton/io/rest/RestSession.java 2012-07-12 22:58:47 UTC (rev 847) @@ -30,10 +30,16 @@ import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpConnection; import org.apache.commons.httpclient.HttpMethod; +import org.apache.commons.httpclient.HttpMethodBase; import org.apache.commons.httpclient.HttpState; import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.StatusLine; +import org.apache.commons.httpclient.methods.DeleteMethod; +import org.apache.commons.httpclient.methods.EntityEnclosingMethod; +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.httpclient.methods.multipart.FilePart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; @@ -183,28 +189,45 @@ ); } - protected String getUri(String... paths) { + protected String getRequestUrl(RestRequest request) { String uri = client.getHostConfiguration().getHostURL(); - for (String path : paths) { - uri += '/' + path; - } - return uri; + String result = request.toPath(uri); + return result; } public HttpMethod doRequest(RestRequest request) throws IOException { - String uri = getUri(request.getPath()); - String[] parameters = request.getParameters(); - Map<String, File> attachments = request.getAttachments(); - if (showRequest) { - log.info("doRequest " + uri); + log.info("doRequest " + getRequestUrl(request)); } if (log.isDebugEnabled()) { log.debug("doRequest with parameters : " + - Arrays.toString(parameters)); + Arrays.toString(request.getParameters())); } - PostMethod gm = new PostMethod(uri); + + HttpMethod gm; + switch (request.getMethod()) { + + case GET: + gm = prepareGetRequest(request); + break; + case POST: + gm = preparePostRequest(request); + + break; + case PUT: + gm = preparePutRequest(request); + + break; + case DELETE: + gm = prepareDeleteRequest(request); + break; + case HEAD: + default: + throw new IllegalStateException("Can not deal with method " + request.getMethod()); + } + + // add cookies if any if (open) { Cookie[] cookies = client.getState().getCookies(); for (Cookie c : cookies) { @@ -216,8 +239,65 @@ } } + client.executeMethod(gm); + + return gm; + } + + public HttpMethod prepareGetRequest(RestRequest request) throws IOException { + + String uri = getRequestUrl(request); + String[] parameters = request.getParameters(); + Map<String, File> attachments = request.getAttachments(); + + + if (attachments != null) { + + // multi-part request + //not possible with a simple Get method + + throw new IllegalStateException("Can not do a GET request with multi-parts, use a POST or UPDATE request"); + } + + GetMethod gm = new GetMethod(uri); + + addParams(gm, parameters); + + return gm; + } + + public HttpMethod prepareDeleteRequest(RestRequest request) throws IOException { + + String uri = getRequestUrl(request); + String[] parameters = request.getParameters(); + Map<String, File> attachments = request.getAttachments(); + + if (attachments != null) { + + // multi-part request + //not possible with a simple Get method + + throw new IllegalStateException("Can not do a DELETE request with multi-parts, use a POST or UPDATE request"); + } + + DeleteMethod gm = new DeleteMethod(uri); + + addParams(gm, parameters); + + return gm; + } + + public HttpMethod preparePostRequest(RestRequest request) throws IOException { + + String uri = getRequestUrl(request); + String[] parameters = request.getParameters(); + Map<String, File> attachments = request.getAttachments(); + + PostMethod gm = new PostMethod(uri); + if (attachments == null) { + // not a multi-part request if (parameters.length > 0) { @@ -234,6 +314,7 @@ } continue; } + gm.addParameter(key, value); } } @@ -241,69 +322,119 @@ // multi-part request - List<Part> paramParts = new ArrayList<Part>(); + prepareMultiPart(gm, attachments, parameters); + } + return gm; + } - if (parameters.length > 0) { + public HttpMethod preparePutRequest(RestRequest request) throws IOException { - // add parameters as part of multi-part + String uri = getRequestUrl(request); + String[] parameters = request.getParameters(); + Map<String, File> attachments = request.getAttachments(); - int nbParams = parameters.length / 2; - for (int i = 0; i < nbParams; i++) { - String key = parameters[2 * i]; - String value = parameters[2 * i + 1]; - if (value == null) { - if (log.isDebugEnabled()) { - log.debug("skip null parameter " + key); - } - continue; - } - paramParts.add(new StringPart(key, value)); + PutMethod gm = new PutMethod(uri); + + if (attachments == null) { + + + // not a multi-part request + + addParams(gm, parameters); + + } else { + + // multi-part request + + prepareMultiPart(gm, attachments, parameters); + } + return gm; + } + + protected void addParams(HttpMethodBase gm, String... parameters) { + if (parameters.length > 0) { + + // add parameters + + int nbParams = parameters.length / 2; + NameValuePair[] params = new NameValuePair[nbParams]; + for (int i = 0; i < nbParams; i++) { + String key = parameters[2 * i]; + String value = parameters[2 * i + 1]; + if (value == null) { if (log.isDebugEnabled()) { - log.debug("add parameter [" + key + "]=" + value); + log.debug("skip null parameter " + key); } + continue; } + params[i] = new NameValuePair(key, value); } + gm.setQueryString(params); + } + } - // add file parts - for (Entry<String, File> entry : attachments.entrySet()) { - String key = entry.getKey(); - File file = entry.getValue(); + protected void prepareMultiPart(EntityEnclosingMethod gm, + Map<String, File> attachments, + String... parameters) throws IOException { + + List<Part> paramParts = new ArrayList<Part>(); + + if (parameters.length > 0) { + + // add parameters as part of multi-part + + int nbParams = parameters.length / 2; + for (int i = 0; i < nbParams; i++) { + String key = parameters[2 * i]; + String value = parameters[2 * i + 1]; + if (value == null) { + if (log.isDebugEnabled()) { + log.debug("skip null parameter " + key); + } + continue; + } + paramParts.add(new StringPart(key, value)); if (log.isDebugEnabled()) { - log.debug("add attachment " + key + "=" + file); + log.debug("add parameter [" + key + "]=" + value); } - paramParts.add(new FilePart( - key, - file.getName(), - file, - FilePart.DEFAULT_CONTENT_TYPE, - configuration.getEncoding()) - ); } - if (attachments.isEmpty()) { - log.warn("no attachment in a multi-part request!"); - } + } - Part[] parts = paramParts.toArray(new Part[paramParts.size()]); - - MultipartRequestEntity entity = - new MultipartRequestEntity(parts, gm.getParams()); + // add file parts + for (Entry<String, File> entry : attachments.entrySet()) { + String key = entry.getKey(); + File file = entry.getValue(); if (log.isDebugEnabled()) { - entity.writeRequest(System.out); + log.debug("add attachment " + key + "=" + file); } - gm.setRequestEntity(entity); -// gm.addRequestHeader("content-type", "multipart/form-data"); - gm.addRequestHeader("content-type", entity.getContentType()); - gm.addRequestHeader("content-length", entity.getContentLength() + - ""); - if (showRequest) { - log.info("content-type : " + entity.getContentType() + - ", content-length : " + entity.getContentLength()); - } + paramParts.add(new FilePart( + key, + file.getName(), + file, + FilePart.DEFAULT_CONTENT_TYPE, + configuration.getEncoding()) + ); + } + if (attachments.isEmpty()) { + log.warn("no attachment in a multi-part request!"); + } + Part[] parts = paramParts.toArray(new Part[paramParts.size()]); + + MultipartRequestEntity entity = + new MultipartRequestEntity(parts, gm.getParams()); + if (log.isDebugEnabled()) { + entity.writeRequest(System.out); } + gm.setRequestEntity(entity); +// gm.addRequestHeader("content-type", "multipart/form-data"); + gm.addRequestHeader("content-type", entity.getContentType()); + gm.addRequestHeader("content-length", entity.getContentLength() + + ""); + if (showRequest) { + log.info("content-type : " + entity.getContentType() + + ", content-length : " + entity.getContentLength()); + } - client.executeMethod(gm); - - return gm; } }