diff --git a/source/test-java/org/alfresco/rest/api/tests/AbstractTestApi.java b/source/test-java/org/alfresco/rest/api/tests/AbstractTestApi.java index 3c1b847554..0240548470 100644 --- a/source/test-java/org/alfresco/rest/api/tests/AbstractTestApi.java +++ b/source/test-java/org/alfresco/rest/api/tests/AbstractTestApi.java @@ -1,28 +1,28 @@ -/* - * #%L - * Alfresco Remote API - * %% - * Copyright (C) 2005 - 2016 Alfresco Software Limited - * %% - * This file is part of the Alfresco software. - * If the software was purchased under a paid Alfresco license, the terms of - * the paid license agreement will prevail. Otherwise, the software is - * provided under the following open source license terms: - * - * Alfresco 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. - * - * Alfresco 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 Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Alfresco. If not, see . - * #L% - */ +/* + * #%L + * Alfresco Remote API + * %% + * Copyright (C) 2005 - 2016 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * + * Alfresco 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. + * + * Alfresco 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ package org.alfresco.rest.api.tests; import static org.junit.Assert.assertEquals; @@ -73,9 +73,9 @@ public abstract class AbstractTestApi @Before public void setupTests() throws Exception { - TestFixture testFixture = getTestFixture(false); - this.applicationContext = testFixture.getApplicationContext(); - this.repoService = testFixture.getRepoService(); + TestFixture testFixture = getTestFixture(false); + this.applicationContext = testFixture.getApplicationContext(); + this.repoService = testFixture.getRepoService(); this.transactionHelper = (RetryingTransactionHelper)applicationContext.getBean("retryingTransactionHelper"); HttpClientProvider httpClientProvider = (HttpClientProvider)applicationContext.getBean("httpClientProvider"); @@ -95,7 +95,7 @@ public abstract class AbstractTestApi }; AuthenticationDetailsProvider authenticationDetailsProvider = new UserAuthenticationDetailsProviderImpl(userDataService, "admin", "admin"); AuthenticatedHttp authenticatedHttp = new AuthenticatedHttp(httpClientProvider, authenticationDetailsProvider); - this.httpClient = new PublicApiHttpClient("localhost", TestFixture.PORT, TestFixture.CONTEXT_PATH, + this.httpClient = new PublicApiHttpClient(TestFixture.HOST, TestFixture.PORT, TestFixture.CONTEXT_PATH, TestFixture.PUBLIC_API_SERVLET_NAME, authenticatedHttp); this.publicApiClient = new PublicApiClient(httpClient, userDataService); } @@ -223,9 +223,9 @@ public abstract class AbstractTestApi } protected abstract TestFixture getTestFixture() throws Exception; - - /** - * @param createTestData The created instance can optionally create test data if required - */ - protected abstract TestFixture getTestFixture(boolean createTestData) throws Exception; + + /** + * @param createTestData The created instance can optionally create test data if required + */ + protected abstract TestFixture getTestFixture(boolean createTestData) throws Exception; } diff --git a/source/test-java/org/alfresco/rest/api/tests/AbstractTestFixture.java b/source/test-java/org/alfresco/rest/api/tests/AbstractTestFixture.java index 5ac27d321d..7c108d728d 100644 --- a/source/test-java/org/alfresco/rest/api/tests/AbstractTestFixture.java +++ b/source/test-java/org/alfresco/rest/api/tests/AbstractTestFixture.java @@ -1,28 +1,28 @@ -/* - * #%L - * Alfresco Remote API - * %% - * Copyright (C) 2005 - 2016 Alfresco Software Limited - * %% - * This file is part of the Alfresco software. - * If the software was purchased under a paid Alfresco license, the terms of - * the paid license agreement will prevail. Otherwise, the software is - * provided under the following open source license terms: - * - * Alfresco 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. - * - * Alfresco 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 Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Alfresco. If not, see . - * #L% - */ +/* + * #%L + * Alfresco Remote API + * %% + * Copyright (C) 2005 - 2016 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * + * Alfresco 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. + * + * Alfresco 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ package org.alfresco.rest.api.tests; import static org.junit.Assert.assertNotNull; @@ -54,12 +54,12 @@ public abstract class AbstractTestFixture implements TestFixture protected TreeMap networks = new TreeMap(); protected TreeMap sites = new TreeMap(); - protected String[] configLocations; - protected final String[] classLocations; - protected int port = 8081; - protected String contextPath = "/alfresco"; - protected String servletName = "api"; - protected String hostname = "localhost"; + private String[] configLocations; + private final String[] classLocations; + private int port = PORT; + private String contextPath = CONTEXT_PATH; + private String servletName = PUBLIC_API_SERVLET_NAME; + protected JettyComponent jetty; protected boolean cleanup; protected Random random = new Random(); @@ -94,29 +94,49 @@ public abstract class AbstractTestFixture implements TestFixture { return port; } - + + public String getContextPath() + { + return contextPath; + } + + public String getServletName() + { + return servletName; + } + + public String[] getConfigLocations() + { + return configLocations; + } + + public String[] getClassLocations() + { + return classLocations; + } + protected abstract JettyComponent makeJettyComponent(); protected abstract void populateTestData(); protected abstract RepoService makeRepoService() throws Exception; public void setup() throws Exception { - setup(true); - } - - public void setup(boolean createTestData) throws Exception - { + setup(true); + } + + public void setup(boolean createTestData) throws Exception + { this.jetty = makeJettyComponent(); this.jetty.start(); this.applicationContext = jetty.getApplicationContext(); this.repoService = makeRepoService(); this.transactionHelper = (RetryingTransactionHelper)repoService.getApplicationContext().getBean("retryingTransactionHelper"); - if (createTestData) - { - populateTestData(); - createTestData(); - } + if (createTestData) + { + populateTestData(); + createTestData(); + } } public RepoService getRepoService() @@ -146,11 +166,11 @@ public abstract class AbstractTestFixture implements TestFixture public TestNetwork getRandomNetwork() { - if (networks.isEmpty()) - { - populateTestData(); - createTestData(); - } + if (networks.isEmpty()) + { + populateTestData(); + createTestData(); + } int r = random.nextInt(networks.size()); List a = new ArrayList(); a.addAll(networks.values()); diff --git a/source/test-java/org/alfresco/rest/api/tests/EnterprisePublicApiTestFixture.java b/source/test-java/org/alfresco/rest/api/tests/EnterprisePublicApiTestFixture.java index bafd5abced..bdb58b47cc 100644 --- a/source/test-java/org/alfresco/rest/api/tests/EnterprisePublicApiTestFixture.java +++ b/source/test-java/org/alfresco/rest/api/tests/EnterprisePublicApiTestFixture.java @@ -1,28 +1,28 @@ -/* - * #%L - * Alfresco Remote API - * %% - * Copyright (C) 2005 - 2016 Alfresco Software Limited - * %% - * This file is part of the Alfresco software. - * If the software was purchased under a paid Alfresco license, the terms of - * the paid license agreement will prevail. Otherwise, the software is - * provided under the following open source license terms: - * - * Alfresco 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. - * - * Alfresco 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 Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Alfresco. If not, see . - * #L% - */ +/* + * #%L + * Alfresco Remote API + * %% + * Copyright (C) 2005 - 2016 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * + * Alfresco 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. + * + * Alfresco 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ package org.alfresco.rest.api.tests; import org.alfresco.repo.web.util.JettyComponent; @@ -45,21 +45,21 @@ public class EnterprisePublicApiTestFixture extends EnterpriseTestFixture /* * Note: synchronized for multi-threaded test access */ - public synchronized static EnterprisePublicApiTestFixture getInstance(boolean createTestData) throws Exception + public synchronized static EnterprisePublicApiTestFixture getInstance(boolean createTestData) throws Exception { if(instance == null) { instance = new EnterprisePublicApiTestFixture(); - instance.setup(createTestData); + instance.setup(createTestData); } return instance; } - public static EnterprisePublicApiTestFixture getInstance() throws Exception - { - return getInstance(true); - } - + public static EnterprisePublicApiTestFixture getInstance() throws Exception + { + return getInstance(true); + } + private EnterprisePublicApiTestFixture() { super(CONFIG_LOCATIONS, CLASS_LOCATIONS, PORT, CONTEXT_PATH, PUBLIC_API_SERVLET_NAME, DEFAULT_NUM_MEMBERS_PER_SITE, false); @@ -68,7 +68,7 @@ public class EnterprisePublicApiTestFixture extends EnterpriseTestFixture @Override protected JettyComponent makeJettyComponent() { - JettyComponent jettyComponent = new EnterpriseJettyComponent(port, contextPath, configLocations, classLocations); + JettyComponent jettyComponent = new EnterpriseJettyComponent(getPort(), getContextPath(), getConfigLocations(), getClassLocations()); return jettyComponent; } diff --git a/source/test-java/org/alfresco/rest/api/tests/TestFixture.java b/source/test-java/org/alfresco/rest/api/tests/TestFixture.java index 01a5c2a040..160bd152f5 100644 --- a/source/test-java/org/alfresco/rest/api/tests/TestFixture.java +++ b/source/test-java/org/alfresco/rest/api/tests/TestFixture.java @@ -23,25 +23,26 @@ * along with Alfresco. If not, see . * #L% */ -package org.alfresco.rest.api.tests; - -import java.util.Iterator; - -import org.alfresco.repo.web.util.JettyComponent; -import org.alfresco.rest.api.tests.RepoService.TestNetwork; -import org.springframework.context.ApplicationContext; - -public interface TestFixture -{ - public final static int PORT = 8081; - public final static String CONTEXT_PATH = "/alfresco"; - public final static String PUBLIC_API_SERVLET_NAME = "api"; - - ApplicationContext getApplicationContext(); - RepoService getRepoService(); - Iterator getNetworksIt(); - TestNetwork getRandomNetwork(); - Iterator networksIterator(); - JettyComponent getJettyComponent(); - TestNetwork getNetwork(String name); -} +package org.alfresco.rest.api.tests; + +import java.util.Iterator; + +import org.alfresco.repo.web.util.JettyComponent; +import org.alfresco.rest.api.tests.RepoService.TestNetwork; +import org.springframework.context.ApplicationContext; + +public interface TestFixture +{ + public final static String HOST = "localhost"; + public final static int PORT = 8765; + public final static String CONTEXT_PATH = "/alfresco"; + public final static String PUBLIC_API_SERVLET_NAME = "api"; + + ApplicationContext getApplicationContext(); + RepoService getRepoService(); + Iterator getNetworksIt(); + TestNetwork getRandomNetwork(); + Iterator networksIterator(); + JettyComponent getJettyComponent(); + TestNetwork getNetwork(String name); +} diff --git a/source/test-java/org/alfresco/rest/api/tests/client/PublicApiHttpClient.java b/source/test-java/org/alfresco/rest/api/tests/client/PublicApiHttpClient.java index e7850b49b2..fd2054192f 100644 --- a/source/test-java/org/alfresco/rest/api/tests/client/PublicApiHttpClient.java +++ b/source/test-java/org/alfresco/rest/api/tests/client/PublicApiHttpClient.java @@ -23,96 +23,97 @@ * along with Alfresco. If not, see . * #L% */ -package org.alfresco.rest.api.tests.client; - +package org.alfresco.rest.api.tests.client; + import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; -import java.io.IOException; +import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.io.UnsupportedEncodingException; -import java.net.URLEncoder; -import java.text.MessageFormat; -import java.util.HashMap; -import java.util.Map; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.text.MessageFormat; +import java.util.HashMap; +import java.util.Map; import java.util.Map.Entry; - -import org.alfresco.error.AlfrescoRuntimeException; -import org.alfresco.opencmis.CMISDispatcherRegistry.Binding; -import org.alfresco.repo.tenant.TenantService; -import org.alfresco.repo.tenant.TenantUtil; -import org.alfresco.repo.web.scripts.BaseWebScriptTest.PatchMethod; -import org.alfresco.rest.api.tests.client.AuthenticatedHttp.HttpRequestCallback; -import org.alfresco.rest.framework.Api; -import org.alfresco.rest.framework.Api.SCOPE; -import org.alfresco.rest.framework.core.ResourceInspector; -import org.alfresco.rest.framework.resource.EntityResource; -import org.alfresco.rest.framework.resource.RelationshipResource; -import org.alfresco.service.cmr.repository.NodeRef; -import org.alfresco.util.Pair; -import org.apache.commons.httpclient.Header; -import org.apache.commons.httpclient.HttpException; -import org.apache.commons.httpclient.HttpMethod; + +import org.alfresco.error.AlfrescoRuntimeException; +import org.alfresco.opencmis.CMISDispatcherRegistry.Binding; +import org.alfresco.repo.tenant.TenantService; +import org.alfresco.repo.tenant.TenantUtil; +import org.alfresco.repo.web.scripts.BaseWebScriptTest.PatchMethod; +import org.alfresco.rest.api.tests.TestFixture; +import org.alfresco.rest.api.tests.client.AuthenticatedHttp.HttpRequestCallback; +import org.alfresco.rest.framework.Api; +import org.alfresco.rest.framework.Api.SCOPE; +import org.alfresco.rest.framework.core.ResourceInspector; +import org.alfresco.rest.framework.resource.EntityResource; +import org.alfresco.rest.framework.resource.RelationshipResource; +import org.alfresco.service.cmr.repository.NodeRef; +import org.alfresco.util.Pair; +import org.apache.commons.httpclient.Header; +import org.apache.commons.httpclient.HttpException; +import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.HttpMethodBase; import org.apache.commons.httpclient.methods.ByteArrayRequestEntity; -import org.apache.commons.httpclient.methods.DeleteMethod; -import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.commons.httpclient.methods.HeadMethod; -import org.apache.commons.httpclient.methods.OptionsMethod; -import org.apache.commons.httpclient.methods.PostMethod; -import org.apache.commons.httpclient.methods.PutMethod; +import org.apache.commons.httpclient.methods.DeleteMethod; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.methods.HeadMethod; +import org.apache.commons.httpclient.methods.OptionsMethod; +import org.apache.commons.httpclient.methods.PostMethod; +import org.apache.commons.httpclient.methods.PutMethod; import org.apache.commons.httpclient.methods.RequestEntity; -import org.apache.commons.httpclient.methods.StringRequestEntity; -import org.apache.commons.httpclient.methods.TraceMethod; +import org.apache.commons.httpclient.methods.StringRequestEntity; +import org.apache.commons.httpclient.methods.TraceMethod; import org.apache.commons.lang.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.core.io.support.PathMatchingResourcePatternResolver; -import org.springframework.core.io.support.ResourcePatternResolver; -import org.springframework.core.type.AnnotationMetadata; -import org.springframework.core.type.classreading.CachingMetadataReaderFactory; -import org.springframework.core.type.classreading.MetadataReader; -import org.springframework.core.type.classreading.MetadataReaderFactory; - -/** +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.core.io.support.ResourcePatternResolver; +import org.springframework.core.type.AnnotationMetadata; +import org.springframework.core.type.classreading.CachingMetadataReaderFactory; +import org.springframework.core.type.classreading.MetadataReader; +import org.springframework.core.type.classreading.MetadataReaderFactory; + +/** * A http client for talking to the rest apis. The caller can pass in a rest api * implementation class to the http method (get, post, put, delete supported) - * and the url will be generated. + * and the url will be generated. * - * @author steveglover - */ -public class PublicApiHttpClient -{ - private static final Log logger = LogFactory.getLog(PublicApiHttpClient.class); - - private static final String OLD_BASE_URL = "{0}://{1}:{2}{3}{4}{5}/api/"; + * @author steveglover + */ +public class PublicApiHttpClient +{ + private static final Log logger = LogFactory.getLog(PublicApiHttpClient.class); + + private static final String OLD_BASE_URL = "{0}://{1}:{2}{3}{4}{5}/api/"; private static final String INDEX_URL = "{0}://{1}:{2}{3}{4}"; private static final String BASE_URL = "{0}://{1}:{2}{3}{4}{5}/{6}/{7}/versions/{8}"; private static final String PUBLICAPI_CMIS_SERVICE_URL = "{0}://{1}:{2}{3}{4}cmis/versions/{5}/{6}"; private static final String PUBLICAPI_CMIS_URL = "{0}://{1}:{2}{3}{4}{5}/{6}/cmis/versions/{7}/{8}"; - private static final String PUBLICAPI_CMIS_URL_SUFFIX = "{0}/{1}/cmis/versions/{2}/{3}"; + private static final String PUBLICAPI_CMIS_URL_SUFFIX = "{0}/{1}/cmis/versions/{2}/{3}"; private static final String ATOM_PUB_URL = "{0}://{1}:{2}{3}cmisatom"; - + private String scheme = "http"; - private String host = "localhost"; - private int port = 8081; - - private String contextPath; - private String servletName; - private AuthenticatedHttp authenticatedHttp; - + private String host = TestFixture.HOST; + private int port = TestFixture.PORT; + + private String contextPath; + private String servletName; + private AuthenticatedHttp authenticatedHttp; + private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(); private MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver); - + // can be overriden by other clients like the workflow client protected String apiName = "alfresco"; - + public PublicApiHttpClient(String host, int port, String contextPath, String servletName, AuthenticatedHttp authenticatedHttp) { this("http", host, port, contextPath, servletName, authenticatedHttp); } - + public PublicApiHttpClient(String scheme, String host, int port, String contextPath, String servletName, AuthenticatedHttp authenticatedHttp) { super(); @@ -135,7 +136,7 @@ public class PublicApiHttpClient } this.authenticatedHttp = authenticatedHttp; } - + public String getCmisUrl(String repositoryId, String operation) { StringBuilder url = new StringBuilder(); @@ -149,16 +150,16 @@ public class PublicApiHttpClient url.append("/"); url.append(repositoryId); } - + if (operation != null) { url.append("/"); url.append(operation); } - + return url.toString(); } - + public String getPublicApiCmisUrl(String networkId, Binding binding, String version, String operation) { StringBuilder url = new StringBuilder(); @@ -172,41 +173,41 @@ public class PublicApiHttpClient url.append(MessageFormat.format(PUBLICAPI_CMIS_URL, new Object[] { scheme, host, String.valueOf(port), contextPath, servletName, networkId, "public", version, binding.toString().toLowerCase() })); } - + if (operation != null) { url.append("/"); url.append(operation); } - + return url.toString(); } - public String getPublicApiCmisUrlSuffix(String networkId, Binding binding, String version, String operation) - { - StringBuilder url = new StringBuilder(); - + public String getPublicApiCmisUrlSuffix(String networkId, Binding binding, String version, String operation) + { + StringBuilder url = new StringBuilder(); + url.append(MessageFormat.format(PUBLICAPI_CMIS_URL_SUFFIX, new Object[] { networkId, "public", version, binding.toString().toLowerCase() })); - + if (operation != null) - { - url.append("/"); - url.append(operation); - } - - return url.toString(); - } - + { + url.append("/"); + url.append(operation); + } + + return url.toString(); + } + public void setHost(String host) { this.host = host; } - + public void setPort(int port) { this.port = port; } - + public void setContextPath(String contextPath) { this.contextPath = contextPath; @@ -224,19 +225,19 @@ public class PublicApiHttpClient } } - protected AnnotationMetadata getAnnotationMetadata(String classname) throws IOException - { + protected AnnotationMetadata getAnnotationMetadata(String classname) throws IOException + { MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(classname); AnnotationMetadata annotationMetaData = metadataReader.getAnnotationMetadata(); return annotationMetaData; - } - + } + public HttpResponse submitRequest(HttpMethod req, final RequestContext rq) throws HttpException, IOException - { + { try { final long start = System.currentTimeMillis(); - + final HttpRequestCallback callback = new HttpRequestCallback() { @Override @@ -250,14 +251,14 @@ public class PublicApiHttpClient { headersMap = new HashMap(headers.length); for (Header header : headers) - { - headersMap.put(header.getName(), header.getValue()); - } + { + headersMap.put(header.getName(), header.getValue()); + } } - + return new HttpResponse(method, rq.getRunAsUser(), method.getResponseBody(), headersMap, (end - start)); } - + @Override public boolean onError(HttpMethod method, Throwable t) { @@ -283,40 +284,40 @@ public class PublicApiHttpClient req.releaseConnection(); } } - } - - public HttpResponse get(final RequestContext rq, final String urlSuffix, Map params) throws IOException + } + + public HttpResponse get(final RequestContext rq, final String urlSuffix, Map params) throws IOException { RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), urlSuffix, params); String url = endpoint.getUrl(); - + GetMethod req = new GetMethod(url); return submitRequest(req, rq); } - + public HttpResponse get(final Class c, final RequestContext rq, final Object entityId, final Object relationshipEntityId, Map params) throws IOException { RestApiEndpoint endpoint = new RestApiEndpoint(c, rq.getNetworkId(), entityId, relationshipEntityId, params); String url = endpoint.getUrl(); - + GetMethod req = new GetMethod(url); return submitRequest(req, rq); } - + public HttpResponse get(final RequestContext rq, String scope, final String entityCollectionName, final Object entityId, final String relationCollectionName, final Object relationshipEntityId, Map params) throws IOException { return get(rq, scope, 1, entityCollectionName, entityId, relationCollectionName, relationshipEntityId, params); } - + public HttpResponse get(final RequestContext rq, final String scope, final int version, final String entityCollectionName, final Object entityId, final String relationCollectionName, final Object relationshipEntityId, Map params) throws IOException { RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), scope, version, entityCollectionName, entityId, relationCollectionName, relationshipEntityId, params); String url = endpoint.getUrl(); - + GetMethod req = new GetMethod(url); return submitRequest(req, rq); } @@ -338,7 +339,7 @@ public class PublicApiHttpClient return submitRequest(req, rq); } - public HttpResponse get(final String urlSuffix, final RequestContext rq, Map params) throws IOException + public HttpResponse get(final String urlSuffix, final RequestContext rq, Map params) throws IOException { RestApiEndpoint endpoint = new RestApiEndpoint(urlSuffix, params); String url = endpoint.getUrl(); @@ -347,15 +348,15 @@ public class PublicApiHttpClient return submitRequest(req, rq); } - public HttpResponse getWithPassword(final String urlSuffix, final RequestContext rq, Map params) throws IOException + public HttpResponse getWithPassword(final String urlSuffix, final RequestContext rq, Map params) throws IOException { RestApiEndpoint endpoint = new RestApiEndpoint(urlSuffix, params); String url = endpoint.getUrl(); - + GetMethod req = new GetMethod(url); return submitRequest(req, rq); } - + public HttpResponse post(final Class c, final RequestContext rq, final Object entityId, final Object relationshipEntityId, final String body) throws IOException { @@ -371,11 +372,11 @@ public class PublicApiHttpClient return submitRequest(req, rq); } - public HttpResponse post(final RequestContext rq, final String urlSuffix, String body) throws IOException + public HttpResponse post(final RequestContext rq, final String urlSuffix, String body) throws IOException { RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), urlSuffix, null); String url = endpoint.getUrl(); - + PostMethod req = new PostMethod(url.toString()); if (body != null) { @@ -384,12 +385,12 @@ public class PublicApiHttpClient } return submitRequest(req, rq); } - + public HttpResponse post(final RequestContext rq, Binding cmisBinding, String version, String cmisOperation, final String body) throws IOException { RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), cmisBinding, version, cmisOperation, null); String url = endpoint.getUrl(); - + PostMethod req = new PostMethod(url.toString()); if (body != null) { @@ -406,7 +407,7 @@ public class PublicApiHttpClient } return submitRequest(req, rq); } - + public HttpResponse put(final RequestContext rq, Binding cmisBinding, String version, String cmisOperation, final String body) throws IOException { RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), cmisBinding, version, cmisOperation, null); @@ -484,15 +485,15 @@ public class PublicApiHttpClient return submitRequest(req, rq); } - public HttpResponse post(final RequestContext rq, final String scope, final String entityCollectionName, final Object entityId, - final String relationCollectionName, final Object relationshipEntityId, final String body) throws IOException - { - return post(rq, scope, entityCollectionName, entityId, relationCollectionName, relationshipEntityId, body, "application/json"); - } - - public HttpResponse post(final RequestContext rq, final String scope, final String entityCollectionName, final Object entityId, - final String relationCollectionName, final Object relationshipEntityId, final String body, String contentType) throws IOException - { + public HttpResponse post(final RequestContext rq, final String scope, final String entityCollectionName, final Object entityId, + final String relationCollectionName, final Object relationshipEntityId, final String body) throws IOException + { + return post(rq, scope, entityCollectionName, entityId, relationCollectionName, relationshipEntityId, body, "application/json"); + } + + public HttpResponse post(final RequestContext rq, final String scope, final String entityCollectionName, final Object entityId, + final String relationCollectionName, final Object relationshipEntityId, final String body, String contentType) throws IOException + { return post(rq, scope, 1, entityCollectionName, entityId, relationCollectionName, relationshipEntityId, body, contentType); } @@ -500,22 +501,22 @@ public class PublicApiHttpClient final String relationCollectionName, final Object relationshipEntityId, final String body, String contentType) throws IOException { RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), scope, version, entityCollectionName, entityId, relationCollectionName, - relationshipEntityId, null); - String url = endpoint.getUrl(); - - PostMethod req = new PostMethod(url.toString()); - if (body != null) - { - if (contentType == null || contentType.isEmpty()) - { - contentType = "application/json"; - } - StringRequestEntity requestEntity = new StringRequestEntity(body, contentType, "UTF-8"); - req.setRequestEntity(requestEntity); - } - return submitRequest(req, rq); - } - + relationshipEntityId, null); + String url = endpoint.getUrl(); + + PostMethod req = new PostMethod(url.toString()); + if (body != null) + { + if (contentType == null || contentType.isEmpty()) + { + contentType = "application/json"; + } + StringRequestEntity requestEntity = new StringRequestEntity(body, contentType, "UTF-8"); + req.setRequestEntity(requestEntity); + } + return submitRequest(req, rq); + } + public HttpResponse post(final RequestContext rq, final String scope, final String entityCollectionName, final Object entityId, final String relationCollectionName, final Object relationshipEntityId, final byte[] body, String contentType) throws IOException { @@ -546,34 +547,34 @@ public class PublicApiHttpClient { RestApiEndpoint endpoint = new RestApiEndpoint(c, rq.getNetworkId(), entityId, relationshipEntityId, null); String url = endpoint.getUrl(); - + DeleteMethod req = new DeleteMethod(url); return submitRequest(req, rq); } - + public HttpResponse delete(final RequestContext rq, final String scope, final String entityCollectionName, final Object entityId, final String relationCollectionName, final Object relationshipEntityId) throws IOException { return delete(rq, scope, 1, entityCollectionName, entityId, relationCollectionName, relationshipEntityId, null); } - + public HttpResponse delete(final RequestContext rq, final String scope, final int version, final String entityCollectionName, final Object entityId, final String relationCollectionName, final Object relationshipEntityId, final Map params) throws IOException { RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), scope, version, entityCollectionName, entityId, relationCollectionName, relationshipEntityId, params); String url = endpoint.getUrl(); - + DeleteMethod req = new DeleteMethod(url); return submitRequest(req, rq); } - + public HttpResponse put(final Class c, final RequestContext rq, final Object entityId, final Object relationshipEntityId, final String body) throws IOException { RestApiEndpoint endpoint = new RestApiEndpoint(c, rq.getNetworkId(), entityId, relationshipEntityId, null); String url = endpoint.getUrl(); - + PutMethod req = new PutMethod(url); if (body != null) { @@ -582,7 +583,7 @@ public class PublicApiHttpClient } return submitRequest(req, rq); } - + public HttpResponse put(final RequestContext rq, final String scope, final String entityCollectionName, final Object entityId, final String relationCollectionName, final Object relationshipEntityId, final String body, final Map params) throws IOException { @@ -631,10 +632,10 @@ public class PublicApiHttpClient * Encapsulates information relating to a rest api end point, generating and * encoding urls based on the rest api implementation class. */ - private class RestApiEndpoint - { + private class RestApiEndpoint + { private String url; - + RestApiEndpoint(String url, Map params) throws IOException { StringBuilder sb = new StringBuilder( @@ -643,29 +644,29 @@ public class PublicApiHttpClient { sb.append(url); } - + addParams(sb, params); - + this.url = sb.toString(); } - + RestApiEndpoint(String tenantDomain, String url, Map params) throws IOException { StringBuilder sb = new StringBuilder(MessageFormat.format(OLD_BASE_URL, new Object[] { scheme, host, String.valueOf(port), contextPath, servletName, tenantDomain == null ? TenantUtil.DEFAULT_TENANT : tenantDomain })); sb.append("/"); sb.append(url); - + addParams(sb, params); - + this.url = sb.toString(); } - + RestApiEndpoint(Class resourceClass, String tenantDomain, Object collectionEntityId, Object relationEntityId, Map params) throws IOException { StringBuilder sb = new StringBuilder(); - + Api api = ResourceInspector.inspectApi(resourceClass); SCOPE scope = api.getScope(); int version = api.getVersion(); @@ -676,17 +677,17 @@ public class PublicApiHttpClient } Pair relationshipCollectionInfo = getRelationCollectionInfo(resourceClass); - + sb.append(MessageFormat.format(BASE_URL, new Object[] { scheme, host, String.valueOf(port), contextPath, servletName, tenantDomain == null ? TenantUtil.DEFAULT_TENANT : tenantDomain, scope.toString(), name, version })); - + if (relationshipCollectionInfo != null) { String entityCollectionName = relationshipCollectionInfo.getFirst(); String relationshipCollectionName = relationshipCollectionInfo.getSecond(); String relationEntityIdString = encodeToString(relationEntityId); String collectionEntityIdString = encodeToString(collectionEntityId); - + sb.append('/'); sb.append(entityCollectionName); sb.append('/'); @@ -695,7 +696,7 @@ public class PublicApiHttpClient sb.append(collectionEntityIdString); sb.append('/'); } - + sb.append(relationshipCollectionName); sb.append('/'); if (relationEntityIdString != null) @@ -710,7 +711,7 @@ public class PublicApiHttpClient if (entityCollectionName != null) { String collectionEntityIdString = encodeToString(collectionEntityId); - + sb.append('/'); sb.append(entityCollectionName); sb.append('/'); @@ -725,16 +726,16 @@ public class PublicApiHttpClient throw new RuntimeException(); } } - + addParams(sb, params); - + this.url = sb.toString(); } - + RestApiEndpoint(String networkId, Binding cmisBinding, String version, String cmisOperation, Map params) throws IOException { StringBuilder sb = new StringBuilder(); - + if (networkId != null) { sb.append(getPublicApiCmisUrl(networkId, cmisBinding, version, cmisOperation)); @@ -743,12 +744,12 @@ public class PublicApiHttpClient { throw new IllegalArgumentException(); } - + addParams(sb, params); - + this.url = sb.toString(); } - + RestApiEndpoint(String tenantDomain, String scope, int version, String collectionName, Object collectionEntityId, String relationName, Object relationEntityId, Map params) throws IOException { @@ -759,7 +760,7 @@ public class PublicApiHttpClient String relationName, Object relationEntityId, Map params) throws IOException { StringBuilder sb = new StringBuilder(); - + if (tenantDomain == null || tenantDomain.equals(TenantService.DEFAULT_DOMAIN)) { tenantDomain = TenantUtil.DEFAULT_TENANT; @@ -772,7 +773,7 @@ public class PublicApiHttpClient sb.append(MessageFormat.format(BASE_URL, new Object[] { scheme, host, String.valueOf(port), contextPath, servletName, tenantDomain, scope, apiName, version })); - + if (collectionName != null) { sb.append('/'); @@ -783,7 +784,7 @@ public class PublicApiHttpClient sb.append(collectionEntityId); } } - + if (relationName != null) { sb.append('/'); @@ -794,24 +795,24 @@ public class PublicApiHttpClient sb.append(relationEntityId); } } - + addParams(sb, params); - + this.url = sb.toString(); } - + RestApiEndpoint(String tenantDomain, String scope, String collectionName, Object collectionEntityId, String relationName, Object relationEntityId, Map params) throws IOException { this(tenantDomain, scope, 1, collectionName, collectionEntityId, relationName, relationEntityId, params); } - + private void addParams(StringBuilder sb, Map params) throws UnsupportedEncodingException { if (params != null && params.size() > 0) { sb.append("?"); - + for (String paramName : params.keySet()) { sb.append(URLEncoder.encode(paramName, "UTF-8")); @@ -824,8 +825,8 @@ public class PublicApiHttpClient } } - private String encodeToString(Object o) throws UnsupportedEncodingException - { + private String encodeToString(Object o) throws UnsupportedEncodingException + { String ret = null; if (o instanceof NodeRef) @@ -839,10 +840,10 @@ public class PublicApiHttpClient } return ret; - } + } - private Pair getRelationCollectionInfo(Class resourceClass) throws IOException - { + private Pair getRelationCollectionInfo(Class resourceClass) throws IOException + { AnnotationMetadata annotationMetaData = getAnnotationMetadata(resourceClass.getCanonicalName()); if (annotationMetaData.isConcrete() && annotationMetaData.isIndependent()) { @@ -851,7 +852,7 @@ public class PublicApiHttpClient Map attrs = annotationMetaData.getAnnotationAttributes(RelationshipResource.class.getName()); String relationshipCollectionName = (String) attrs.get("name"); Class entityResource = (Class) attrs.get("entityResource"); - + String entityCollectionName = getEntityCollectionInfo(entityResource.getCanonicalName()); Pair ret = new Pair(entityCollectionName, relationshipCollectionName); @@ -866,15 +867,15 @@ public class PublicApiHttpClient { throw new AlfrescoRuntimeException(""); } - } - - private String getEntityCollectionInfo(Class resourceClass) throws IOException - { + } + + private String getEntityCollectionInfo(Class resourceClass) throws IOException + { return getEntityCollectionInfo(resourceClass.getCanonicalName()); - } - - private String getEntityCollectionInfo(String className) throws IOException - { + } + + private String getEntityCollectionInfo(String className) throws IOException + { AnnotationMetadata annotationMetaData = getAnnotationMetadata(className); if (annotationMetaData.isConcrete() && annotationMetaData.isIndependent()) { @@ -892,13 +893,13 @@ public class PublicApiHttpClient { throw new AlfrescoRuntimeException(""); } - } + } public String getUrl() throws UnsupportedEncodingException { return url; } - } + } /** * @author Jamal Kaabi-Mofrad @@ -1305,4 +1306,4 @@ public class PublicApiHttpClient return req; } } -} +}