r364 - in trunk: jredmine-client jredmine-client/src/it/redmine-1.4.x jredmine-client/src/it/redmine-2.0.x jredmine-client/src/it/redmine-2.1.x jredmine-client/src/main/java/org/nuiton/jredmine jredmine-client/src/main/java/org/nuiton/jredmine/client jredmine-client/src/main/java/org/nuiton/jredmine/rest jredmine-client/src/test/java/org/nuiton/jredmine jredmine-client/src/test/java/org/nuiton/jredmine/client jredmine-client/src/test/java/org/nuiton/jredmine/rest jredmine-maven-plugin jre
Author: tchemit Date: 2012-10-13 23:50:04 +0200 (Sat, 13 Oct 2012) New Revision: 364 Url: http://nuiton.org/repositories/revision/jredmine/364 Log: refs #2354: Do not use any longer helper-m-p rest api but simple httpcomponents client Added: trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/ trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClient.java trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClientAuthConfiguration.java trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClientConfiguration.java trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineRequestFactory.java trunk/jredmine-client/src/test/java/org/nuiton/jredmine/client/ trunk/jredmine-client/src/test/java/org/nuiton/jredmine/client/RedmineClientTest.java trunk/jredmine-client/src/test/java/org/nuiton/jredmine/client/RedmineRequestFactoryTest.java Removed: trunk/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRequestFactory.java trunk/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRestClient.java trunk/jredmine-client/src/test/java/org/nuiton/jredmine/rest/RedmineRequestFactoryTest.java trunk/jredmine-client/src/test/java/org/nuiton/jredmine/rest/RedmineRestClientTest.java Modified: trunk/jredmine-client/pom.xml trunk/jredmine-client/src/it/redmine-1.4.x/pom.xml trunk/jredmine-client/src/it/redmine-2.0.x/pom.xml trunk/jredmine-client/src/it/redmine-2.1.x/pom.xml trunk/jredmine-client/src/main/java/org/nuiton/jredmine/DefaultRedmineServiceImplementor.java trunk/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineConfigurationUtil.java trunk/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineServiceConfiguration.java trunk/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineServiceImplementor.java trunk/jredmine-client/src/test/java/org/nuiton/jredmine/RedmineFixtures.java trunk/jredmine-maven-plugin/pom.xml trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/AbstractRedmineMojo.java trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/DisplayDataMojo.java trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/GenerateChangesMojo.java trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/LoginMojo.java trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/NextVersionMojo.java trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/PublishAttachmentsMojo.java trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/PublishNewsMojo.java trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/UpdateVersionMojo.java trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AbstractAnnouncementMojo.java trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AnnouncementGenerator.java trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AnnouncementGeneratorConfiguration.java trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/report/AbstractRedmineReport.java trunk/jredmine-maven-plugin/src/test/java/org/nuiton/jredmine/plugin/AbstractRedmineMojoTest.java Modified: trunk/jredmine-client/pom.xml =================================================================== --- trunk/jredmine-client/pom.xml 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-client/pom.xml 2012-10-13 21:50:04 UTC (rev 364) @@ -21,7 +21,9 @@ #L% --> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> @@ -56,6 +58,10 @@ <groupId>velocity</groupId> <artifactId>velocity</artifactId> </exclusion> + <exclusion> + <groupId>commons-httpclient</groupId> + <artifactId>commons-httpclient</artifactId> + </exclusion> </exclusions> </dependency> @@ -71,11 +77,21 @@ </dependency> <dependency> - <groupId>commons-httpclient</groupId> - <artifactId>commons-httpclient</artifactId> + <groupId>commons-collections</groupId> + <artifactId>commons-collections</artifactId> </dependency> <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + </dependency> + + <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpmime</artifactId> + </dependency> + + <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> </dependency> @@ -163,7 +179,7 @@ </plugins> </reporting> - + </profile> <profile> <id>assembly-profile</id> @@ -196,8 +212,10 @@ <descriptorRef>full</descriptorRef> </descriptorRefs> <descriptors> - <descriptor>src/main/assembly/jredmine_rails-1.x.xml</descriptor> - <descriptor>src/main/assembly/jredmine_rails-2.x.xml</descriptor> + <descriptor>src/main/assembly/jredmine_rails-1.x.xml + </descriptor> + <descriptor>src/main/assembly/jredmine_rails-2.x.xml + </descriptor> </descriptors> </configuration> </plugin> @@ -225,7 +243,8 @@ <mergeUserSettings>true</mergeUserSettings> <cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo> <settingsFile>src/it/settings.xml</settingsFile> - <localRepositoryPath>${project.build.directory}/local-repo</localRepositoryPath> + <localRepositoryPath>${project.build.directory}/local-repo + </localRepositoryPath> </configuration> <executions> <execution> Modified: trunk/jredmine-client/src/it/redmine-1.4.x/pom.xml =================================================================== --- trunk/jredmine-client/src/it/redmine-1.4.x/pom.xml 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-client/src/it/redmine-1.4.x/pom.xml 2012-10-13 21:50:04 UTC (rev 364) @@ -89,8 +89,9 @@ <phase>initialize</phase> <configuration> <serverId>jredmine-test-it</serverId> - <usernameOut>jredmine-test.login</usernameOut> + <usernameOut>jredmine-test.username</usernameOut> <passwordOut>jredmine-test.password</passwordOut> + <privateKeyOut>jredmine-test.apiKey</privateKeyOut> </configuration> </execution> </executions> Modified: trunk/jredmine-client/src/it/redmine-2.0.x/pom.xml =================================================================== --- trunk/jredmine-client/src/it/redmine-2.0.x/pom.xml 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-client/src/it/redmine-2.0.x/pom.xml 2012-10-13 21:50:04 UTC (rev 364) @@ -89,8 +89,9 @@ <phase>initialize</phase> <configuration> <serverId>jredmine-test-it</serverId> - <usernameOut>jredmine-test.login</usernameOut> + <usernameOut>jredmine-test.username</usernameOut> <passwordOut>jredmine-test.password</passwordOut> + <privateKeyOut>jredmine-test.apiKey</privateKeyOut> </configuration> </execution> </executions> Modified: trunk/jredmine-client/src/it/redmine-2.1.x/pom.xml =================================================================== --- trunk/jredmine-client/src/it/redmine-2.1.x/pom.xml 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-client/src/it/redmine-2.1.x/pom.xml 2012-10-13 21:50:04 UTC (rev 364) @@ -89,8 +89,9 @@ <phase>initialize</phase> <configuration> <serverId>jredmine-test-it</serverId> - <usernameOut>jredmine-test.login</usernameOut> + <usernameOut>jredmine-test.username</usernameOut> <passwordOut>jredmine-test.password</passwordOut> + <privateKeyOut>jredmine-test.apiKey</privateKeyOut> </configuration> </execution> </executions> Modified: trunk/jredmine-client/src/main/java/org/nuiton/jredmine/DefaultRedmineServiceImplementor.java =================================================================== --- trunk/jredmine-client/src/main/java/org/nuiton/jredmine/DefaultRedmineServiceImplementor.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-client/src/main/java/org/nuiton/jredmine/DefaultRedmineServiceImplementor.java 2012-10-13 21:50:04 UTC (rev 364) @@ -25,12 +25,11 @@ package org.nuiton.jredmine; import com.google.common.base.Preconditions; -import org.nuiton.io.rest.RestClient; -import org.nuiton.io.rest.RestException; import org.nuiton.io.rest.RestRequest; import org.nuiton.jredmine.model.io.xpp3.RedmineXpp3Helper; -import org.nuiton.jredmine.rest.RedmineRestClient; +import org.nuiton.jredmine.client.RedmineClient; +import java.io.IOException; import java.io.InputStream; /** @@ -44,8 +43,12 @@ */ public class DefaultRedmineServiceImplementor implements RedmineServiceImplementor { - /** Redmine Client Rest. */ - protected RestClient session; + /** + * Redmine client. + * + * @since 1.5 + */ + protected RedmineClient client; /** xpp3 xpp3Helper to transform xml stream to pojo */ protected final RedmineXpp3Helper xpp3Helper; @@ -70,13 +73,10 @@ public RedmineServiceImplementor init(RedmineServiceConfiguration configuration) throws RedmineServiceException { Preconditions.checkNotNull(configuration, "the client configuration was not be null!"); Preconditions.checkState(!init, "the client " + this + " was already init!"); - session = new RedmineRestClient(); - session.setConfiguration(configuration); + client = new RedmineClient(configuration); try { - if (!session.isOpen()) { - session.open(); - } + client.open(); init = true; } catch (Exception e) { @@ -90,18 +90,18 @@ public void destroy() throws RedmineServiceException { if (isInit()) { try { - if (session.isOpen()) { + if (client.isOpen()) { try { - session.close(); - } catch (RestException ex) { + client.close(); + } catch (IOException e) { throw new RedmineServiceException( "has problem while closing Rest client " + - ex.getMessage(), ex); + e.getMessage(), e); } } } finally { - if (session != null) { - session = null; + if (client != null) { + client = null; } init = false; } @@ -109,8 +109,8 @@ } @Override - public RestClient getSession() { - return session; + public RedmineClient getClient() { + return client; } @Override @@ -156,25 +156,14 @@ @Override public void checkLoggued() throws IllegalStateException, RedmineServiceLoginException, NullPointerException { checkInit(); - checkSessionNotNull(session); - checkSessionConfigurationNotNull(session); - if (session.getConfiguration().isAnonymous()) { + checkSessionNotNull(); + checkSessionConfigurationNotNull(); + if (client.getConfiguration().isAnonymous()) { throw new RedmineServiceLoginException( "can not access this service in anonymous mode"); } } - @Override - public void checkNotLoggued(RestClient session) throws RedmineServiceLoginException, NullPointerException { -// checkInit(); - checkSessionNotNull(session); - checkSessionConfigurationNotNull(session); - if (!session.getConfiguration().isAnonymous()) { - throw new RedmineServiceLoginException( - "session must be anonymous in this service"); - } - } - protected InputStream askDataStream(String requestName, Object... args) throws RedmineServiceException { @@ -183,7 +172,7 @@ // obtain data from rest client try { - InputStream stream = session.askData(r); + InputStream stream = client.executeRequest(r); return stream; } catch (Exception e) { throw new RedmineServiceException( @@ -200,7 +189,7 @@ // obtain data from rest client try { - InputStream stream = session.sendData(r); + InputStream stream = client.executeRequest(r); return stream; } catch (Exception e) { throw new RedmineServiceException( @@ -213,7 +202,7 @@ Object... args) throws RedmineServiceException { RestRequest r; try { - r = session.getRequest(requestName, args); + r = client.getRequest(requestName, args); } catch (Exception e) { throw new RedmineServiceException( "could not find the request named " + requestName + @@ -263,14 +252,14 @@ } } - protected void checkSessionNotNull(RestClient session) { - if (session == null) { + protected void checkSessionNotNull() { + if (client == null) { throw new NullPointerException("session can not be null"); } } - protected void checkSessionConfigurationNotNull(RestClient session) { - if (session.getConfiguration() == null) { + protected void checkSessionConfigurationNotNull() { + if (client.getConfiguration() == null) { throw new NullPointerException( "session configuration can not be null"); } Modified: trunk/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineConfigurationUtil.java =================================================================== --- trunk/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineConfigurationUtil.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineConfigurationUtil.java 2012-10-13 21:50:04 UTC (rev 364) @@ -31,8 +31,8 @@ import org.apache.commons.lang3.builder.ToStringStyle; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.nuiton.io.rest.RestException; -import org.nuiton.jredmine.rest.RedmineRestClient; +import org.nuiton.jredmine.client.RedmineClient; +import org.nuiton.jredmine.client.RedmineClientAuthConfiguration; import java.io.File; import java.io.IOException; @@ -64,7 +64,7 @@ boolean ok = false; - RedmineRestClient client = new RedmineRestClient(conf); + RedmineClient client = new RedmineClient(conf); try { @@ -79,7 +79,7 @@ } finally { try { client.close(); - } catch (RestException e) { + } catch (IOException e) { if (log.isErrorEnabled()) { log.error("Could not close session", e); } @@ -91,7 +91,7 @@ // try to connect anonymous conf = cloneConfiguration(anoConf); - client = new RedmineRestClient(conf); + client = new RedmineClient(conf); try { @@ -105,7 +105,7 @@ } finally { try { client.close(); - } catch (RestException e) { + } catch (IOException e) { if (log.isErrorEnabled()) { log.error("Could not close session", e); } @@ -156,11 +156,14 @@ RedmineServiceConfiguration result = new SimpleRedmineServiceConfiguration(); - overridePropertyFromProperties(result, "url", "restUrl", props); + RedmineClientAuthConfiguration authConfiguration = + new RedmineClientAuthConfiguration(); + result.setAuthConfiguration(authConfiguration); + overridePropertyFromProperties(result, "url", props); overridePropertyFromProperties(result, "encoding", props); overridePropertyFromProperties(result, "verbose", props); - overridePropertyFromEnv(result, "url", "restUrl"); + overridePropertyFromEnv(result, "url"); overridePropertyFromEnv(result, "encoding"); overridePropertyFromEnv(result, "verbose"); result.setAnonymous(true); @@ -174,9 +177,11 @@ RedmineServiceConfiguration result = cloneConfiguration(anoConf); - overridePropertyFromEnv(result, "login", "restUsername"); - overridePropertyFromEnv(result, "password", "restPassword"); - overridePropertyFromEnv(result, "apiKey", "apiKey"); + RedmineClientAuthConfiguration authConfiguration = + result.getAuthConfiguration(); + overridePropertyFromEnv(authConfiguration, "username"); + overridePropertyFromEnv(authConfiguration, "password"); + overridePropertyFromEnv(authConfiguration, "apiKey"); result.setAnonymous(false); @@ -186,61 +191,45 @@ public static RedmineServiceConfiguration cloneConfiguration( RedmineServiceConfiguration src) { RedmineServiceConfiguration dst = new SimpleRedmineServiceConfiguration(); - dst.setRestUrl(src.getRestUrl()); - dst.setRestUsername(src.getRestUsername()); - dst.setRestPassword(src.getRestPassword()); - dst.setApiKey(src.getApiKey()); - dst.setEncoding(src.getEncoding()); - dst.setVerbose(src.isVerbose()); - dst.setAnonymous(src.isAnonymous()); + copyConfiguration(src, dst); return dst; } public static void copyConfiguration(RedmineServiceConfiguration src, RedmineServiceConfiguration dst) { - dst.setRestUrl(src.getRestUrl()); - dst.setRestUsername(src.getRestUsername()); - dst.setRestPassword(src.getRestPassword()); - dst.setApiKey(src.getApiKey()); + dst.setUrl(src.getUrl()); dst.setEncoding(src.getEncoding()); dst.setVerbose(src.isVerbose()); dst.setAnonymous(src.isAnonymous()); - } - protected static void overridePropertyFromProperties(RedmineServiceConfiguration conf, - String prop, - Properties props) throws IOException { - overridePropertyFromProperties(conf, prop, prop, props); + RedmineClientAuthConfiguration authConfiguration = new RedmineClientAuthConfiguration(); + authConfiguration.setApiKey(src.getAuthConfiguration().getApiKey()); + authConfiguration.setUsername(src.getAuthConfiguration().getUsername()); + authConfiguration.setPassword(src.getAuthConfiguration().getPassword()); + dst.setAuthConfiguration(authConfiguration); } - protected static void overridePropertyFromProperties(RedmineServiceConfiguration conf, + protected static void overridePropertyFromProperties(Object conf, String prop, - String beanProp, Properties props) throws IOException { String value = props.getProperty(PROPERTY_PREFIX + prop); if (StringUtils.isNotEmpty(value)) { try { - BeanUtilsBean.getInstance().setProperty(conf, beanProp, value); + BeanUtilsBean.getInstance().setProperty(conf, prop, value); } catch (Exception e) { throw new IOException( - "Could not set property '" + beanProp + + "Could not set property '" + prop + "' with value '" + value + "' to configuration"); } } } - protected static void overridePropertyFromEnv(RedmineServiceConfiguration conf, + protected static void overridePropertyFromEnv(Object conf, String prop) throws IOException { - overridePropertyFromEnv(conf, prop, prop); - } - - protected static void overridePropertyFromEnv(RedmineServiceConfiguration conf, - String prop, - String beanProp) throws IOException { String value = System.getenv(PROPERTY_PREFIX + prop); if (StringUtils.isNotEmpty(value) && !"null".equals(value)) { try { - BeanUtilsBean.getInstance().setProperty(conf, beanProp, value); + BeanUtilsBean.getInstance().setProperty(conf, prop, value); } catch (Exception e) { throw new IOException( "Could not set property '" + prop + @@ -257,59 +246,65 @@ */ public static class SimpleRedmineServiceConfiguration implements RedmineServiceConfiguration { - URL restUrl; + URL url; - String restUsername; - - String restPassword; - boolean verbose; boolean anonymous; String encoding; - String apiKey; + RedmineClientAuthConfiguration authConfiguration; @Override - public String getEncoding() { - return encoding; + public RedmineClientAuthConfiguration getAuthConfiguration() { + return authConfiguration; } @Override - public void setEncoding(String encoding) { - this.encoding = encoding; + public void setAuthConfiguration(RedmineClientAuthConfiguration authConfiguration) { + this.authConfiguration = authConfiguration; } @Override - public String getRestPassword() { - return restPassword; + public String getEncoding() { + return encoding; } @Override - public void setRestPassword(String restPassword) { - this.restPassword = restPassword; + public void setEncoding(String encoding) { + this.encoding = encoding; } - @Override - public URL getRestUrl() { - return restUrl; - } +// @Override +// public String getPassword() { +// return restPassword; +// } +// +// @Override +// public void setPassword(String password) { +// this.restPassword = password; +// } @Override - public void setRestUrl(URL restUrl) { - this.restUrl = restUrl; + public URL getUrl() { + return url; } @Override - public String getRestUsername() { - return restUsername; + public void setUrl(URL url) { + this.url = url; } - @Override - public void setRestUsername(String restUsername) { - this.restUsername = restUsername; - } +// @Override +// public String getUsername() { +// return restUsername; +// } +// +// @Override +// public void setUsername(String username) { +// this.restUsername = username; +// } @Override public boolean isVerbose() { @@ -337,26 +332,27 @@ this, ToStringStyle.MULTI_LINE_STYLE ); - b.append("redmineUrl", restUrl); + b.append("redmineUrl", url); if (anonymous) { b.append("anonymous", true); } else { - b.append("redmineUsername", restUsername); - b.append("redminePassword", "***"); + b.append("apiKey", authConfiguration.getApiKey()); + b.append("username", authConfiguration.getUsername()); + b.append("password", "***"); } b.append("encoding", encoding); b.append("verbose", verbose); return b.toString(); } - @Override - public String getApiKey() { - return apiKey; - } - - @Override - public void setApiKey(String apiKey) { - this.apiKey = apiKey; - } +// @Override +// public String getApiKey() { +// return apiKey; +// } +// +// @Override +// public void setApiKey(String apiKey) { +// this.apiKey = apiKey; +// } } } Modified: trunk/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineServiceConfiguration.java =================================================================== --- trunk/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineServiceConfiguration.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineServiceConfiguration.java 2012-10-13 21:50:04 UTC (rev 364) @@ -23,7 +23,7 @@ * #L% */ -import org.nuiton.io.rest.RestClientConfiguration; +import org.nuiton.jredmine.client.RedmineClientConfiguration; /** * Contract of a redmine service configuration. @@ -31,10 +31,6 @@ * @author tchemit <chemit@codelutin.com> * @since 1.4 */ -public interface RedmineServiceConfiguration extends RestClientConfiguration { +public interface RedmineServiceConfiguration extends RedmineClientConfiguration { - String getApiKey(); - - void setApiKey(String apiKey); - } \ No newline at end of file Modified: trunk/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineServiceImplementor.java =================================================================== --- trunk/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineServiceImplementor.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-client/src/main/java/org/nuiton/jredmine/RedmineServiceImplementor.java 2012-10-13 21:50:04 UTC (rev 364) @@ -25,15 +25,15 @@ package org.nuiton.jredmine; import org.nuiton.io.rest.RestClient; -import org.nuiton.jredmine.rest.RedmineRestClient; +import org.nuiton.jredmine.client.RedmineClient; /** * Technical contract to implements a redmine service which wrap the - * {@link RedmineRestClient}. + * {@link RedmineClient}. * <p/> * A default implementation is offered in {@link DefaultRedmineServiceImplementor}. * <p/> - * Any concrete implentation of a redmine service should implements this.. + * Any concrete implentation of a redmine service should implements this. * * @author tchemit <chemit@codelutin.com> * @see DefaultRedmineServiceImplementor @@ -70,7 +70,7 @@ * * @return the transport layer. */ - RestClient getSession(); + RedmineClient getClient(); /** * Generic method to obtain a single data from a redmine server. @@ -132,12 +132,4 @@ */ void checkLoggued() throws IllegalStateException, RedmineServiceLoginException, NullPointerException; - /** - * Checks if the current session is anonymous. - * - * @param session the session to test - * @throws RedmineServiceLoginException if not anonymous - * @throws NullPointerException if something is null - */ - void checkNotLoggued(RestClient session) throws RedmineServiceLoginException, NullPointerException; } Added: trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClient.java =================================================================== --- trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClient.java (rev 0) +++ trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClient.java 2012-10-13 21:50:04 UTC (rev 364) @@ -0,0 +1,524 @@ +package org.nuiton.jredmine.client; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.http.HttpEntity; +import org.apache.http.HttpHost; +import org.apache.http.HttpResponse; +import org.apache.http.HttpStatus; +import org.apache.http.StatusLine; +import org.apache.http.client.HttpClient; +import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; +import org.apache.http.client.methods.HttpGet; +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.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.DefaultHttpClient; +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.nuiton.io.rest.RestDataNotFoundException; +import org.nuiton.io.rest.RestException; +import org.nuiton.io.rest.RestRequest; +import org.nuiton.jredmine.model.ModelHelper; + +import java.io.Closeable; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Arrays; +import java.util.Map; + +/** + * A simple redmine client. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class RedmineClient implements Closeable { + + /** Logger. */ + private static final Log log = LogFactory.getLog(RedmineClient.class); + + protected final RedmineClientConfiguration configuration; + + protected final HttpClient client; + + protected final HttpHost host; + + protected final HttpContext clientContext; + + protected final RedmineRequestFactory requestFactory; + + protected boolean showRequest; + + protected boolean open; + + public RedmineClient(RedmineClientConfiguration configuration) { + this.configuration = configuration; + showRequest = configuration.isVerbose(); + client = new DefaultHttpClient(); + clientContext = new BasicHttpContext(); + + int port = configuration.getUrl().getPort(); + if (port == -1) { + + // no port set + host = new HttpHost(configuration.getUrl().getHost()); + } else { + + // a specific port is asked + host = new HttpHost(configuration.getUrl().getHost(), port); + } + + // set encoding (will then encode parameters fine) + client.getParams().setParameter( + CoreProtocolPNames.HTTP_CONTENT_CHARSET, + configuration.getEncoding()); + + requestFactory = new RedmineRequestFactory(); + requestFactory.addDefaultRequests(); + } + + public RedmineRequestFactory getRequestFactory() { + return requestFactory; + } + + public boolean isOpen() { + return open; + } + + public void open() throws RestException, IOException { + + if (!isOpen()) { + + // first ping + ping(); + + if (!configuration.isAnonymous()) { + + // log in + login(); + } + + open = true; + } + } + + /** + * Ask some data from the server + * + * @param request request used for asking data + * @return the stream of the response + * @throws RestException if any pb while asking data + */ + public InputStream executeRequest(RestRequest request) throws RestException { + if (!isOpen()) { + throw new IllegalStateException("the client is not opened"); + } + + try { + if (!open) { + throw new IllegalStateException("the session is not opened"); + } + + HttpRequestBase gm = prepareRequest(request); + + HttpResponse response = executeRequest(gm); + + StatusLine sl = response.getStatusLine(); + int statusCode = sl.getStatusCode(); + + if (log.isDebugEnabled()) { + log.debug("status code " + statusCode + " for " + gm.getURI()); + } + + HttpEntity entity = response.getEntity(); + + if (statusCode == HttpStatus.SC_NOT_FOUND) { + String responseAsString = EntityUtils.toString(entity); + throw new RestDataNotFoundException( + "could not retreave some datas : " + + responseAsString); + } + + if (statusCode != HttpStatus.SC_OK) { + String responseAsString = EntityUtils.toString(entity); + log.error("Error = " + responseAsString); + releaseConnection(); + throw new RestException( + "Got error code <" + statusCode + ":" + + sl.getReasonPhrase() + "> on " + gm.getURI()); + } + InputStream stream = entity.getContent(); + return stream; + } catch (IOException e) { + throw new RestException( + "could not ask data to server to reason " + + e.getMessage(), e); + } + } + + public RedmineClientConfiguration getConfiguration() { + return configuration; + } + + @Override + public void close() throws IOException { + if (open) { + + try { + + releaseConnection(); + + RestRequest request = getRequest(ModelHelper.LOGOUT_REQUEST_NAME); + HttpRequestBase httpRequest = prepareRequest(request); + + HttpResponse response = executeRequest(httpRequest); + EntityUtils.consume(response.getEntity()); + } catch (Exception e) { + throw new IOException( + "could not close client for reason " + + e.getMessage(), e); + } finally { + + try { + + client.getConnectionManager().shutdown(); + } finally { + + open = false; + } + } + } + } + + protected HttpResponse executeRequest(HttpRequestBase gm) throws IOException { + + // always release any previous connection + releaseConnection(); + + HttpResponse response = client.execute(gm, clientContext); + return response; + } + + protected HttpRequestBase prepareRequest(RestRequest request) throws IOException { + + if (showRequest) { + log.info("prepareRequest " + getRequestUrl(request)); + } + if (log.isDebugEnabled()) { + log.debug("prepareRequest with parameters : " + + Arrays.toString(request.getParameters())); + } + + HttpRequestBase 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()); + } + + if (getConfiguration().getAuthConfiguration().isUseApiKey()) { + + gm.addHeader("X-Redmine-API-Key", + getConfiguration().getAuthConfiguration().getApiKey()); + } +// Map<String, String> headers = request.getHeaders(); +// if (MapUtils.isNotEmpty(headers)) { +// +// for (Map.Entry<String, String> entry : headers.entrySet()) { +// gm.addHeader(entry.getKey(), entry.getValue()); +// } +// } + return gm; + } + + protected String getRequestUrl(RestRequest request) { + String uri = host.toURI(); + String result = request.toPath(uri); + return result; + } + + protected HttpGet 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"); + } + + HttpGet gm = new HttpGet(uri); + + addParams(gm, parameters); + + return gm; + } + + protected HttpDelete 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"); + } + + HttpDelete gm = new HttpDelete(uri); + + addParams(gm, parameters); + + return gm; + } + + protected HttpPost preparePostRequest(RestRequest request) throws IOException { + + String uri = getRequestUrl(request); + String[] parameters = request.getParameters(); + Map<String, File> attachments = request.getAttachments(); + + HttpPost gm = new HttpPost(uri); + + if (attachments == null) { + + // not a multi-part request + addParams(gm, parameters); + } else { + + // multi-part request + prepareMultiPart(gm, attachments, parameters); + } + return gm; + } + + protected HttpPut preparePutRequest(RestRequest request) throws IOException { + + String uri = getRequestUrl(request); + String[] parameters = request.getParameters(); + Map<String, File> attachments = request.getAttachments(); + + HttpPut gm = new HttpPut(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(HttpRequestBase gm, + String... parameters) throws IOException { + + if (parameters.length > 0) { + + // add parameters + + URIBuilder uriBuilder = new URIBuilder(gm.getURI()); + 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; + } + uriBuilder.addParameter(key, value); + } + try { + URI uri = uriBuilder.build(); + gm.setURI(uri); + } catch (URISyntaxException e) { + throw new IOException("Could not build uri", e); + } + } + } + + protected void prepareMultiPart(HttpEntityEnclosingRequestBase gm, + Map<String, File> attachments, + String... parameters) throws IOException { + + MultipartEntity entity = new MultipartEntity(); + + 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; + } + entity.addPart(key, new StringBody(value)); + if (log.isDebugEnabled()) { + log.debug("add parameter [" + key + "]=" + value); + } + } + } + + // add file parts + for (Map.Entry<String, File> entry : attachments.entrySet()) { + String key = entry.getKey(); + File file = entry.getValue(); + if (log.isDebugEnabled()) { + log.debug("add attachment " + key + "=" + file); + } + FileBody bin = new FileBody(file, file.getName(), "", + configuration.getEncoding()); + entity.addPart(key, bin); + + } + if (attachments.isEmpty()) { + log.warn("no attachment in a multi-part request!"); + } + + if (log.isDebugEnabled()) { + entity.writeTo(System.out); + } + gm.setEntity(entity); + gm.addHeader("content-type", entity.getContentType().getValue()); + gm.addHeader("content-length", entity.getContentLength() + ""); + if (showRequest) { + log.info("content-type : " + entity.getContentType() + + ", content-length : " + entity.getContentLength()); + } + } + + protected void ping() throws RestException, IOException { + + RestRequest request = getRequest(ModelHelper.PING_REQUEST_NAME); + + HttpRequestBase gm = prepareRequest(request); + + HttpResponse response = executeRequest(gm); + + try { + StatusLine sl = response.getStatusLine(); + int statusCode = sl.getStatusCode(); + if (log.isDebugEnabled()) { + log.debug("status code " + statusCode + " for " + gm.getURI()); + } + + if (statusCode != HttpStatus.SC_OK) { + + throw new RestException( + "Could not ping to " + gm.getURI() + + " statusCode: " + statusCode + ", reason: " + + sl.getReasonPhrase()); + } + + String content = EntityUtils.toString(response.getEntity()); + + boolean ok = "ping".equals(content); + if (!ok) { + throw new IOException( + "can not connect to " + configuration.getUrl()); + } + } finally { + EntityUtils.consume(response.getEntity()); + } + } + + protected void login() throws IOException, RestException { + + RedmineClientAuthConfiguration conf = + getConfiguration().getAuthConfiguration(); + RestRequest request; + String apiKey = conf.getApiKey(); + boolean useApiKey = conf.isUseApiKey(); + if (useApiKey) { + + // use api key to login + request = getRequest(ModelHelper.LOGIN_BY_API_KEY_REQUEST_NAME, + apiKey); + } else { + // use normal login / password + request = getRequest(ModelHelper.LOGIN_REQUEST_NAME, + conf.getUsername(), + conf.getPassword()); + } + + HttpRequestBase gm = prepareRequest(request); + + HttpResponse response = executeRequest(gm); + + try { + StatusLine sl = response.getStatusLine(); + int statusCode = sl.getStatusCode(); + if (log.isDebugEnabled()) { + log.debug("status code " + statusCode + " for " + gm.getURI()); + } + + if (statusCode != HttpStatus.SC_OK) { + throw new RestException( + "Got error code <" + statusCode + ":" + + sl.getReasonPhrase() + "> on " + gm.getURI()); + } + + // ok session is logged in + } finally { + EntityUtils.consume(response.getEntity()); + } + } + + /** + * Obtain a request given his id and the args given. + * + * @param id id of the request + * @param args args passed to build the request + * @return the new request + */ + public RestRequest getRequest(String id, Object... args) { + RestRequest r = requestFactory.getRequest(id, args); + return r; + } + + protected void releaseConnection() { + client.getConnectionManager().closeExpiredConnections(); + } +} Property changes on: trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClient.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClientAuthConfiguration.java =================================================================== --- trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClientAuthConfiguration.java (rev 0) +++ trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClientAuthConfiguration.java 2012-10-13 21:50:04 UTC (rev 364) @@ -0,0 +1,57 @@ +package org.nuiton.jredmine.client; + +import org.apache.commons.lang3.StringUtils; + +/** + * Configuration of the authentication for the {@link RedmineClient}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class RedmineClientAuthConfiguration { + + public static RedmineClientAuthConfiguration newConf(String apiKey, + String username, + String password) { + RedmineClientAuthConfiguration result = + new RedmineClientAuthConfiguration(); + result.setApiKey(apiKey); + result.setUsername(username); + result.setPassword(password); + return result; + } + + protected String username; + + protected String password; + + protected String apiKey; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getApiKey() { + return apiKey; + } + + public void setApiKey(String apiKey) { + this.apiKey = apiKey; + } + + public boolean isUseApiKey() { + return StringUtils.isNotBlank(apiKey); + } +} Property changes on: trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClientAuthConfiguration.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClientConfiguration.java =================================================================== --- trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClientConfiguration.java (rev 0) +++ trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClientConfiguration.java 2012-10-13 21:50:04 UTC (rev 364) @@ -0,0 +1,66 @@ +/* + * #%L + * Maven helper plugin + * + * $Id$ + * $HeadURL: http://svn.nuiton.org/svn/maven-helper-plugin/trunk/src/main/java/org/nuiton... $ + * %% + * Copyright (C) 2009 - 2010 Tony Chemit, CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +package org.nuiton.jredmine.client; + +import java.net.URL; + +/** + * Configuration for a {@link RedmineClient}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public interface RedmineClientConfiguration { + + /** @return base url of server */ + URL getUrl(); + + /** @return the encoding used to encode request to send */ + String getEncoding(); + + /** @return {@code true} to make verbose client (show request and parameters) */ + boolean isVerbose(); + + /** @return {@code true} if rest client does not need login */ + boolean isAnonymous(); + + /** @param url the url of server to set */ + void setUrl(URL url); + + /** @param encoding the encodng used to encode request to set */ + void setEncoding(String encoding); + + /** @param verbose the flag verbose to set */ + void setVerbose(boolean verbose); + + /** @param anonymous the flag anonymous to set */ + void setAnonymous(boolean anonymous); + + RedmineClientAuthConfiguration getAuthConfiguration(); + + void setAuthConfiguration(RedmineClientAuthConfiguration authConfiguration); + +} Property changes on: trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineClientConfiguration.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineRequestFactory.java =================================================================== --- trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineRequestFactory.java (rev 0) +++ trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineRequestFactory.java 2012-10-13 21:50:04 UTC (rev 364) @@ -0,0 +1,450 @@ +package org.nuiton.jredmine.client; +/* + * #%L + * JRedmine :: Client + * $Id$ + * $HeadURL: http://svn.nuiton.org/svn/jredmine/branches/jredmine-1.x/jredmine-client/src... $ + * %% + * Copyright (C) 2009 - 2012 Tony Chemit, CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import com.google.common.base.Strings; +import org.nuiton.io.rest.AbstractRequestFactory; +import org.nuiton.io.rest.RestMethod; +import org.nuiton.io.rest.RestRequestBuilder; +import org.nuiton.jredmine.model.Attachment; +import org.nuiton.jredmine.model.ModelHelper; +import org.nuiton.jredmine.model.News; +import org.nuiton.jredmine.model.TimeEntry; +import org.nuiton.jredmine.model.Version; +import org.nuiton.jredmine.model.VersionStatusEnum; + +import java.io.File; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * Request factory for the redmine client. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.4 + */ +public class RedmineRequestFactory extends AbstractRequestFactory { + + public static String getRequestScope(RestRequestBuilder requestBuilder) { + String scope; + if (requestBuilder instanceof DefaultRequestBuilder) { + + scope = ((DefaultRequestBuilder) requestBuilder).getScope(); + } else { + scope = null; + } + return scope; + } + + @Override + public void addDefaultRequests() { + + // misc requests + + addRequestBuilder(new DefaultRequestBuilder(ModelHelper.PING_REQUEST_NAME, RestMethod.GET, "jredmine", "ping")); + addRequestBuilder(new DefaultRequestBuilder(ModelHelper.LOGOUT_REQUEST_NAME, RestMethod.GET, "jredmine", "logout")); + addRequestBuilder(new DefaultRequestBuilder(ModelHelper.LOGIN_REQUEST_NAME, RestMethod.POST, "jredmine", "login") { + + private static final long serialVersionUID = 1L; + + @Override + public void checkRequestArgs(Object... args) { + checkRequestArgs(2, "userName, password", args); + } + + @Override + public String[] getParameters(Object... args) { + String login = (String) args[0]; + String password = (String) args[1]; + return new String[]{"username", login, "password", password}; + } + }); + + addRequestBuilder(new DefaultRequestBuilder(ModelHelper.LOGIN_BY_API_KEY_REQUEST_NAME, RestMethod.POST, "jredmine", "login") { + + private static final long serialVersionUID = 1L; + + @Override + public void checkRequestArgs(Object... args) { + checkRequestArgs(1, "apiKey", args); + } + +// @Override +// public Map<String, String> getHeaders(Object... args) { +// Map<String, String> headers = Maps.newHashMap(); +// String apiKey = (String) args[0]; +// headers.put("X-Redmine-API-Key", apiKey); +// return headers; +// } + }); + + // data with no scope requests + + addRequestBuilder(new DefaultRequestBuilder(ModelHelper.GET_ALL_PROJECT_REQUEST_NAME, RestMethod.GET, "jredmine", "get_projects.xml")); + addRequestBuilder(new DefaultRequestBuilder(ModelHelper.GET_USER_PROJECTS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_user_projects.xml")); + addRequestBuilder(new DefaultRequestBuilder(ModelHelper.GET_ALL_ISSUE_STATUS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_statuses.xml")); + addRequestBuilder(new DefaultRequestBuilder(ModelHelper.GET_ALL_ISSUE_PRIORITY_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_priorities.xml")); + + // data with project scope requests + + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_PROJECT_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project.xml")); + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_PROJECT_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_issues.xml")); + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_PROJECT_OPENED_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_opened_issues.xml")); + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_PROJECT_CLOSED_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_closed_issues.xml")); + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_VERSION_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_versions.xml")); + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_ISSUE_CATEGORY_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_categories.xml")); + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_TRACKER_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_trackers.xml")); + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_USER_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_users.xml")); + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_NEWS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_news.xml")); + + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.ADD_VERSION_REQUEST_NAME, RestMethod.POST, "jredmine", "add_version.xml") { + private static final long serialVersionUID = 1L; + + @Override + public void checkRequestArgs(Object... args) { + checkRequestArgs(2, "projectName, version", args); + } + + @Override + public String[] getParameters(Object... args) { + Version version = (Version) args[1]; + String date = getVersionEffectiveDate(version); + String status = getVersionStatus(version); + return new String[]{ + "version[name]", version.getName(), + "version[description]", version.getDescription(), + "version[effective_date]", date, + "version[status]", status + }; + } + }); + + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.UPDATE_VERSION_REQUEST_NAME, RestMethod.POST, "jredmine", "update_version.xml") { + private static final long serialVersionUID = 1L; + + @Override + public void checkRequestArgs(Object... args) { + checkRequestArgs(2, "projectName, version", args); + } + + @Override + public String[] getParameters(Object... args) { + Version version = (Version) args[1]; + String date = getVersionEffectiveDate(version); + String status = getVersionStatus(version); + return new String[]{ + "version[name]", version.getName(), + "version[description]", version.getDescription(), + "version[effective_date]", date, + "version[status]", status + }; + } + }); + + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.NEXT_VERSION_REQUEST_NAME, RestMethod.POST, "jredmine", "next_version.xml") { + private static final long serialVersionUID = 1L; + + @Override + public void checkRequestArgs(Object... args) { + checkRequestArgs(3, + "projectName, version, oldVersionName", args); + } + + @Override + public String[] getParameters(Object... args) { + + Version version = (Version) args[1]; + String date = getVersionEffectiveDate(version); + String status = getVersionStatus(version); + String oldVersionName = (String) args[2]; + return new String[]{ + "oldVersionName", oldVersionName, + "version[name]", version.getName(), + "version[description]", version.getDescription(), + "version[effective_date]", date, + "version[status]", status + }; + } + }); + + addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.ADD_NEWS_REQUEST_NAME, RestMethod.POST, "jredmine", "add_news.xml") { + private static final long serialVersionUID = 1L; + + @Override + public void checkRequestArgs(Object... args) { + checkRequestArgs(2, "projectName, news", args); + } + + @Override + public String[] getParameters(Object... args) { + News news = (News) args[1]; + return new String[]{ + "news[title]", news.getTitle(), + "news[summary]", news.getSummary(), + "news[description]", news.getDescription() + }; + } + }); + + // version scope requests + + addRequestBuilder(new VersionScopeRequestBuilder(ModelHelper.GET_VERSION_REQUEST_NAME, RestMethod.GET, "jredmine", "get_version.xml")); + addRequestBuilder(new VersionScopeRequestBuilder(ModelHelper.GET_ALL_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_version_issues.xml")); + addRequestBuilder(new VersionScopeRequestBuilder(ModelHelper.GET_ALL_ATTACHMENTS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_version_attachments.xml")); + addRequestBuilder(new VersionScopeRequestBuilder(ModelHelper.ADD_ATTACHMENT_REQUEST_NAME, RestMethod.POST, "jredmine", "add_version_attachment.xml") { + private static final long serialVersionUID = 1L; + + @Override + public void checkRequestArgs(Object... args) { + checkRequestArgs(3, + "projectName, versionName, attachment", args); + } + + @Override + public String[] getParameters(Object... args) { + String versionId = (String) args[1]; + Attachment attachment = (Attachment) args[2]; + return new String[]{ + "version_name", versionId, + "attachment[description]", attachment.getDescription() + }; + } + + @Override + public Map<String, File> getAttachments(Object... args) { + Map<String, File> upload = new HashMap<String, File>(); + Attachment attachment = (Attachment) args[2]; + upload.put("attachment[file]", attachment.getToUpload()); + return upload; + } + }); + + // issue scope requests + + addRequestBuilder(new IssueScopeRequestBuilder(ModelHelper.GET_ALL_ISSUE_TIME_ENTRY_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_times.xml")); + + addRequestBuilder(new IssueScopeRequestBuilder(ModelHelper.ADD_ISSUE_TIME_ENTRY_REQUEST_NAME, RestMethod.POST, "jredmine", "add_issue_time.xml") { + private static final long serialVersionUID = 1L; + + @Override + public void checkRequestArgs(Object... args) { + checkRequestArgs(3, + "projectName, issueId, timeEntry", args); + } + + @Override + public String[] getParameters(Object... args) { + String issueId = (String) args[1]; + TimeEntry timeEntry = (TimeEntry) args[2]; + Date d = timeEntry.getSpentOn(); + if (d == null) { + d = new Date(); + } + String date = DATE_FORMAT.format(d); + return new String[]{ + "issue_id", issueId, + "time_entry[activity_id]", timeEntry.getActivityId() + "", + "time_entry[spent_on]", date, + "time_entry[hours]", timeEntry.getHours() + "", + "time_entry[comments]", timeEntry.getComments() == null ? "" : timeEntry.getComments() + }; + } + }); + } + + protected static String getVersionStatus(Version version) { + String status = version.getStatus(); + if (Strings.isNullOrEmpty(status)) { + + // use default open status + status = VersionStatusEnum.open.name(); + } + return status; + } + + protected static String getVersionEffectiveDate(Version version) { + return version.getEffectiveDate() == null ? "" : + DATE_FORMAT.format(version.getEffectiveDate()); + } + + /** + * Default {@link RestRequestBuilder} to be used in {@link RedmineClient}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.0.0 + */ + protected static class DefaultRequestBuilder extends AbstractRequestBuilder { + + private static final long serialVersionUID = 1L; + + protected String scope; + + public DefaultRequestBuilder(String name, + RestMethod method, + String... action) { + this(name, null, method, action); + } + + public DefaultRequestBuilder(String name, + String scope, + RestMethod method, + String... action) { + super(name, method, action); + this.scope = scope; + } + + /** + * Gets the scope of this request (scope can be null,project, + * version issue,...). + * + * @return the scope of this request + */ + public final String getScope() { + return scope; + } + + @Override + public void checkRequestArgs(Object... args) { + + // by default request have no ags + checkRequestArgs(0, "", args); + } + } + + /** + * Builder of requests which are in a project context. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.0.0 + */ + protected static class ProjectScopeRequestBuilder extends DefaultRequestBuilder { + + private static final long serialVersionUID = 1L; + + public ProjectScopeRequestBuilder(String name, + RestMethod method, + String... action) { + this(name, ModelHelper.PROJECT_SCOPE, method, action); + } + + public ProjectScopeRequestBuilder(String name, + String scope, + RestMethod method, + String... action) { + super(name, scope, method, action); + } + + @Override + public void checkRequestArgs(Object... args) { + checkRequestArgs(1, "projectName", args); + } + + @Override + public String[] getPath(Object... args) { + + // one args : projectName + + String projectName = (String) args[0]; + + String[] result = new String[action.length + 1]; + System.arraycopy(action, 0, result, 0, action.length); + result[action.length] = projectName; + return result; + } + } + + /** + * Builder of requests which are in a project and issue context. + * <p/> + * Created: 1 janv. 2010 + * + * @author tchemit <chemit@codelutin.com> + * @since 1.0.3 + */ + protected static class IssueScopeRequestBuilder extends ProjectScopeRequestBuilder { + + private static final long serialVersionUID = 1L; + + public IssueScopeRequestBuilder(String name, + RestMethod method, + String... action) { + super(name, ModelHelper.ISSUE_SCOPE, method, action); + } + + @Override + public void checkRequestArgs(Object... args) { + checkRequestArgs(2, "projectName, issueId", args); + } + + @Override + public String[] getParameters(Object... args) { + + // args 1 = project id + // args 2 = issue id + + String issueId = (String) args[1]; + + return new String[]{ + "issue_id", issueId + }; + } + } + + /** + * Builder of requests which are in a project and version context. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.0.0 + */ + protected static class VersionScopeRequestBuilder extends ProjectScopeRequestBuilder { + + private static final long serialVersionUID = 1L; + + public VersionScopeRequestBuilder(String name, + RestMethod method, + String... action) { + super(name, ModelHelper.VERSION_SCOPE, method, action); + } + + @Override + public void checkRequestArgs(Object... args) { + checkRequestArgs(2, "projectName, versionName", args); + } + + @Override + public String[] getParameters(Object... args) { + + // args 1 = project id + // args 2 = version name + + String versionName = (String) args[1]; + + return new String[]{ + "version_name", versionName + }; + } + } + +} Property changes on: trunk/jredmine-client/src/main/java/org/nuiton/jredmine/client/RedmineRequestFactory.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Deleted: trunk/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRequestFactory.java =================================================================== --- trunk/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRequestFactory.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRequestFactory.java 2012-10-13 21:50:04 UTC (rev 364) @@ -1,450 +0,0 @@ -package org.nuiton.jredmine.rest; -/* - * #%L - * JRedmine :: Client - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 Tony Chemit, CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -import com.google.common.base.Strings; -import org.nuiton.io.rest.AbstractRequestFactory; -import org.nuiton.io.rest.RestMethod; -import org.nuiton.io.rest.RestRequestBuilder; -import org.nuiton.jredmine.model.Attachment; -import org.nuiton.jredmine.model.ModelHelper; -import org.nuiton.jredmine.model.News; -import org.nuiton.jredmine.model.TimeEntry; -import org.nuiton.jredmine.model.Version; -import org.nuiton.jredmine.model.VersionStatusEnum; - -import java.io.File; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - -/** - * Request factory for the redmine client. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.4 - */ -public class RedmineRequestFactory extends AbstractRequestFactory { - - public static String getRequestScope(RestRequestBuilder requestBuilder) { - String scope; - if (requestBuilder instanceof DefaultRequestBuilder) { - - scope = ((DefaultRequestBuilder) requestBuilder).getScope(); - } else { - scope = null; - } - return scope; - } - - @Override - public void addDefaultRequests() { - - // misc requests - - addRequestBuilder(new DefaultRequestBuilder(ModelHelper.PING_REQUEST_NAME, RestMethod.GET, "jredmine", "ping")); - addRequestBuilder(new DefaultRequestBuilder(ModelHelper.LOGOUT_REQUEST_NAME, RestMethod.GET, "jredmine", "logout")); - addRequestBuilder(new DefaultRequestBuilder(ModelHelper.LOGIN_REQUEST_NAME, RestMethod.POST, "jredmine", "login") { - - private static final long serialVersionUID = 1L; - - @Override - public void checkRequestArgs(Object... args) { - checkRequestArgs(2, "userName, password", args); - } - - @Override - public String[] getParameters(Object... args) { - String login = (String) args[0]; - String password = (String) args[1]; - return new String[]{"username", login, "password", password}; - } - }); - - addRequestBuilder(new DefaultRequestBuilder(ModelHelper.LOGIN_BY_API_KEY_REQUEST_NAME, RestMethod.POST, "jredmine", "login") { - - private static final long serialVersionUID = 1L; - - @Override - public void checkRequestArgs(Object... args) { - checkRequestArgs(1, "apiKey", args); - } - -// @Override -// public Map<String, String> getHeaders(Object... args) { -// Map<String, String> headers = Maps.newHashMap(); -// String apiKey = (String) args[0]; -// headers.put("X-Redmine-API-Key", apiKey); -// return headers; -// } - }); - - // data with no scope requests - - addRequestBuilder(new DefaultRequestBuilder(ModelHelper.GET_ALL_PROJECT_REQUEST_NAME, RestMethod.GET, "jredmine", "get_projects.xml")); - addRequestBuilder(new DefaultRequestBuilder(ModelHelper.GET_USER_PROJECTS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_user_projects.xml")); - addRequestBuilder(new DefaultRequestBuilder(ModelHelper.GET_ALL_ISSUE_STATUS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_statuses.xml")); - addRequestBuilder(new DefaultRequestBuilder(ModelHelper.GET_ALL_ISSUE_PRIORITY_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_priorities.xml")); - - // data with project scope requests - - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_PROJECT_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project.xml")); - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_PROJECT_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_issues.xml")); - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_PROJECT_OPENED_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_opened_issues.xml")); - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_PROJECT_CLOSED_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_closed_issues.xml")); - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_VERSION_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_versions.xml")); - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_ISSUE_CATEGORY_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_categories.xml")); - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_TRACKER_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_trackers.xml")); - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_USER_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_users.xml")); - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.GET_ALL_NEWS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_project_news.xml")); - - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.ADD_VERSION_REQUEST_NAME, RestMethod.POST, "jredmine", "add_version.xml") { - private static final long serialVersionUID = 1L; - - @Override - public void checkRequestArgs(Object... args) { - checkRequestArgs(2, "projectName, version", args); - } - - @Override - public String[] getParameters(Object... args) { - Version version = (Version) args[1]; - String date = getVersionEffectiveDate(version); - String status = getVersionStatus(version); - return new String[]{ - "version[name]", version.getName(), - "version[description]", version.getDescription(), - "version[effective_date]", date, - "version[status]", status - }; - } - }); - - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.UPDATE_VERSION_REQUEST_NAME, RestMethod.POST, "jredmine", "update_version.xml") { - private static final long serialVersionUID = 1L; - - @Override - public void checkRequestArgs(Object... args) { - checkRequestArgs(2, "projectName, version", args); - } - - @Override - public String[] getParameters(Object... args) { - Version version = (Version) args[1]; - String date = getVersionEffectiveDate(version); - String status = getVersionStatus(version); - return new String[]{ - "version[name]", version.getName(), - "version[description]", version.getDescription(), - "version[effective_date]", date, - "version[status]", status - }; - } - }); - - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.NEXT_VERSION_REQUEST_NAME, RestMethod.POST, "jredmine", "next_version.xml") { - private static final long serialVersionUID = 1L; - - @Override - public void checkRequestArgs(Object... args) { - checkRequestArgs(3, - "projectName, version, oldVersionName", args); - } - - @Override - public String[] getParameters(Object... args) { - - Version version = (Version) args[1]; - String date = getVersionEffectiveDate(version); - String status = getVersionStatus(version); - String oldVersionName = (String) args[2]; - return new String[]{ - "oldVersionName", oldVersionName, - "version[name]", version.getName(), - "version[description]", version.getDescription(), - "version[effective_date]", date, - "version[status]", status - }; - } - }); - - addRequestBuilder(new ProjectScopeRequestBuilder(ModelHelper.ADD_NEWS_REQUEST_NAME, RestMethod.POST, "jredmine", "add_news.xml") { - private static final long serialVersionUID = 1L; - - @Override - public void checkRequestArgs(Object... args) { - checkRequestArgs(2, "projectName, news", args); - } - - @Override - public String[] getParameters(Object... args) { - News news = (News) args[1]; - return new String[]{ - "news[title]", news.getTitle(), - "news[summary]", news.getSummary(), - "news[description]", news.getDescription() - }; - } - }); - - // version scope requests - - addRequestBuilder(new VersionScopeRequestBuilder(ModelHelper.GET_VERSION_REQUEST_NAME, RestMethod.GET, "jredmine", "get_version.xml")); - addRequestBuilder(new VersionScopeRequestBuilder(ModelHelper.GET_ALL_ISSUES_REQUEST_NAME, RestMethod.GET, "jredmine", "get_version_issues.xml")); - addRequestBuilder(new VersionScopeRequestBuilder(ModelHelper.GET_ALL_ATTACHMENTS_REQUEST_NAME, RestMethod.GET, "jredmine", "get_version_attachments.xml")); - addRequestBuilder(new VersionScopeRequestBuilder(ModelHelper.ADD_ATTACHMENT_REQUEST_NAME, RestMethod.POST, "jredmine", "add_version_attachment.xml") { - private static final long serialVersionUID = 1L; - - @Override - public void checkRequestArgs(Object... args) { - checkRequestArgs(3, - "projectName, versionName, attachment", args); - } - - @Override - public String[] getParameters(Object... args) { - String versionId = (String) args[1]; - Attachment attachment = (Attachment) args[2]; - return new String[]{ - "version_name", versionId, - "attachment[description]", attachment.getDescription() - }; - } - - @Override - public Map<String, File> getAttachments(Object... args) { - Map<String, File> upload = new HashMap<String, File>(); - Attachment attachment = (Attachment) args[2]; - upload.put("attachment[file]", attachment.getToUpload()); - return upload; - } - }); - - // issue scope requests - - addRequestBuilder(new IssueScopeRequestBuilder(ModelHelper.GET_ALL_ISSUE_TIME_ENTRY_REQUEST_NAME, RestMethod.GET, "jredmine", "get_issue_times.xml")); - - addRequestBuilder(new IssueScopeRequestBuilder(ModelHelper.ADD_ISSUE_TIME_ENTRY_REQUEST_NAME, RestMethod.POST, "jredmine", "add_issue_time.xml") { - private static final long serialVersionUID = 1L; - - @Override - public void checkRequestArgs(Object... args) { - checkRequestArgs(3, - "projectName, issueId, timeEntry", args); - } - - @Override - public String[] getParameters(Object... args) { - String issueId = (String) args[1]; - TimeEntry timeEntry = (TimeEntry) args[2]; - Date d = timeEntry.getSpentOn(); - if (d == null) { - d = new Date(); - } - String date = DATE_FORMAT.format(d); - return new String[]{ - "issue_id", issueId, - "time_entry[activity_id]", timeEntry.getActivityId() + "", - "time_entry[spent_on]", date, - "time_entry[hours]", timeEntry.getHours() + "", - "time_entry[comments]", timeEntry.getComments() == null ? "" : timeEntry.getComments() - }; - } - }); - } - - protected static String getVersionStatus(Version version) { - String status = version.getStatus(); - if (Strings.isNullOrEmpty(status)) { - - // use default open status - status = VersionStatusEnum.open.name(); - } - return status; - } - - protected static String getVersionEffectiveDate(Version version) { - return version.getEffectiveDate() == null ? "" : - DATE_FORMAT.format(version.getEffectiveDate()); - } - - /** - * Default {@link RestRequestBuilder} to be used in {@link RedmineRestClient}. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.0.0 - */ - protected static class DefaultRequestBuilder extends AbstractRequestBuilder { - - private static final long serialVersionUID = 1L; - - protected String scope; - - public DefaultRequestBuilder(String name, - RestMethod method, - String... action) { - this(name, null, method, action); - } - - public DefaultRequestBuilder(String name, - String scope, - RestMethod method, - String... action) { - super(name, method, action); - this.scope = scope; - } - - /** - * Gets the scope of this request (scope can be null,project, - * version issue,...). - * - * @return the scope of this request - */ - public final String getScope() { - return scope; - } - - @Override - public void checkRequestArgs(Object... args) { - - // by default request have no ags - checkRequestArgs(0, "", args); - } - } - - /** - * Builder of requests which are in a project context. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.0.0 - */ - protected static class ProjectScopeRequestBuilder extends DefaultRequestBuilder { - - private static final long serialVersionUID = 1L; - - public ProjectScopeRequestBuilder(String name, - RestMethod method, - String... action) { - this(name, ModelHelper.PROJECT_SCOPE, method, action); - } - - public ProjectScopeRequestBuilder(String name, - String scope, - RestMethod method, - String... action) { - super(name, scope, method, action); - } - - @Override - public void checkRequestArgs(Object... args) { - checkRequestArgs(1, "projectName", args); - } - - @Override - public String[] getPath(Object... args) { - - // one args : projectName - - String projectName = (String) args[0]; - - String[] result = new String[action.length + 1]; - System.arraycopy(action, 0, result, 0, action.length); - result[action.length] = projectName; - return result; - } - } - - /** - * Builder of requests which are in a project and issue context. - * <p/> - * Created: 1 janv. 2010 - * - * @author tchemit <chemit@codelutin.com> - * @since 1.0.3 - */ - protected static class IssueScopeRequestBuilder extends ProjectScopeRequestBuilder { - - private static final long serialVersionUID = 1L; - - public IssueScopeRequestBuilder(String name, - RestMethod method, - String... action) { - super(name, ModelHelper.ISSUE_SCOPE, method, action); - } - - @Override - public void checkRequestArgs(Object... args) { - checkRequestArgs(2, "projectName, issueId", args); - } - - @Override - public String[] getParameters(Object... args) { - - // args 1 = project id - // args 2 = issue id - - String issueId = (String) args[1]; - - return new String[]{ - "issue_id", issueId - }; - } - } - - /** - * Builder of requests which are in a project and version context. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.0.0 - */ - protected static class VersionScopeRequestBuilder extends ProjectScopeRequestBuilder { - - private static final long serialVersionUID = 1L; - - public VersionScopeRequestBuilder(String name, - RestMethod method, - String... action) { - super(name, ModelHelper.VERSION_SCOPE, method, action); - } - - @Override - public void checkRequestArgs(Object... args) { - checkRequestArgs(2, "projectName, versionName", args); - } - - @Override - public String[] getParameters(Object... args) { - - // args 1 = project id - // args 2 = version name - - String versionName = (String) args[1]; - - return new String[]{ - "version_name", versionName - }; - } - } - -} Deleted: trunk/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRestClient.java =================================================================== --- trunk/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRestClient.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-client/src/main/java/org/nuiton/jredmine/rest/RedmineRestClient.java 2012-10-13 21:50:04 UTC (rev 364) @@ -1,173 +0,0 @@ -/* - * #%L - * JRedmine :: Client - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 Tony Chemit, CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ -package org.nuiton.jredmine.rest; - -import org.apache.commons.httpclient.HttpMethod; -import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.StatusLine; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.codehaus.plexus.util.StringUtils; -import org.nuiton.io.rest.RequestFactory; -import org.nuiton.io.rest.RestClient; -import org.nuiton.io.rest.RestClientConfiguration; -import org.nuiton.io.rest.RestRequest; -import org.nuiton.io.rest.RestSession; -import org.nuiton.jredmine.RedmineServiceConfiguration; -import org.nuiton.jredmine.model.ModelHelper; - -import java.io.IOException; - -/** - * Implementation of a {@link RestClient} to access a Redmine server via the - * {@code redmine_rest} rails plugin. - * - * @author tchemit <chemit@codelutin.com> - * @plexus.component role="org.nuiton.io.rest.RestClient" role-hint="redmine" - * @since 1.0.0 - */ -public class RedmineRestClient extends RestClient { - - private static final Log log = LogFactory.getLog(RedmineRestClient.class); - - private final RequestFactory requestFactory; - - public RedmineRestClient() { - requestFactory = new RedmineRequestFactory(); - requestFactory.addDefaultRequests(); - } - - public RedmineRestClient(RestClientConfiguration configuration) { - super(configuration); - requestFactory = new RedmineRequestFactory(); - requestFactory.addDefaultRequests(); - } - - @Override - public RequestFactory getRequestFactory() { - return requestFactory; - } - - @Override - protected void open(RestSession session) throws IOException { - - ping(session); - - if (!configuration.isAnonymous()) { - login(session); - } - } - - @Override - protected void close(RestSession session) throws IOException { - - if (session != null) { - try { - RestRequest request = getRequest(ModelHelper.LOGOUT_REQUEST_NAME); - session.setOpen(false); - session.doRequest(request); - } finally { - session.close(); - } - } - } - - protected void ping(RestSession session) throws IOException { - - try { - - RestRequest request = getRequest(ModelHelper.PING_REQUEST_NAME); - HttpMethod gm = session.doRequest(request); - - StatusLine sl = gm.getStatusLine(); - int statusCode = sl.getStatusCode(); - if (log.isDebugEnabled()) { - log.debug("status code " + statusCode + " for " + gm.getPath()); - } - - if (statusCode != HttpStatus.SC_OK) { - gm.releaseConnection(); - throw new IOException( - "Got error code <" + statusCode + ":" + - sl.getReasonPhrase() + "> on " + gm.getPath()); - } - - String content = gm.getResponseBodyAsString(); - - boolean ok = "ping".equals(content); - if (!ok) { - throw new IOException( - "can not connect to " + configuration.getRestUrl()); - } - } catch (IOException ex) { - throw ex; - } catch (Exception ex) { - throw new IOException( - "could not ping " + configuration.getRestUrl() + - " for reason " + ex.getMessage(), ex); - } - } - - protected void login(RestSession session) throws IOException { - - RestRequest request; - String apiKey = getConfiguration().getApiKey(); - boolean useApiKey = StringUtils.isNotBlank(apiKey); - if (useApiKey) { - - // use api key to login - request = getRequest(ModelHelper.LOGIN_BY_API_KEY_REQUEST_NAME, - apiKey); - } else { - // use normal login / password - request = getRequest(ModelHelper.LOGIN_REQUEST_NAME, - configuration.getRestUsername(), - configuration.getRestPassword()); - } - - HttpMethod gm = session.doRequest(request); - - StatusLine sl = gm.getStatusLine(); - int statusCode = sl.getStatusCode(); - if (log.isDebugEnabled()) { - log.debug("status code " + statusCode + " for " + gm.getPath()); - } - - if (statusCode != HttpStatus.SC_OK) { - gm.releaseConnection(); - throw new IOException( - "Got error code <" + statusCode + ":" + - sl.getReasonPhrase() + "> on " + gm.getPath()); - } - - // ok session is logged in - - } - - @Override - public RedmineServiceConfiguration getConfiguration() { - return (RedmineServiceConfiguration) super.getConfiguration(); - } -} Modified: trunk/jredmine-client/src/test/java/org/nuiton/jredmine/RedmineFixtures.java =================================================================== --- trunk/jredmine-client/src/test/java/org/nuiton/jredmine/RedmineFixtures.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-client/src/test/java/org/nuiton/jredmine/RedmineFixtures.java 2012-10-13 21:50:04 UTC (rev 364) @@ -66,469 +66,6 @@ return "1925"; } -// public List<Attachment> getAttachments() { -// return get(Attachment.class); -// } -// -// public List<Issue> getIssues() { -// return get(Issue.class); -// } -// -// public List<Project> getProjects() { -// return get(Project.class); -// } -// -// public List<Tracker> getTrackers() { -// return get(Tracker.class); -// } -// -// public List<User> getUsers() { -// return get(User.class); -// } -// -// public List<Version> getVersions() { -// return get(Version.class); -// } -// -// public <T> List<T> get(Class<T> modelType) { -// if (model == null) { -// try { -// loadModel(); -// } catch (Exception ex) { -// throw new RuntimeException(ex); -// } -// } -// return (List<T>) model.get(modelType); -// } -// -// public <T> T get(Class<T> type, int pos) { -// List<T> ts = get(type); -// return ts.get(pos); -// } -// -// private void loadModel() -// throws Exception { -// model = ArrayListMultimap.create(); -// -// Attachment tempA; -// tempA = new Attachment(); -// tempA.setAuthorId((Integer) RedmineDataConverter.Integer.convert("4")); -// tempA.setContainerId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempA.setId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempA.setFilesize((Integer) RedmineDataConverter.Integer.convert("411")); -// tempA.setDownloads((Integer) RedmineDataConverter.Integer.convert("0")); -// tempA.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-05T12:56:41+02:00")); -// tempA.setContainerType((String) RedmineDataConverter.Text.convert("Version")); -// tempA.setContentType((String) RedmineDataConverter.Text.convert("application/json")); -// tempA.setDigest((String) RedmineDataConverter.Text.convert("6ea84342c7475c05fb077b4aca832f9a")); -// tempA.setDiskFilename((String) RedmineDataConverter.Text.convert("090905125641_get_issue.json")); -// tempA.setFilename((String) RedmineDataConverter.Text.convert("get_issue.json")); -// model.put(Attachment.class, tempA); -// tempA = new Attachment(); -// tempA.setAuthorId((Integer) RedmineDataConverter.Integer.convert("4")); -// tempA.setContainerId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempA.setId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempA.setFilesize((Integer) RedmineDataConverter.Integer.convert("411")); -// tempA.setDownloads((Integer) RedmineDataConverter.Integer.convert("0")); -// tempA.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-05T12:56:41+02:00")); -// tempA.setContainerType((String) RedmineDataConverter.Text.convert("Version")); -// tempA.setContentType((String) RedmineDataConverter.Text.convert("application/json")); -// tempA.setDigest((String) RedmineDataConverter.Text.convert("6ea84342c7475c05fb077b4aca832f9a")); -// tempA.setDiskFilename((String) RedmineDataConverter.Text.convert("090905125641_get_issue.json2")); -// tempA.setFilename((String) RedmineDataConverter.Text.convert("get_issue.json2")); -// model.put(Attachment.class, tempA); -// -// Issue tempI; -// tempI = new Issue(); -// tempI.setAuthorId((Integer) RedmineDataConverter.Integer.convert("5")); -// tempI.setCategoryId((Integer) RedmineDataConverter.Integer.convert("2")); -// tempI.setDoneRatio((Integer) RedmineDataConverter.Integer.convert("0")); -// tempI.setLockVersion((Integer) RedmineDataConverter.Integer.convert("7")); -// tempI.setPriorityId((Integer) RedmineDataConverter.Integer.convert("4")); -// tempI.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempI.setStatusId((Integer) RedmineDataConverter.Integer.convert("3")); -// tempI.setTrackerId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempI.setFixedVersionId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempI.setId((Integer) RedmineDataConverter.Integer.convert("3")); -// tempI.setParentId((Integer) RedmineDataConverter.Integer.convert("3")); -// tempI.setRootId((Integer) RedmineDataConverter.Integer.convert("3")); -// tempI.setLft((Integer) RedmineDataConverter.Integer.convert("1")); -// tempI.setRgt((Integer) RedmineDataConverter.Integer.convert("2")); -// tempI.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T20:11:52+02:00")); -// tempI.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T00:37:40+02:00")); -// tempI.setStartDate((Date) RedmineDataConverter.Date.convert("2009-09-04")); -// tempI.setDescription((String) RedmineDataConverter.Text.convert("avec une description !")); -// tempI.setSubject((String) RedmineDataConverter.Text.convert("yes!")); -// model.put(Issue.class, tempI); -// tempI = new Issue(); -// tempI.setAuthorId((Integer) RedmineDataConverter.Integer.convert("5")); -// tempI.setCategoryId((Integer) RedmineDataConverter.Integer.convert("2")); -// tempI.setDoneRatio((Integer) RedmineDataConverter.Integer.convert("0")); -// tempI.setLockVersion((Integer) RedmineDataConverter.Integer.convert("7")); -// tempI.setPriorityId((Integer) RedmineDataConverter.Integer.convert("4")); -// tempI.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempI.setStatusId((Integer) RedmineDataConverter.Integer.convert("3")); -// tempI.setTrackerId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempI.setFixedVersionId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempI.setId((Integer) RedmineDataConverter.Integer.convert("4")); -// tempI.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T20:11:52+02:00")); -// tempI.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T00:37:40+02:00")); -// tempI.setStartDate((Date) RedmineDataConverter.Date.convert("2009-09-04")); -// tempI.setDescription((String) RedmineDataConverter.Text.convert("avec une description !2")); -// tempI.setSubject((String) RedmineDataConverter.Text.convert("yes!2")); -// model.put(Issue.class, tempI); -// -// Project tempP; -// tempP = new Project(); -// tempP.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T18:11:54+02:00")); -// tempP.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T18:11:54+02:00")); -// tempP.setIdentifier((String) RedmineDataConverter.Text.convert("one")); -// tempP.setName((String) RedmineDataConverter.Text.convert("one")); -// tempP.setId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempP.setLft((Integer) RedmineDataConverter.Integer.convert("1")); -// tempP.setRgt((Integer) RedmineDataConverter.Integer.convert("2")); -// tempP.setProjectsCount((Integer) RedmineDataConverter.Integer.convert("0")); -// tempP.setStatus((Integer) RedmineDataConverter.Integer.convert("1")); -// tempP.setIsPublic((Boolean) RedmineDataConverter.Boolean.convert("true")); -// model.put(Project.class, tempP); -// tempP = new Project(); -// tempP.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-05T16:22:14+02:00")); -// tempP.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-05T16:22:29+02:00")); -// tempP.setIdentifier((String) RedmineDataConverter.Text.convert("two")); -// tempP.setName((String) RedmineDataConverter.Text.convert("two")); -// tempP.setId((Integer) RedmineDataConverter.Integer.convert("2")); -// tempP.setProjectsCount((Integer) RedmineDataConverter.Integer.convert("0")); -// tempP.setStatus((Integer) RedmineDataConverter.Integer.convert("1")); -// tempP.setIsPublic((Boolean) RedmineDataConverter.Boolean.convert("false")); -// model.put(Project.class, tempP); -// -// Tracker tempT; -// tempT = new Tracker(); -// tempT.setId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempT.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempT.setTrackerId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempT.setPosition((Integer) RedmineDataConverter.Integer.convert("1")); -// tempT.setIsInChlog((Boolean) RedmineDataConverter.Boolean.convert("true")); -// tempT.setIsInRoadmap((Boolean) RedmineDataConverter.Boolean.convert("false")); -// tempT.setName((String) RedmineDataConverter.Text.convert("Anomalie")); -// model.put(Tracker.class, tempT); -// tempT = new Tracker(); -// tempT.setId((Integer) RedmineDataConverter.Integer.convert("2")); -// tempT.setIsInChlog((Boolean) RedmineDataConverter.Boolean.convert("true")); -// tempT.setIsInRoadmap((Boolean) RedmineDataConverter.Boolean.convert("true")); -// tempT.setName((String) RedmineDataConverter.Text.convert("Evolution")); -// tempT.setPosition((Integer) RedmineDataConverter.Integer.convert("2")); -// tempT.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempT.setTrackerId((Integer) RedmineDataConverter.Integer.convert("2")); -// model.put(Tracker.class, tempT); -// tempT = new Tracker(); -// tempT.setId((Integer) RedmineDataConverter.Integer.convert("3")); -// tempT.setIsInChlog((Boolean) RedmineDataConverter.Boolean.convert("false")); -// tempT.setIsInRoadmap((Boolean) RedmineDataConverter.Boolean.convert("false")); -// tempT.setName((String) RedmineDataConverter.Text.convert("Assistance")); -// tempT.setPosition((Integer) RedmineDataConverter.Integer.convert("3")); -// tempT.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempT.setTrackerId((Integer) RedmineDataConverter.Integer.convert("3")); -// model.put(Tracker.class, tempT); -// -// User tempU; -// tempU = new User(); -// tempU.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T17:24:46+02:00")); -// tempU.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T01:23:59+02:00")); -// tempU.setLastLoginOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T01:23:59+02:00")); -// tempU.setId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempU.setMemberId((Integer) RedmineDataConverter.Integer.convert("5")); -// tempU.setRoleId((Integer) RedmineDataConverter.Integer.convert("3")); -// tempU.setStatus((Integer) RedmineDataConverter.Integer.convert("1")); -// tempU.setAdmin((Boolean) RedmineDataConverter.Boolean.convert("true")); -// tempU.setMailNotification((Boolean) RedmineDataConverter.Boolean.convert("true")); -// tempU.setFirstname((String) RedmineDataConverter.Text.convert("Redmine")); -// tempU.setHashedPassword( -// (String) RedmineDataConverter.Text.convert("70c881d4a26984ddce795f6f71817c9cf4480e79")); -// tempU.setLanguage((String) RedmineDataConverter.Text.convert("fr")); -// tempU.setLastname((String) RedmineDataConverter.Text.convert("Admin")); -// tempU.setLogin((String) RedmineDataConverter.Text.convert("admin")); -// tempU.setMail((String) RedmineDataConverter.Text.convert("dummy@codelutin.com")); -// tempU.setIdentityUrl((String) RedmineDataConverter.Text.convert("yo")); -// model.put(User.class, tempU); -// tempU = new User(); -// tempU.setAdmin((Boolean) RedmineDataConverter.Boolean.convert("true")); -// tempU.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T19:49:02+02:00")); -// tempU.setFirstname((String) RedmineDataConverter.Text.convert("tony")); -// tempU.setHashedPassword( -// (String) RedmineDataConverter.Text.convert("8aed1322e5450badb078e1fb60a817a1df25a2ca")); -// tempU.setId((Integer) RedmineDataConverter.Integer.convert("5")); -// tempU.setLanguage((String) RedmineDataConverter.Text.convert("fr")); -// tempU.setLastLoginOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T19:49:38+02:00")); -// tempU.setLastname((String) RedmineDataConverter.Text.convert("chemit2")); -// tempU.setLogin((String) RedmineDataConverter.Text.convert("tchemit2")); -// tempU.setMail((String) RedmineDataConverter.Text.convert("chemit@codelutin.com")); -// tempU.setMailNotification((Boolean) RedmineDataConverter.Boolean.convert("false")); -// tempU.setMemberId((Integer) RedmineDataConverter.Integer.convert("4")); -// tempU.setRoleId((Integer) RedmineDataConverter.Integer.convert("3")); -// tempU.setStatus((Integer) RedmineDataConverter.Integer.convert("1")); -// tempU.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T19:49:38+02:00")); -// model.put(User.class, tempU); -// tempU = new User(); -// tempU.setAdmin((Boolean) RedmineDataConverter.Boolean.convert("false")); -// tempU.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-05T16:24:11+02:00")); -// tempU.setFirstname((String) RedmineDataConverter.Text.convert("dev")); -// tempU.setHashedPassword( -// (String) RedmineDataConverter.Text.convert("70c881d4a26984ddce795f6f71817c9cf4480e79")); -// tempU.setId((Integer) RedmineDataConverter.Integer.convert("7")); -// tempU.setLanguage((String) RedmineDataConverter.Text.convert("fr")); -// tempU.setLastLoginOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T16:34:39+02:00")); -// tempU.setLastname((String) RedmineDataConverter.Text.convert("dev")); -// tempU.setLogin((String) RedmineDataConverter.Text.convert("dev")); -// tempU.setMail((String) RedmineDataConverter.Text.convert("dev3@ynot-home.info")); -// tempU.setMailNotification((Boolean) RedmineDataConverter.Boolean.convert("false")); -// tempU.setMemberId((Integer) RedmineDataConverter.Integer.convert("9")); -// tempU.setRoleId((Integer) RedmineDataConverter.Integer.convert("4")); -// tempU.setStatus((Integer) RedmineDataConverter.Integer.convert("1")); -// tempU.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T16:34:39+02:00")); -// model.put(User.class, tempU); -// -// Version tempV; -// tempV = new Version(); -// tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T02:47:39+02:00")); -// tempV.setDescription((String) RedmineDataConverter.Text.convert("yo")); -// tempV.setId((Integer) RedmineDataConverter.Integer.convert("9")); -// tempV.setName((String) RedmineDataConverter.Text.convert("yor")); -// tempV.setSharing((String) RedmineDataConverter.Text.convert("none")); -// tempV.setStatus((String) RedmineDataConverter.Text.convert("open")); -// tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T02:50:49+02:00")); -// model.put(Version.class, tempV); -// tempV = new Version(); -// tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:05:09+02:00")); -// tempV.setDescription((String) RedmineDataConverter.Text.convert("ysssoye")); -// tempV.setId((Integer) RedmineDataConverter.Integer.convert("13")); -// tempV.setName((String) RedmineDataConverter.Text.convert("rrrrrrrrrouuuuuua")); -// tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:05:09+02:00")); -// model.put(Version.class, tempV); -// tempV = new Version(); -// tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:07:58+02:00")); -// tempV.setDescription((String) RedmineDataConverter.Text.convert("ysssoye")); -// tempV.setId((Integer) RedmineDataConverter.Integer.convert("15")); -// tempV.setName((String) RedmineDataConverter.Text.convert("aaaauuuuuua")); -// tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:07:58+02:00")); -// model.put(Version.class, tempV); -// tempV = new Version(); -// tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T04:12:25+02:00")); -// tempV.setDescription((String) RedmineDataConverter.Text.convert("ysssoyeppppppppppppppppp")); -// tempV.setId((Integer) RedmineDataConverter.Integer.convert("16")); -// tempV.setName((String) RedmineDataConverter.Text.convert("aaaau")); -// tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T04:13:20+02:00")); -// model.put(Version.class, tempV); -// tempV = new Version(); -// tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:05:40+02:00")); -// tempV.setDescription((String) RedmineDataConverter.Text.convert("ysssoye")); -// tempV.setId((Integer) RedmineDataConverter.Integer.convert("14")); -// tempV.setName((String) RedmineDataConverter.Text.convert("aaaaaaaaaarrrrrrrrrouuuuuua")); -// tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:05:40+02:00")); -// model.put(Version.class, tempV); -// tempV = new Version(); -// tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-05T00:39:15+02:00")); -// tempV.setId((Integer) RedmineDataConverter.Integer.convert("5")); -// tempV.setName((String) RedmineDataConverter.Text.convert("2")); -// tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-05T00:39:15+02:00")); -// model.put(Version.class, tempV); -// tempV = new Version(); -// tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T18:13:05+02:00")); -// tempV.setId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempV.setName((String) RedmineDataConverter.Text.convert("1.0.0")); -// tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-04T18:13:05+02:00")); -// model.put(Version.class, tempV); -// tempV = new Version(); -// tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:00:12+02:00")); -// tempV.setDescription((String) RedmineDataConverter.Text.convert("yoye")); -// tempV.setEffectiveDate((Date) RedmineDataConverter.Date.convert("2009-09-06")); -// tempV.setId((Integer) RedmineDataConverter.Integer.convert("11")); -// tempV.setName((String) RedmineDataConverter.Text.convert("yaouuuuuua")); -// tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:00:12+02:00")); -// model.put(Version.class, tempV); -// tempV = new Version(); -// tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T02:50:59+02:00")); -// tempV.setDescription((String) RedmineDataConverter.Text.convert("yoye")); -// tempV.setEffectiveDate((Date) RedmineDataConverter.Date.convert("2009-09-06")); -// tempV.setId((Integer) RedmineDataConverter.Integer.convert("10")); -// tempV.setName((String) RedmineDataConverter.Text.convert("ya")); -// tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T02:54:16+02:00")); -// model.put(Version.class, tempV); -// tempV = new Version(); -// tempV.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:00:37+02:00")); -// tempV.setDescription((String) RedmineDataConverter.Text.convert("ysssoye")); -// tempV.setEffectiveDate((Date) RedmineDataConverter.Date.convert("2009-09-06")); -// tempV.setId((Integer) RedmineDataConverter.Integer.convert("12")); -// tempV.setName((String) RedmineDataConverter.Text.convert("ouuuuuua")); -// tempV.setProjectId((Integer) RedmineDataConverter.Integer.convert("1")); -// tempV.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-06T03:00:42+02:00")); -// model.put(Version.class, tempV); -// -// IssueStatus tempIS; -// tempIS = new IssueStatus(); -// tempIS.setId(1); -// tempIS.setName("Nouveau"); -// tempIS.setPosition(1); -// tempIS.setDefaultDoneRatio(10); -// tempIS.setIsClosed(false); -// tempIS.setIsDefault(true); -// model.put(IssueStatus.class, tempIS); -// tempIS = new IssueStatus(); -// tempIS.setId(2); -// tempIS.setName("Assigné"); -// tempIS.setPosition(2); -// tempIS.setIsClosed(false); -// tempIS.setIsDefault(false); -// model.put(IssueStatus.class, tempIS); -// tempIS = new IssueStatus(); -// tempIS.setId(3); -// tempIS.setName("Résolu"); -// tempIS.setPosition(3); -// tempIS.setIsClosed(false); -// tempIS.setIsDefault(false); -// model.put(IssueStatus.class, tempIS); -// tempIS = new IssueStatus(); -// tempIS.setId(4); -// tempIS.setName("Commentaire"); -// tempIS.setPosition(4); -// tempIS.setIsClosed(false); -// tempIS.setIsDefault(false); -// model.put(IssueStatus.class, tempIS); -// tempIS = new IssueStatus(); -// tempIS.setId(5); -// tempIS.setName("Fermé"); -// tempIS.setPosition(5); -// tempIS.setIsClosed(true); -// tempIS.setIsDefault(false); -// model.put(IssueStatus.class, tempIS); -// tempIS = new IssueStatus(); -// tempIS.setId(6); -// tempIS.setPosition(6); -// tempIS.setName("Rejeté"); -// tempIS.setIsClosed(true); -// tempIS.setIsDefault(false); -// model.put(IssueStatus.class, tempIS); -// -// IssuePriority tempIP; -// tempIP = new IssuePriority(); -// tempIP.setId(3); -// tempIP.setParentId(1); -// tempIP.setProjectId(2); -// tempIP.setName("Bas"); -// tempIP.setPosition(1); -// tempIP.setOpt("IPRI"); -// tempIP.setIsDefault(false); -// tempIP.setActive(true); -// model.put(IssuePriority.class, tempIP); -// tempIP = new IssuePriority(); -// tempIP.setId(4); -// tempIP.setName("Normal"); -// tempIP.setPosition(2); -// tempIP.setOpt("IPRI"); -// tempIP.setIsDefault(true); -// model.put(IssuePriority.class, tempIP); -// tempIP = new IssuePriority(); -// tempIP.setId(5); -// tempIP.setName("Haut"); -// tempIP.setPosition(3); -// tempIP.setOpt("IPRI"); -// tempIP.setIsDefault(false); -// model.put(IssuePriority.class, tempIP); -// tempIP = new IssuePriority(); -// tempIP.setId(6); -// tempIP.setName("Urgent"); -// tempIP.setPosition(4); -// tempIP.setOpt("IPRI"); -// tempIP.setIsDefault(false); -// model.put(IssuePriority.class, tempIP); -// tempIP = new IssuePriority(); -// tempIP.setId(7); -// tempIP.setName("Immédiat"); -// tempIP.setPosition(5); -// tempIP.setOpt("IPRI"); -// tempIP.setIsDefault(false); -// model.put(IssuePriority.class, tempIP); -// -// IssueCategory tempIC; -// tempIC = new IssueCategory(); -// tempIC.setId(1); -// tempIC.setName("categorie one"); -// tempIC.setProjectId(1); -// model.put(IssueCategory.class, tempIC); -// tempIC = new IssueCategory(); -// tempIC.setId(2); -// tempIC.setName("categorie two"); -// tempIC.setProjectId(1); -// model.put(IssueCategory.class, tempIC); -// -// News tempN; -// tempN = new News(); -// tempN.setId(85); -// tempN.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-17T21:50:26+02:00")); -// tempN.setProjectId(1); -// tempN.setAuthorId(4); -// tempN.setCommentsCount(0); -// tempN.setDescription("description"); -// tempN.setSummary("summary"); -// tempN.setTitle("title"); -// model.put(News.class, tempN); -// tempN = new News(); -// tempN.setId(86); -// tempN.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-09-17T21:55:26+02:00")); -// tempN.setProjectId(1); -// tempN.setAuthorId(4); -// tempN.setCommentsCount(0); -// tempN.setDescription("description2"); -// tempN.setSummary("summary2"); -// tempN.setTitle("title2"); -// model.put(News.class, tempN); -// -// TimeEntry tempE; -// -// tempE = new TimeEntry(); -// tempE.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-12-31T23:02:02+01:00")); -// tempE.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-12-31T23:02:02+01:00")); -// tempE.setSpentOn((Date) RedmineDataConverter.Date.convert("2009-12-31")); -// tempE.setId(1); -// tempE.setProjectId(1); -// tempE.setUserId(4); -// tempE.setIssueId(6); -// tempE.setActivityId(8); -// -// tempE.setHours(1); -// tempE.setTmonth(12); -// tempE.setTyear(2009); -// tempE.setTweek(53); -// tempE.setComments("Test"); -// model.put(TimeEntry.class, tempE); -// -// tempE = new TimeEntry(); -// tempE.setCreatedOn((Date) RedmineDataConverter.Datetime.convert("2009-12-31T23:10:01+01:00")); -// tempE.setUpdatedOn((Date) RedmineDataConverter.Datetime.convert("2009-12-31T23:10:01+01:00")); -// tempE.setSpentOn((Date) RedmineDataConverter.Date.convert("2009-12-31")); -// tempE.setId(2); -// tempE.setProjectId(1); -// tempE.setUserId(4); -// tempE.setIssueId(6); -// tempE.setActivityId(9); -// -// tempE.setHours(2); -// tempE.setTmonth(12); -// tempE.setTyear(2009); -// tempE.setTweek(53); -// tempE.setComments("deuxième temps"); -// model.put(TimeEntry.class, tempE); -// } - public Version versionToAdd() { Version version = new Version(); version.setId(TIMESTAMP); Added: trunk/jredmine-client/src/test/java/org/nuiton/jredmine/client/RedmineClientTest.java =================================================================== --- trunk/jredmine-client/src/test/java/org/nuiton/jredmine/client/RedmineClientTest.java (rev 0) +++ trunk/jredmine-client/src/test/java/org/nuiton/jredmine/client/RedmineClientTest.java 2012-10-13 21:50:04 UTC (rev 364) @@ -0,0 +1,278 @@ +/* + * #%L + * JRedmine :: Client + * + * $Id$ + * $HeadURL: http://svn.nuiton.org/svn/jredmine/branches/jredmine-1.x/jredmine-client/src... $ + * %% + * Copyright (C) 2009 - 2012 Tony Chemit, CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ +package org.nuiton.jredmine.client; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codehaus.plexus.util.IOUtil; +import org.junit.After; +import org.junit.Assert; +import org.junit.Assume; +import org.junit.Before; +import org.junit.ClassRule; +import org.junit.Test; +import org.nuiton.io.rest.RestRequest; +import org.nuiton.jredmine.RedmineFixtures; +import org.nuiton.jredmine.RedmineServiceConfiguration; +import org.nuiton.jredmine.model.ModelHelper; +import org.nuiton.jredmine.test.RedmineFixtureClassRule; + +import java.io.InputStream; + +/** + * Tests the {@link RedmineClient}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.5 + */ +public class RedmineClientTest { + + /** Logger. */ + private static final Log log = + LogFactory.getLog(RedmineClientTest.class); + + @ClassRule + public static final RedmineFixtureClassRule<RedmineFixtures> checkConfigRule = + RedmineFixtureClassRule.newFixtures(RedmineFixtures.class); + + protected RedmineFixtures getFixtures() { + return checkConfigRule.getFixtures(); + } + + protected RedmineClient client; + + protected String projectName; + + protected String versionName; + + protected String issueId; + + @Before + public void setUp() throws Exception { + + RedmineFixtures fixtures = checkConfigRule.getFixtures(); + + client = new RedmineClient(getConfiguration()); + client.open(); + + projectName = fixtures.projectName(); + versionName = fixtures.versionName(); + issueId = fixtures.issueId(); + } + + @After + public void tearDown() throws Exception { + + if (client != null) { + client.close(); + } + } + + @Test + public void isOpen() throws Exception { + Assert.assertTrue(client.isOpen()); + } + + @Test + public void isClose() throws Exception { + Assert.assertTrue(client.isOpen()); + client.close(); + Assert.assertFalse(client.isOpen()); + } + + @Test + public void getProjects() throws Exception { + + askData(ModelHelper.GET_ALL_PROJECT_REQUEST_NAME); + } + + @Test + public void getIssuePriorities() throws Exception { + + askData(ModelHelper.GET_ALL_ISSUE_PRIORITY_REQUEST_NAME); + } + + @Test + public void getIssueStatuses() throws Exception { + + askData(ModelHelper.GET_ALL_ISSUE_STATUS_REQUEST_NAME); + } + + @Test + public void getProject() throws Exception { + + askData(ModelHelper.GET_PROJECT_REQUEST_NAME, projectName); + } + + @Test + public void getIssueCategories() throws Exception { + + askData(ModelHelper.GET_ALL_ISSUE_CATEGORY_REQUEST_NAME, projectName); + } + + @Test + public void getTrackers() throws Exception { + + askData(ModelHelper.GET_ALL_TRACKER_REQUEST_NAME, projectName); + } + + @Test + public void getNews() throws Exception { + + askData(ModelHelper.GET_ALL_NEWS_REQUEST_NAME, projectName); + } + + @Test + public void getProjectMembers() throws Exception { + + askData(ModelHelper.GET_ALL_USER_REQUEST_NAME, projectName); + } + + @Test + public void getProjectIssues() throws Exception { + + askData(ModelHelper.GET_ALL_PROJECT_ISSUES_REQUEST_NAME, projectName); + } + + @Test + public void getVersions() throws Exception { + + askData(ModelHelper.GET_ALL_VERSION_REQUEST_NAME, projectName); + } + + @Test + public void getVersion() throws Exception { + + askData(ModelHelper.GET_VERSION_REQUEST_NAME, projectName, versionName); + } + + @Test + public void getVersionIssues() throws Exception { + + askData(ModelHelper.GET_ALL_ISSUES_REQUEST_NAME, projectName, versionName); + } + + @Test + public void getOpenedIssues() throws Exception { + + askData(ModelHelper.GET_ALL_PROJECT_OPENED_ISSUES_REQUEST_NAME, projectName); + } + + @Test + public void getClosedIssues() throws Exception { + + askData(ModelHelper.GET_ALL_PROJECT_CLOSED_ISSUES_REQUEST_NAME, projectName); + } + + @Test + public void getIssueTimeEntries() throws Exception { + + askData(ModelHelper.GET_ALL_ISSUE_TIME_ENTRY_REQUEST_NAME, projectName, issueId); + } + + @Test + public void getAttachments() throws Exception { + + askData(ModelHelper.GET_ALL_ATTACHMENTS_REQUEST_NAME, projectName, versionName); + } + + @Test + public void login() throws Exception { + + Assume.assumeTrue(!getConfiguration().isAnonymous()); + //Assume.assumeTrue(StringUtils.isBlank(getConfiguration().getApiKey())); + client.open(); + } + + @Test + public void loginByApiKey() throws Exception { + + Assume.assumeTrue(!getConfiguration().isAnonymous()); + //Assume.assumeTrue(StringUtils.isNotBlank(getConfiguration().getAuthConfiguration().getApiKey())); + client.open(); + } + + @Test + public void getUserProjects() throws Exception { + + Assume.assumeTrue(!getConfiguration().isAnonymous()); + askData(ModelHelper.GET_USER_PROJECTS_REQUEST_NAME); + } + + @Test + public void addVersion() throws Exception { + Assume.assumeTrue(!getConfiguration().isAnonymous()); + //TODO + } + + @Test + public void addAttachment() throws Exception { + Assume.assumeTrue(!getConfiguration().isAnonymous()); + //TODO + } + + @Test + public void addNews() throws Exception { + Assume.assumeTrue(!getConfiguration().isAnonymous()); + //TODO + } + + @Test + public void updateVersion() throws Exception { + Assume.assumeTrue(!getConfiguration().isAnonymous()); + //TODO + } + + @Test + public void nextVersion() throws Exception { + Assume.assumeTrue(!getConfiguration().isAnonymous()); + //TODO + } + + @Test + public void addIssueTime() throws Exception { + Assume.assumeTrue(!getConfiguration().isAnonymous()); + //TODO + } + + protected void askData(String requestId, Object... params) throws Exception { + + RestRequest request = client.getRequest(requestId, params); + InputStream askData = client.executeRequest(request); + try { + String toString = IOUtil.toString(askData); + if (getConfiguration().isVerbose() && log.isInfoEnabled()) { + log.info(toString); + } + askData.close(); + } finally { + IOUtil.close(askData); + } + } + + protected RedmineServiceConfiguration getConfiguration() { + return checkConfigRule.getConf(); + } +} Property changes on: trunk/jredmine-client/src/test/java/org/nuiton/jredmine/client/RedmineClientTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/jredmine-client/src/test/java/org/nuiton/jredmine/client/RedmineRequestFactoryTest.java =================================================================== --- trunk/jredmine-client/src/test/java/org/nuiton/jredmine/client/RedmineRequestFactoryTest.java (rev 0) +++ trunk/jredmine-client/src/test/java/org/nuiton/jredmine/client/RedmineRequestFactoryTest.java 2012-10-13 21:50:04 UTC (rev 364) @@ -0,0 +1,244 @@ +package org.nuiton.jredmine.client; +/* + * #%L + * JRedmine :: Client + * $Id$ + * $HeadURL: http://svn.nuiton.org/svn/jredmine/branches/jredmine-1.x/jredmine-client/src... $ + * %% + * Copyright (C) 2009 - 2012 Tony Chemit, CodeLutin + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Lesser Public License for more details. + * + * You should have received a copy of the GNU General Lesser Public + * License along with this program. If not, see + * <http://www.gnu.org/licenses/lgpl-3.0.html>. + * #L% + */ + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.nuiton.io.rest.RequestFactory; +import org.nuiton.io.rest.RestRequest; +import org.nuiton.jredmine.RedmineFixtures; +import org.nuiton.jredmine.model.Attachment; +import org.nuiton.jredmine.model.ModelHelper; +import org.nuiton.jredmine.model.News; +import org.nuiton.jredmine.model.TimeEntry; +import org.nuiton.jredmine.model.Version; +import org.nuiton.jredmine.client.RedmineRequestFactory; + +import java.util.Arrays; + +/** + * Tests the {@link RedmineRequestFactory}. + * + * @author tchemit <chemit@codelutin.com> + * @since 1.4 + */ +public class RedmineRequestFactoryTest { + + protected static final RedmineFixtures fixtures = new RedmineFixtures(); + + protected RequestFactory factory; + + protected String projectName; + + protected String versionName; + + protected String issueId; + + @Before + public void setUp() throws Exception { + factory = new RedmineRequestFactory(); + + factory.addDefaultRequests(); + + projectName = fixtures.projectName(); + versionName = fixtures.versionName(); + issueId = fixtures.issueId(); + } + + @Test + public void login() { + assertRequestParameters( + assertRequestUrl("/jredmine/login", ModelHelper.LOGIN_REQUEST_NAME, "redmineuser", "redminepassword"), + "username", "redmineuser", "password", "redminepassword"); + } + + @Test + public void loginByApiKey() { + assertRequestParameters(assertRequestUrl("/jredmine/login", ModelHelper.LOGIN_BY_API_KEY_REQUEST_NAME, "apiKey")); + } + + @Test + public void logout() { + assertRequestParameters(assertRequestUrl("/jredmine/logout", ModelHelper.LOGOUT_REQUEST_NAME)); + } + + @Test + public void ping() { + assertRequestParameters(assertRequestUrl("/jredmine/ping", ModelHelper.PING_REQUEST_NAME)); + } + + @Test + public void getAllProjects() { + assertRequestParameters(assertRequestUrl("/jredmine/get_projects.xml", ModelHelper.GET_ALL_PROJECT_REQUEST_NAME)); + } + + @Test + public void getAllIssueStatuses() { + assertRequestParameters(assertRequestUrl("/jredmine/get_issue_statuses.xml", ModelHelper.GET_ALL_ISSUE_STATUS_REQUEST_NAME)); + } + + @Test + public void getAllIssuePriorities() { + assertRequestParameters(assertRequestUrl("/jredmine/get_issue_priorities.xml", ModelHelper.GET_ALL_ISSUE_PRIORITY_REQUEST_NAME)); + } + + @Test + public void getProject() { + assertRequestParameters(assertRequestUrl("/jredmine/get_project.xml/" + projectName, ModelHelper.GET_PROJECT_REQUEST_NAME, projectName)); + } + + @Test + public void getAllIssues() { + assertRequestParameters(assertRequestUrl("/jredmine/get_project_issues.xml/" + projectName, ModelHelper.GET_ALL_PROJECT_ISSUES_REQUEST_NAME, projectName)); + } + + @Test + public void getAllOpenedIssues() { + assertRequestParameters(assertRequestUrl("/jredmine/get_project_opened_issues.xml/" + projectName, ModelHelper.GET_ALL_PROJECT_OPENED_ISSUES_REQUEST_NAME, projectName)); + } + + @Test + public void getAllClosesIssues() { + assertRequestParameters(assertRequestUrl("/jredmine/get_project_closed_issues.xml/" + projectName, ModelHelper.GET_ALL_PROJECT_CLOSED_ISSUES_REQUEST_NAME, projectName)); + } + + @Test + public void getAllVersions() { + assertRequestParameters(assertRequestUrl("/jredmine/get_project_versions.xml/" + projectName, ModelHelper.GET_ALL_VERSION_REQUEST_NAME, projectName)); + } + + @Test + public void getAllIssueCategories() { + assertRequestParameters(assertRequestUrl("/jredmine/get_issue_categories.xml/" + projectName, ModelHelper.GET_ALL_ISSUE_CATEGORY_REQUEST_NAME, projectName)); + } + + @Test + public void getAllTrackers() { + assertRequestParameters(assertRequestUrl("/jredmine/get_project_trackers.xml/" + projectName, ModelHelper.GET_ALL_TRACKER_REQUEST_NAME, projectName)); + } + + @Test + public void getAllUsers() { + assertRequestParameters(assertRequestUrl("/jredmine/get_project_users.xml/" + projectName, ModelHelper.GET_ALL_USER_REQUEST_NAME, projectName)); + } + + @Test + public void getAllNews() { + assertRequestParameters(assertRequestUrl("/jredmine/get_project_news.xml/" + projectName, ModelHelper.GET_ALL_NEWS_REQUEST_NAME, projectName)); + } + + @Test + public void addVersion() { + Version version = fixtures.versionToAdd(); + assertRequestParameters(assertRequestUrl("/jredmine/add_version.xml/" + projectName, ModelHelper.ADD_VERSION_REQUEST_NAME, projectName, version), + "version[name]", "1.3_" + RedmineFixtures.TIMESTAMP, "version[description]", "Version to add", "version[effective_date]", "", "version[status]", "open"); + } + + @Test + public void updateVersion() { + Version version = fixtures.versionToUpdate(); + assertRequestParameters(assertRequestUrl("/jredmine/update_version.xml/" + projectName, ModelHelper.UPDATE_VERSION_REQUEST_NAME, projectName, version), + "version[name]", "1.3", "version[description]", "Version to update", "version[effective_date]", "", "version[status]", "open"); + } + + @Test + public void nextVersion() { + Version version = fixtures.versionToUpdate(); + String oldVersionName = fixtures.oldVersionName(); + assertRequestParameters(assertRequestUrl("/jredmine/next_version.xml/" + projectName, ModelHelper.NEXT_VERSION_REQUEST_NAME, projectName, version, oldVersionName), + "oldVersionName", oldVersionName, "version[name]", "1.3", "version[description]", "Version to update", "version[effective_date]", "", "version[status]", "open"); + } + + @Test + public void addNews() { + News newsToAdd = fixtures.newsToAdd(); + assertRequestParameters(assertRequestUrl("/jredmine/add_news.xml/" + projectName, ModelHelper.ADD_NEWS_REQUEST_NAME, projectName, newsToAdd), + "news[title]", newsToAdd.getTitle(), + "news[summary]", newsToAdd.getSummary(), + "news[description]", newsToAdd.getDescription()); + } + + @Test + public void getVersion() { + assertRequestParameters(assertRequestUrl("/jredmine/get_version.xml/" + projectName, ModelHelper.GET_VERSION_REQUEST_NAME, projectName, versionName), + "version_name", versionName); + } + + @Test + public void getAllVersionIssues() { + assertRequestParameters(assertRequestUrl("/jredmine/get_version_issues.xml/" + projectName, ModelHelper.GET_ALL_ISSUES_REQUEST_NAME, projectName, versionName), + "version_name", versionName); + } + + @Test + public void getAllAttachments() { + assertRequestParameters(assertRequestUrl("/jredmine/get_version_attachments.xml/" + projectName, ModelHelper.GET_ALL_ATTACHMENTS_REQUEST_NAME, projectName, versionName), + "version_name", versionName); + } + + @Test + public void addAttachment() { + Attachment attachment = fixtures.attachmentToAdd(); + + assertRequestParameters(assertRequestUrl("/jredmine/add_version_attachment.xml/" + projectName, ModelHelper.ADD_ATTACHMENT_REQUEST_NAME, projectName, versionName, attachment), + "version_name", versionName, + "attachment[description]", attachment.getDescription()); + } + + @Test + public void getAllIssueTimeEntires() { + assertRequestParameters(assertRequestUrl("/jredmine/get_issue_times.xml/" + projectName, ModelHelper.GET_ALL_ISSUE_TIME_ENTRY_REQUEST_NAME, projectName, issueId), + "issue_id", issueId); + } + + @Test + public void addIssueTimeEntry() { + TimeEntry timeEntry = fixtures.timeEntryToAdd(); + assertRequestParameters(assertRequestUrl("/jredmine/add_issue_time.xml/" + projectName, ModelHelper.ADD_ISSUE_TIME_ENTRY_REQUEST_NAME, projectName, issueId, timeEntry), + "issue_id", issueId, + "time_entry[activity_id]", timeEntry.getActivityId() + "", + "time_entry[spent_on]", "2012-07-15", + "time_entry[hours]", timeEntry.getHours() + "", + "time_entry[comments]", timeEntry.getComments()); + } + + protected RestRequest assertRequestUrl(String expectedUrl, + String requestId, + Object... args) { + + RestRequest request = factory.getRequest(requestId, args); + + Assert.assertNotNull(request); + String actual = request.toPath(""); + Assert.assertEquals(expectedUrl, actual); + return request; + } + + protected void assertRequestParameters(RestRequest request, + String... expectedParams) { + String[] actual = request.getParameters(); + Assert.assertArrayEquals("Expected:" + Arrays.toString(expectedParams) + " but was:" + Arrays.toString(actual), expectedParams, actual); + } +} Property changes on: trunk/jredmine-client/src/test/java/org/nuiton/jredmine/client/RedmineRequestFactoryTest.java ___________________________________________________________________ Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Deleted: trunk/jredmine-client/src/test/java/org/nuiton/jredmine/rest/RedmineRequestFactoryTest.java =================================================================== --- trunk/jredmine-client/src/test/java/org/nuiton/jredmine/rest/RedmineRequestFactoryTest.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-client/src/test/java/org/nuiton/jredmine/rest/RedmineRequestFactoryTest.java 2012-10-13 21:50:04 UTC (rev 364) @@ -1,243 +0,0 @@ -package org.nuiton.jredmine.rest; -/* - * #%L - * JRedmine :: Client - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 Tony Chemit, CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.nuiton.io.rest.RequestFactory; -import org.nuiton.io.rest.RestRequest; -import org.nuiton.jredmine.RedmineFixtures; -import org.nuiton.jredmine.model.Attachment; -import org.nuiton.jredmine.model.ModelHelper; -import org.nuiton.jredmine.model.News; -import org.nuiton.jredmine.model.TimeEntry; -import org.nuiton.jredmine.model.Version; - -import java.util.Arrays; - -/** - * Tests the {@link RedmineRequestFactory}. - * - * @author tchemit <chemit@codelutin.com> - * @since 1.4 - */ -public class RedmineRequestFactoryTest { - - protected static final RedmineFixtures fixtures = new RedmineFixtures(); - - protected RequestFactory factory; - - protected String projectName; - - protected String versionName; - - protected String issueId; - - @Before - public void setUp() throws Exception { - factory = new RedmineRequestFactory(); - - factory.addDefaultRequests(); - - projectName = fixtures.projectName(); - versionName = fixtures.versionName(); - issueId = fixtures.issueId(); - } - - @Test - public void login() { - assertRequestParameters( - assertRequestUrl("/jredmine/login", ModelHelper.LOGIN_REQUEST_NAME, "redmineuser", "redminepassword"), - "username", "redmineuser", "password", "redminepassword"); - } - - @Test - public void loginByApiKey() { - assertRequestParameters(assertRequestUrl("/jredmine/login", ModelHelper.LOGIN_BY_API_KEY_REQUEST_NAME, "apiKey")); - } - - @Test - public void logout() { - assertRequestParameters(assertRequestUrl("/jredmine/logout", ModelHelper.LOGOUT_REQUEST_NAME)); - } - - @Test - public void ping() { - assertRequestParameters(assertRequestUrl("/jredmine/ping", ModelHelper.PING_REQUEST_NAME)); - } - - @Test - public void getAllProjects() { - assertRequestParameters(assertRequestUrl("/jredmine/get_projects.xml", ModelHelper.GET_ALL_PROJECT_REQUEST_NAME)); - } - - @Test - public void getAllIssueStatuses() { - assertRequestParameters(assertRequestUrl("/jredmine/get_issue_statuses.xml", ModelHelper.GET_ALL_ISSUE_STATUS_REQUEST_NAME)); - } - - @Test - public void getAllIssuePriorities() { - assertRequestParameters(assertRequestUrl("/jredmine/get_issue_priorities.xml", ModelHelper.GET_ALL_ISSUE_PRIORITY_REQUEST_NAME)); - } - - @Test - public void getProject() { - assertRequestParameters(assertRequestUrl("/jredmine/get_project.xml/" + projectName, ModelHelper.GET_PROJECT_REQUEST_NAME, projectName)); - } - - @Test - public void getAllIssues() { - assertRequestParameters(assertRequestUrl("/jredmine/get_project_issues.xml/" + projectName, ModelHelper.GET_ALL_PROJECT_ISSUES_REQUEST_NAME, projectName)); - } - - @Test - public void getAllOpenedIssues() { - assertRequestParameters(assertRequestUrl("/jredmine/get_project_opened_issues.xml/" + projectName, ModelHelper.GET_ALL_PROJECT_OPENED_ISSUES_REQUEST_NAME, projectName)); - } - - @Test - public void getAllClosesIssues() { - assertRequestParameters(assertRequestUrl("/jredmine/get_project_closed_issues.xml/" + projectName, ModelHelper.GET_ALL_PROJECT_CLOSED_ISSUES_REQUEST_NAME, projectName)); - } - - @Test - public void getAllVersions() { - assertRequestParameters(assertRequestUrl("/jredmine/get_project_versions.xml/" + projectName, ModelHelper.GET_ALL_VERSION_REQUEST_NAME, projectName)); - } - - @Test - public void getAllIssueCategories() { - assertRequestParameters(assertRequestUrl("/jredmine/get_issue_categories.xml/" + projectName, ModelHelper.GET_ALL_ISSUE_CATEGORY_REQUEST_NAME, projectName)); - } - - @Test - public void getAllTrackers() { - assertRequestParameters(assertRequestUrl("/jredmine/get_project_trackers.xml/" + projectName, ModelHelper.GET_ALL_TRACKER_REQUEST_NAME, projectName)); - } - - @Test - public void getAllUsers() { - assertRequestParameters(assertRequestUrl("/jredmine/get_project_users.xml/" + projectName, ModelHelper.GET_ALL_USER_REQUEST_NAME, projectName)); - } - - @Test - public void getAllNews() { - assertRequestParameters(assertRequestUrl("/jredmine/get_project_news.xml/" + projectName, ModelHelper.GET_ALL_NEWS_REQUEST_NAME, projectName)); - } - - @Test - public void addVersion() { - Version version = fixtures.versionToAdd(); - assertRequestParameters(assertRequestUrl("/jredmine/add_version.xml/" + projectName, ModelHelper.ADD_VERSION_REQUEST_NAME, projectName, version), - "version[name]", "1.3_" + RedmineFixtures.TIMESTAMP, "version[description]", "Version to add", "version[effective_date]", "", "version[status]", "open"); - } - - @Test - public void updateVersion() { - Version version = fixtures.versionToUpdate(); - assertRequestParameters(assertRequestUrl("/jredmine/update_version.xml/" + projectName, ModelHelper.UPDATE_VERSION_REQUEST_NAME, projectName, version), - "version[name]", "1.3", "version[description]", "Version to update", "version[effective_date]", "", "version[status]", "open"); - } - - @Test - public void nextVersion() { - Version version = fixtures.versionToUpdate(); - String oldVersionName = fixtures.oldVersionName(); - assertRequestParameters(assertRequestUrl("/jredmine/next_version.xml/" + projectName, ModelHelper.NEXT_VERSION_REQUEST_NAME, projectName, version, oldVersionName), - "oldVersionName", oldVersionName, "version[name]", "1.3", "version[description]", "Version to update", "version[effective_date]", "", "version[status]", "open"); - } - - @Test - public void addNews() { - News newsToAdd = fixtures.newsToAdd(); - assertRequestParameters(assertRequestUrl("/jredmine/add_news.xml/" + projectName, ModelHelper.ADD_NEWS_REQUEST_NAME, projectName, newsToAdd), - "news[title]", newsToAdd.getTitle(), - "news[summary]", newsToAdd.getSummary(), - "news[description]", newsToAdd.getDescription()); - } - - @Test - public void getVersion() { - assertRequestParameters(assertRequestUrl("/jredmine/get_version.xml/" + projectName, ModelHelper.GET_VERSION_REQUEST_NAME, projectName, versionName), - "version_name", versionName); - } - - @Test - public void getAllVersionIssues() { - assertRequestParameters(assertRequestUrl("/jredmine/get_version_issues.xml/" + projectName, ModelHelper.GET_ALL_ISSUES_REQUEST_NAME, projectName, versionName), - "version_name", versionName); - } - - @Test - public void getAllAttachments() { - assertRequestParameters(assertRequestUrl("/jredmine/get_version_attachments.xml/" + projectName, ModelHelper.GET_ALL_ATTACHMENTS_REQUEST_NAME, projectName, versionName), - "version_name", versionName); - } - - @Test - public void addAttachment() { - Attachment attachment = fixtures.attachmentToAdd(); - - assertRequestParameters(assertRequestUrl("/jredmine/add_version_attachment.xml/" + projectName, ModelHelper.ADD_ATTACHMENT_REQUEST_NAME, projectName, versionName, attachment), - "version_name", versionName, - "attachment[description]", attachment.getDescription()); - } - - @Test - public void getAllIssueTimeEntires() { - assertRequestParameters(assertRequestUrl("/jredmine/get_issue_times.xml/" + projectName, ModelHelper.GET_ALL_ISSUE_TIME_ENTRY_REQUEST_NAME, projectName, issueId), - "issue_id", issueId); - } - - @Test - public void addIssueTimeEntry() { - TimeEntry timeEntry = fixtures.timeEntryToAdd(); - assertRequestParameters(assertRequestUrl("/jredmine/add_issue_time.xml/" + projectName, ModelHelper.ADD_ISSUE_TIME_ENTRY_REQUEST_NAME, projectName, issueId, timeEntry), - "issue_id", issueId, - "time_entry[activity_id]", timeEntry.getActivityId() + "", - "time_entry[spent_on]", "2012-07-15", - "time_entry[hours]", timeEntry.getHours() + "", - "time_entry[comments]", timeEntry.getComments()); - } - - protected RestRequest assertRequestUrl(String expectedUrl, - String requestId, - Object... args) { - - RestRequest request = factory.getRequest(requestId, args); - - Assert.assertNotNull(request); - String actual = request.toPath(""); - Assert.assertEquals(expectedUrl, actual); - return request; - } - - protected void assertRequestParameters(RestRequest request, - String... expectedParams) { - String[] actual = request.getParameters(); - Assert.assertArrayEquals("Expected:" + Arrays.toString(expectedParams) + " but was:" + Arrays.toString(actual), expectedParams, actual); - } -} Deleted: trunk/jredmine-client/src/test/java/org/nuiton/jredmine/rest/RedmineRestClientTest.java =================================================================== --- trunk/jredmine-client/src/test/java/org/nuiton/jredmine/rest/RedmineRestClientTest.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-client/src/test/java/org/nuiton/jredmine/rest/RedmineRestClientTest.java 2012-10-13 21:50:04 UTC (rev 364) @@ -1,272 +0,0 @@ -/* - * #%L - * JRedmine :: Client - * - * $Id$ - * $HeadURL$ - * %% - * Copyright (C) 2009 - 2012 Tony Chemit, CodeLutin - * %% - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. - * #L% - */ -package org.nuiton.jredmine.rest; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.codehaus.plexus.util.IOUtil; -import org.codehaus.plexus.util.StringUtils; -import org.junit.After; -import org.junit.Assert; -import org.junit.Assume; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Test; -import org.nuiton.io.rest.RestRequest; -import org.nuiton.jredmine.RedmineFixtures; -import org.nuiton.jredmine.RedmineServiceConfiguration; -import org.nuiton.jredmine.model.ModelHelper; -import org.nuiton.jredmine.test.RedmineAnonymousFixtureClassRule; - -import java.io.InputStream; - -/** - * @author tchemit <chemit@codelutin.com> - * @since 1.0.0 - */ -public class RedmineRestClientTest { - - /** Logger. */ - private static final Log log = - LogFactory.getLog(RedmineRestClientTest.class); - - @ClassRule - public static final RedmineAnonymousFixtureClassRule<RedmineFixtures> checkConfigRule = - RedmineAnonymousFixtureClassRule.newFixtures(RedmineFixtures.class); - - protected RedmineFixtures getFixtures() { - return checkConfigRule.getFixtures(); - } - - protected RedmineRestClient client; - - protected String projectName; - - protected String versionName; - - protected String issueId; - - @Before - public void setUp() throws Exception { - - RedmineFixtures fixtures = checkConfigRule.getFixtures(); - - client = new RedmineRestClient(getConfiguration()); - client.open(); - - projectName = fixtures.projectName(); - versionName = fixtures.versionName(); - issueId = fixtures.issueId(); - } - - @After - public void tearDown() throws Exception { - - if (client != null) { - client.close(); - } - } - - @Test - public void isOpen() throws Exception { - Assert.assertTrue(client.isOpen()); - } - - @Test - public void isClose() throws Exception { - Assert.assertTrue(client.isOpen()); - client.close(); - Assert.assertFalse(client.isOpen()); - } - - @Test - public void getProjects() throws Exception { - - askData(ModelHelper.GET_ALL_PROJECT_REQUEST_NAME); - } - - @Test - public void getIssuePriorities() throws Exception { - - askData(ModelHelper.GET_ALL_ISSUE_PRIORITY_REQUEST_NAME); - } - - @Test - public void getIssueStatuses() throws Exception { - - askData(ModelHelper.GET_ALL_ISSUE_STATUS_REQUEST_NAME); - } - - @Test - public void getProject() throws Exception { - - askData(ModelHelper.GET_PROJECT_REQUEST_NAME, projectName); - } - - @Test - public void getIssueCategories() throws Exception { - - askData(ModelHelper.GET_ALL_ISSUE_CATEGORY_REQUEST_NAME, projectName); - } - - @Test - public void getTrackers() throws Exception { - - askData(ModelHelper.GET_ALL_TRACKER_REQUEST_NAME, projectName); - } - - @Test - public void getNews() throws Exception { - - askData(ModelHelper.GET_ALL_NEWS_REQUEST_NAME, projectName); - } - - @Test - public void getProjectMembers() throws Exception { - - askData(ModelHelper.GET_ALL_USER_REQUEST_NAME, projectName); - } - - @Test - public void getProjectIssues() throws Exception { - - askData(ModelHelper.GET_ALL_PROJECT_ISSUES_REQUEST_NAME, projectName); - } - - @Test - public void getVersions() throws Exception { - - askData(ModelHelper.GET_ALL_VERSION_REQUEST_NAME, projectName); - } - - @Test - public void getVersion() throws Exception { - - askData(ModelHelper.GET_VERSION_REQUEST_NAME, projectName, versionName); - } - - @Test - public void getVersionIssues() throws Exception { - - askData(ModelHelper.GET_ALL_ISSUES_REQUEST_NAME, projectName, versionName); - } - - @Test - public void getOpenedIssues() throws Exception { - - askData(ModelHelper.GET_ALL_PROJECT_OPENED_ISSUES_REQUEST_NAME, projectName); - } - - @Test - public void getClosedIssues() throws Exception { - - askData(ModelHelper.GET_ALL_PROJECT_CLOSED_ISSUES_REQUEST_NAME, projectName); - } - - @Test - public void getIssueTimeEntries() throws Exception { - - askData(ModelHelper.GET_ALL_ISSUE_TIME_ENTRY_REQUEST_NAME, projectName, issueId); - } - - @Test - public void getAttachments() throws Exception { - - askData(ModelHelper.GET_ALL_ATTACHMENTS_REQUEST_NAME, projectName, versionName); - } - - @Test - public void login() throws Exception { - - Assume.assumeTrue(!getConfiguration().isAnonymous()); - Assume.assumeTrue(StringUtils.isBlank(getConfiguration().getApiKey())); - client.open(); - } - - @Test - public void loginByApiKey() throws Exception { - - Assume.assumeTrue(!getConfiguration().isAnonymous()); - Assume.assumeTrue(StringUtils.isNotBlank(getConfiguration().getApiKey())); - client.open(); - } - - @Test - public void getUserProjects() throws Exception { - - Assume.assumeTrue(!getConfiguration().isAnonymous()); - askData(ModelHelper.GET_USER_PROJECTS_REQUEST_NAME); - } - - @Test - public void addVersion() throws Exception { - Assume.assumeTrue(!getConfiguration().isAnonymous()); - //TODO - } - - @Test - public void addAttachment() throws Exception { - Assume.assumeTrue(!getConfiguration().isAnonymous()); - //TODO - } - - @Test - public void addNews() throws Exception { - Assume.assumeTrue(!getConfiguration().isAnonymous()); - //TODO - } - - @Test - public void updateVersion() throws Exception { - Assume.assumeTrue(!getConfiguration().isAnonymous()); - //TODO - } - - @Test - public void nextVersion() throws Exception { - Assume.assumeTrue(!getConfiguration().isAnonymous()); - //TODO - } - - @Test - public void addIssueTime() throws Exception { - Assume.assumeTrue(!getConfiguration().isAnonymous()); - //TODO - } - - protected void askData(String requestId, Object... params) throws Exception { - - RestRequest request = client.getRequest(requestId, params); - InputStream askData = client.askData(request); - String toString = IOUtil.toString(askData); - if (getConfiguration().isVerbose() && log.isInfoEnabled()) { - log.info(toString); - } - } - - protected RedmineServiceConfiguration getConfiguration() { - return checkConfigRule.getConf(); - } -} Modified: trunk/jredmine-maven-plugin/pom.xml =================================================================== --- trunk/jredmine-maven-plugin/pom.xml 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-maven-plugin/pom.xml 2012-10-13 21:50:04 UTC (rev 364) @@ -64,6 +64,11 @@ </dependency> <dependency> + <groupId>org.apache.httpcomponents</groupId> + <artifactId>httpclient</artifactId> + </dependency> + + <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> Modified: trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/AbstractRedmineMojo.java =================================================================== --- trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/AbstractRedmineMojo.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/AbstractRedmineMojo.java 2012-10-13 21:50:04 UTC (rev 364) @@ -32,6 +32,7 @@ import org.apache.maven.project.MavenProject; import org.nuiton.jredmine.RedmineService; import org.nuiton.jredmine.RedmineServiceConfiguration; +import org.nuiton.jredmine.client.RedmineClientAuthConfiguration; import org.nuiton.plugin.AbstractPlugin; import java.net.URL; @@ -64,16 +65,16 @@ @Parameter(property = "redmine.url") protected URL url; -// /** -// * The redmine's server login apiKey. -// * <p/> -// * <b>Note:</b> This parameter (or username/password) is mandatory if you -// * not use a {@code anonymous} service. -// * -// * @since 2.0 -// */ -// @Parameter(property = "redmine.apiKey") -// protected String apiKey; + /** + * The redmine's server login apiKey. + * <p/> + * <b>Note:</b> This parameter (or username/password) is mandatory if you + * not use a {@code anonymous} service. + * + * @since 1.5 + */ + @Parameter(property = "redmine.apiKey") + protected String apiKey; /** * The redmine's server login. @@ -167,6 +168,8 @@ protected boolean initOk = true; + private RedmineClientAuthConfiguration authConfiguration; + /////////////////////////////////////////////////////////////////////////// /// Plugin /////////////////////////////////////////////////////////////////////////// @@ -206,62 +209,29 @@ } /////////////////////////////////////////////////////////////////////////// - /// RestClientConfiguration + /// RedmineClientConfiguration /////////////////////////////////////////////////////////////////////////// @Override - public String getRestPassword() { - return password; + public RedmineClientAuthConfiguration getAuthConfiguration() { + return authConfiguration; } @Override - public URL getRestUrl() { - return url; + public void setAuthConfiguration(RedmineClientAuthConfiguration authConfiguration) { + this.authConfiguration = authConfiguration; } @Override - public String getRestUsername() { - return username; + public URL getUrl() { + return url; } @Override - public void setRestPassword(String restPassword) { - password = restPassword; + public void setUrl(URL url) { + this.url = url; } - @Override - public void setRestUrl(URL restUrl) { - url = restUrl; - } - - @Override - public void setRestUsername(String restUsername) { - username = restUsername; - } - - /** - * <strong>Note:</strong> Still not used (will be in version 2.0) - * - * @return the api key - * @since 2.0 - */ - @Override - public String getApiKey() { - return null; -// return apiKey; - } - - /** - * <strong>Note:</strong> Still not used (will be in version 2.0) - * - * @param apiKey the niew api key to set - * @since 2.0 - */ - @Override - public void setApiKey(String apiKey) { -// this.apiKey = apiKey; - } - /////////////////////////////////////////////////////////////////////////// /// AbstractRedmineMojo /////////////////////////////////////////////////////////////////////////// @@ -290,10 +260,9 @@ } else if (issueManagement.getSystem() != null && !issueManagement.getSystem().equalsIgnoreCase(REDMINE_SYSTEM)) { throw new MojoExecutionException("Redmine's Plugin only supports 'redmine' Issue Management system."); } + // prepare Redmine service configuration - URL url = getRestUrl(); - if (url == null || url.toString().isEmpty()) { // no redmine url specified, guess it from issueManagement @@ -307,22 +276,27 @@ // apply configuration - setRestUrl(url); + setUrl(url); + authConfiguration = RedmineClientAuthConfiguration.newConf( + apiKey, username, password + ); + if (verbose) { if (isAnonymous()) { getLog().info("Redmine anonymous configuration :\n>> host : " + - getRestUrl()); + getUrl()); } else { -// if (StringUtils.isNotBlank(apiKey)) { -// getLog().info("Redmine configuration :\n>> host : " + -// getRestUrl() + "\n>> apiKey: " + -// getApiKey()); -// } else { - getLog().info("Redmine configuration :\n>> host : " + - getRestUrl() + "\n>> username : " + - getRestUsername()); -// } + + if (authConfiguration.isUseApiKey()) { + getLog().info("Redmine configuration :\n>> host : " + + getUrl() + "\n>> apiKey: " + + apiKey); + } else { + getLog().info("Redmine configuration :\n>> host : " + + getUrl() + "\n>> username : " + + username); + } } } @@ -338,7 +312,7 @@ } serviceInit = false; initOk = false; - getLog().error("could not init Redmine service [" + getRestUrl() + "] with user '" + getRestUsername() + "'", e); + getLog().error("could not init Redmine service [" + getUrl() + "]", e); } } @@ -355,7 +329,7 @@ } if (!serviceInit) { - getLog().error("could not init Redmine service [" + getRestUrl() + "] with user '" + getRestUsername() + "'"); + getLog().error("could not init Redmine service [" + getUrl() + "]"); return false; } @@ -379,6 +353,18 @@ /// Others /////////////////////////////////////////////////////////////////////////// + public void setApiKey(String apiKey) { + this.apiKey = apiKey; + } + + public void setUsername(String username) { + this.username = username; + } + + public void setPassword(String password) { + this.password = password; + } + protected void closeService() { if (service != null) { try { Modified: trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/DisplayDataMojo.java =================================================================== --- trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/DisplayDataMojo.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/DisplayDataMojo.java 2012-10-13 21:50:04 UTC (rev 364) @@ -44,7 +44,7 @@ import org.nuiton.jredmine.model.Tracker; import org.nuiton.jredmine.model.User; import org.nuiton.jredmine.model.Version; -import org.nuiton.jredmine.rest.RedmineRequestFactory; +import org.nuiton.jredmine.client.RedmineRequestFactory; import org.nuiton.plugin.PluginHelper; import java.util.Arrays; @@ -159,7 +159,7 @@ } /////////////////////////////////////////////////////////////////////////// - /// RestClientConfiguration + /// RedmineClientConfiguration /////////////////////////////////////////////////////////////////////////// @Override @@ -350,7 +350,7 @@ protected String getDataScope(Class<?> type) { RequestFactory requestFactory = - service.getDelegateImplementor().getSession().getRequestFactory(); + service.getDelegateImplementor().getClient().getRequestFactory(); String requestName = typesToRequestName.get(type); RestRequestBuilder requestBuilder = Modified: trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/GenerateChangesMojo.java =================================================================== --- trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/GenerateChangesMojo.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/GenerateChangesMojo.java 2012-10-13 21:50:04 UTC (rev 364) @@ -69,7 +69,6 @@ /** The actions understood by the changes.xml format. */ enum Actions { - add, fix, update, @@ -209,7 +208,7 @@ } /////////////////////////////////////////////////////////////////////////// - /// RestClientConfiguration + /// RedmineClientConfiguration /////////////////////////////////////////////////////////////////////////// @Override Modified: trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/LoginMojo.java =================================================================== --- trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/LoginMojo.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/LoginMojo.java 2012-10-13 21:50:04 UTC (rev 364) @@ -35,10 +35,12 @@ import org.nuiton.jredmine.RedmineService; import org.nuiton.jredmine.RedmineServiceConfiguration; import org.nuiton.jredmine.RedmineServiceImplementor; +import org.nuiton.jredmine.client.RedmineClientAuthConfiguration; import org.nuiton.plugin.AbstractPlugin; import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher; import java.net.URL; +import java.util.Properties; /** * Obtain login for the redmine server to use. @@ -97,15 +99,15 @@ @Parameter(property = "redmine.serverId", required = true) protected String serverId; -// /** -// * Un flag pour utiliser le clef API pluto que le login/password. -// * <p/> -// * Il faut alors que le serveur contienne en username le clef API (encrypté ou non). -// * -// * @since 2.0 -// */ -// @Parameter(property = "redmine.useApiKey", defaultValue = "false") -// private boolean useApiKey; + /** + * Un flag pour utiliser le clef API pluto que le login/password. + * <p/> + * Il faut alors que le serveur contienne en username le clef API (encrypté ou non). + * + * @since 2.0 + */ + @Parameter(property = "redmine.useApiKey", defaultValue = "false") + private boolean useApiKey; /////////////////////////////////////////////////////////////////////////// /// Mojo components @@ -155,6 +157,9 @@ */ protected static ShareServerSecretPlugin plugin; + + private RedmineClientAuthConfiguration authConfiguration; + private static final String REDMINE_USERNAME = "redmine.username"; private static final String REDMINE_PASSWORD = "redmine.password"; @@ -199,7 +204,7 @@ } /////////////////////////////////////////////////////////////////////////// - /// RestClientConfiguration + /// RedmineClientConfiguration /////////////////////////////////////////////////////////////////////////// @Override @@ -211,47 +216,26 @@ public void setAnonymous(boolean anonymous) { } - /////////////////////////////////////////////////////////////////////////// - /// RestClientConfiguration - /////////////////////////////////////////////////////////////////////////// - @Override - public String getRestPassword() { - return project.getProperties().getProperty(REDMINE_PASSWORD); - } - - @Override - public URL getRestUrl() { + public URL getUrl() { return url; } @Override - public String getRestUsername() { - return project.getProperties().getProperty(REDMINE_USERNAME); + public void setUrl(URL url) { + this.url = url; } @Override - public void setRestPassword(String restPassword) { + public RedmineClientAuthConfiguration getAuthConfiguration() { + return authConfiguration; } @Override - public void setRestUrl(URL restUrl) { - url = restUrl; + public void setAuthConfiguration(RedmineClientAuthConfiguration authConfiguration) { + this.authConfiguration = authConfiguration; } - @Override - public void setRestUsername(String restUsername) { - } - - @Override - public String getApiKey() { - return project.getProperties().getProperty(REDMINE_API_KEY); - } - - @Override - public void setApiKey(String apiKey) { - } - /////////////////////////////////////////////////////////////////////////// /// AbstractPlugin /////////////////////////////////////////////////////////////////////////// @@ -261,12 +245,12 @@ if (plugin == null) { plugin = new ShareServerSecretPlugin(); plugin.setServerId(serverId); -// if (useApiKey) { -// plugin.setUsernameOut(REDMINE_API_KEY); -// } else { - plugin.setUsernameOut(REDMINE_USERNAME); - plugin.setPasswordOut(REDMINE_PASSWORD); -// } + if (useApiKey) { + plugin.setUsernameOut(REDMINE_API_KEY); + } else { + plugin.setUsernameOut(REDMINE_USERNAME); + plugin.setPasswordOut(REDMINE_PASSWORD); + } plugin.setRunOnce(true); plugin.setProject(project); @@ -297,8 +281,6 @@ // prepare Redmine service configuration - URL url = getRestUrl(); - if (url == null || url.toString().isEmpty()) { // no redmine url specified, guess it from issueManagement @@ -312,7 +294,7 @@ // apply configuration - setRestUrl(url); + setUrl(url); } @Override @@ -337,6 +319,13 @@ return; } + Properties properties = project.getProperties(); + authConfiguration = RedmineClientAuthConfiguration.newConf( + properties.getProperty(REDMINE_API_KEY), + properties.getProperty(REDMINE_USERNAME), + properties.getProperty(REDMINE_PASSWORD) + ); + // init Redmine service try { @@ -351,7 +340,7 @@ throw e; } // if (verbose) { - getLog().error("could not init Redmine service [" + getRestUrl() + "] with user '" + getRestUsername() + "'", e); + getLog().error("could not init Redmine service [" + getUrl() + "]", e); // } // return false; } Modified: trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/NextVersionMojo.java =================================================================== --- trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/NextVersionMojo.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/NextVersionMojo.java 2012-10-13 21:50:04 UTC (rev 364) @@ -132,7 +132,7 @@ } /////////////////////////////////////////////////////////////////////////// - /// RestClientConfiguration + /// RedmineClientConfiguration /////////////////////////////////////////////////////////////////////////// @Override Modified: trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/PublishAttachmentsMojo.java =================================================================== --- trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/PublishAttachmentsMojo.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/PublishAttachmentsMojo.java 2012-10-13 21:50:04 UTC (rev 364) @@ -123,7 +123,7 @@ } /////////////////////////////////////////////////////////////////////////// - /// RestClientConfiguration + /// RedmineClientConfiguration /////////////////////////////////////////////////////////////////////////// @Override Modified: trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/PublishNewsMojo.java =================================================================== --- trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/PublishNewsMojo.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/PublishNewsMojo.java 2012-10-13 21:50:04 UTC (rev 364) @@ -112,7 +112,7 @@ } /////////////////////////////////////////////////////////////////////////// - /// RestClientConfiguration + /// RedmineClientConfiguration /////////////////////////////////////////////////////////////////////////// @Override Modified: trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/UpdateVersionMojo.java =================================================================== --- trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/UpdateVersionMojo.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/UpdateVersionMojo.java 2012-10-13 21:50:04 UTC (rev 364) @@ -149,7 +149,7 @@ } /////////////////////////////////////////////////////////////////////////// - /// RestClientConfiguration + /// RedmineClientConfiguration /////////////////////////////////////////////////////////////////////////// @Override Modified: trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AbstractAnnouncementMojo.java =================================================================== --- trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AbstractAnnouncementMojo.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AbstractAnnouncementMojo.java 2012-10-13 21:50:04 UTC (rev 364) @@ -268,10 +268,10 @@ return attachmentLinkTemplate; } - @Override - public String getUrl() { - return url.toString(); - } +// @Override +// public String getUrl() { +// return url.toString(); +// } @Override public String getArtifactId() { Modified: trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AnnouncementGenerator.java =================================================================== --- trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AnnouncementGenerator.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AnnouncementGenerator.java 2012-10-13 21:50:04 UTC (rev 364) @@ -81,7 +81,7 @@ } public String getUrl() { - return configuration.getUrl(); + return configuration.getUrl().toString(); } public Map<Attachment, String> getAttachmentsUrls(Attachment[] attachments) { Modified: trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AnnouncementGeneratorConfiguration.java =================================================================== --- trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AnnouncementGeneratorConfiguration.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/announcement/AnnouncementGeneratorConfiguration.java 2012-10-13 21:50:04 UTC (rev 364) @@ -29,6 +29,7 @@ import org.nuiton.jredmine.model.Attachment; import java.io.File; +import java.net.URL; import java.util.Map; /** @@ -41,7 +42,7 @@ public interface AnnouncementGeneratorConfiguration { /** @return the redmine url */ - String getUrl(); + URL getUrl(); /** @return the redmine attachment url template */ String getAttachmentLinkTemplate(); Modified: trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/report/AbstractRedmineReport.java =================================================================== --- trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/report/AbstractRedmineReport.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-maven-plugin/src/main/java/org/nuiton/jredmine/plugin/report/AbstractRedmineReport.java 2012-10-13 21:50:04 UTC (rev 364) @@ -196,7 +196,7 @@ } /////////////////////////////////////////////////////////////////////////// - /// RestClientConfiguration + /// RedmineClientConfiguration /////////////////////////////////////////////////////////////////////////// @Override Modified: trunk/jredmine-maven-plugin/src/test/java/org/nuiton/jredmine/plugin/AbstractRedmineMojoTest.java =================================================================== --- trunk/jredmine-maven-plugin/src/test/java/org/nuiton/jredmine/plugin/AbstractRedmineMojoTest.java 2012-10-13 21:48:33 UTC (rev 363) +++ trunk/jredmine-maven-plugin/src/test/java/org/nuiton/jredmine/plugin/AbstractRedmineMojoTest.java 2012-10-13 21:50:04 UTC (rev 364) @@ -33,6 +33,7 @@ import org.nuiton.jredmine.RedmineConfigurationUtil; import org.nuiton.jredmine.RedmineFixtures; import org.nuiton.jredmine.RedmineServiceConfiguration; +import org.nuiton.jredmine.client.RedmineClientAuthConfiguration; import org.nuiton.plugin.AbstractMojoTest; import java.io.File; @@ -62,9 +63,15 @@ // add a issue management IssueManagement i = new IssueManagement(); i.setSystem(AbstractRedmineMojo.REDMINE_SYSTEM); - i.setUrl(mojo.getRestUrl().toString()); + i.setUrl(mojo.getUrl().toString()); mojo.getProject().setIssueManagement(i); + RedmineClientAuthConfiguration authConfiguration = + getConfiguration().getAuthConfiguration(); + mojo.setUsername(authConfiguration.getUsername()); + mojo.setPassword(authConfiguration.getPassword()); + mojo.setApiKey(authConfiguration.getApiKey()); + if (mojo instanceof RedmineProjectAware) { // fill projectId if none is given from pom.xml @@ -110,6 +117,8 @@ try { mojo.init(); + + canContinue = mojo.checkSkip(); if (!canContinue) { if (log.isInfoEnabled()) {
participants (1)
-
tchemit@users.nuiton.org