Author: tchemit Date: 2012-07-18 02:14:43 +0200 (Wed, 18 Jul 2012) New Revision: 856 Url: http://nuiton.org/repositories/revision/maven-helper-plugin/856 Log: fixes #2196: Improve RequestBuilder design Modified: trunk/src/main/java/org/nuiton/io/rest/AbstractRequestFactory.java trunk/src/main/java/org/nuiton/io/rest/RestRequestBuilder.java Modified: trunk/src/main/java/org/nuiton/io/rest/AbstractRequestFactory.java =================================================================== --- trunk/src/main/java/org/nuiton/io/rest/AbstractRequestFactory.java 2012-07-17 22:52:31 UTC (rev 855) +++ trunk/src/main/java/org/nuiton/io/rest/AbstractRequestFactory.java 2012-07-18 00:14:43 UTC (rev 856) @@ -23,10 +23,15 @@ * #L% */ +import com.google.common.base.Joiner; +import com.google.common.base.Preconditions; import com.google.common.collect.Maps; +import org.apache.commons.lang3.ArrayUtils; +import java.io.File; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Map; /** @@ -48,6 +53,7 @@ } /** {@inheritDoc} */ + @Override public void addRequestBuilder(RestRequestBuilder builder) { String name = builder.getName(); if (requestBuilders.containsKey(name)) { @@ -58,6 +64,7 @@ } /** {@inheritDoc} */ + @Override public RestRequest getRequest(String id, Object... args) { RestRequest r; @@ -78,10 +85,132 @@ } /** {@inheritDoc} */ + @Override public RestRequestBuilder getRequestBuilder(String id) { RestRequestBuilder builder = requestBuilders.get(id); return builder; } + /** + * Abstract implementation of a {@link RestRequestBuilder}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ + protected static abstract class AbstractRequestBuilder implements RestRequestBuilder { + + private static final long serialVersionUID = 1L; + + /** + * Name of the request builder. + * + * @since 1.5 + */ + protected String name; + + /** + * Actions of the request (will by default give path of the request). + * + * @since 1.5 + */ + protected String[] action; + + /** + * REST Method to use for this request. + * + * @see RestMethod + * @since 1.5 + */ + protected final RestMethod method; + + public AbstractRequestBuilder(String name, + RestMethod method, + String... action) { + this.name = name; + this.action = action; + this.method = method; + } + + @Override + public String getName() { + return name; + } + + @Override + public String[] getParameters(Object... args) { + // by default, no parameter + return ArrayUtils.EMPTY_STRING_ARRAY; + } + + @Override + public String[] getPath(Object... args) { + // by default, path is extactly action + return action; + } + + @Override + public Map<String, File> getAttachments(Object... args) { + // by default, no attachments + return null; + } + + @Override + public RestRequest create(final Object... args) { + + checkRequestArgs(args); + + return new RestRequest() { + + @Override + public String[] getPath() { + return AbstractRequestBuilder.this.getPath(args); + } + + @Override + public String[] getParameters() { + return AbstractRequestBuilder.this.getParameters(args); + } + + @Override + public Map<String, File> getAttachments() { + return AbstractRequestBuilder.this.getAttachments(args); + } + + @Override + public boolean containsAttachments() { + Map<String, File> attachments = getAttachments(); + return attachments != null && !attachments.isEmpty(); + } + + @Override + public String toPath(String redmineUrl) { + String result = redmineUrl + "/" + + Joiner.on('/').join(getPath()); + return result; + } + + @Override + public RestMethod getMethod() { + return method; + } + + }; + } + + protected void checkRequestArgs(int nbRequires, + String requires, + Object... args) { + Preconditions.checkState( + args.length == nbRequires, + nbRequires == 0 ? + "The request " + getName() + " requires no arg " + + "but had: " + Arrays.toString(args) + : + "The request " + getName() + " requires " + + nbRequires + " args: " + "(" + requires + "), but had: " + + Arrays.toString(args)); + } + } + } Modified: trunk/src/main/java/org/nuiton/io/rest/RestRequestBuilder.java =================================================================== --- trunk/src/main/java/org/nuiton/io/rest/RestRequestBuilder.java 2012-07-17 22:52:31 UTC (rev 855) +++ trunk/src/main/java/org/nuiton/io/rest/RestRequestBuilder.java 2012-07-18 00:14:43 UTC (rev 856) @@ -25,7 +25,9 @@ package org.nuiton.io.rest; +import java.io.File; import java.io.Serializable; +import java.util.Map; /** * The contract of a request builder. @@ -39,10 +41,48 @@ String getName(); /** + * Checks that args used to create the request are fine. + * <p/> + * This method should be invoked just at the begin of the + * method {@link #create(Object...)}. + * + * @param args the args of the request + * @since 1.5 + */ + void checkRequestArgs(Object... args); + + /** * Create the request given the {@code args}. * * @param args args to create the request * @return the created request */ RestRequest create(Object... args); + + /** + * Computes the parameters associated to this requet. + * + * @param args the args of the request + * @return parameters to use for this request + * @since 1.5 + */ + String[] getParameters(Object... args); + + /** + * Computes the path associated to this request. + * + * @param args the args of the request + * @return path to use for this request + * @since 1.5 + */ + String[] getPath(Object... args); + + /** + * Computes the map of attachments associated to this request + * + * @param args the args of the request + * @return map of attachments to use for this request + * @since 1.5 + */ + Map<String, File> getAttachments(Object... args); }