diff --git a/source/test-java/org/alfresco/rest/api/tests/AbstractBaseApiTest.java b/source/test-java/org/alfresco/rest/api/tests/AbstractBaseApiTest.java index 231bd7a674..444c94a660 100644 --- a/source/test-java/org/alfresco/rest/api/tests/AbstractBaseApiTest.java +++ b/source/test-java/org/alfresco/rest/api/tests/AbstractBaseApiTest.java @@ -69,7 +69,20 @@ public abstract class AbstractBaseApiTest extends EnterpriseTestApi return response; } - protected HttpResponse post(String url, String runAsUser, String body, String queryString, String contentType, int expectedStatus) throws Exception + protected HttpResponse post(String url, String runAsUser, String body, String queryString, String contentType, int expectedStatus) throws Exception + { + publicApiClient.setRequestContext(new RequestContext(runAsUser)); + if (queryString != null) + { + url += queryString; + } + HttpResponse response = publicApiClient.post(getScope(), url, null, null, null, body, contentType); + checkStatus(expectedStatus, response.getStatusCode()); + + return response; + } + + protected HttpResponse post(String url, String runAsUser, byte[] body, String queryString, String contentType, int expectedStatus) throws Exception { publicApiClient.setRequestContext(new RequestContext(runAsUser)); if (queryString != null) diff --git a/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java b/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java index 8f1538a7b4..e142993172 100644 --- a/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java +++ b/source/test-java/org/alfresco/rest/api/tests/NodeApiTest.java @@ -20,12 +20,7 @@ package org.alfresco.rest.api.tests; import static org.alfresco.rest.api.tests.util.RestApiUtil.parsePaging; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.Assert.*; import org.alfresco.model.ContentModel; import org.alfresco.model.ForumModel; @@ -77,6 +72,8 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.Serializable; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -102,6 +99,8 @@ public class NodeApiTest extends AbstractBaseApiTest { private static final String RESOURCE_PREFIX = "publicapi/upload/"; + private static final String URL_NODES = "nodes/"; + /** * User one from network one */ @@ -547,10 +546,10 @@ public class NodeApiTest extends AbstractBaseApiTest MultiPartRequest reqBody = multiPartBuilder.build(); // Try to upload into a non-existent folder - post(getChildrenUrl(UUID.randomUUID().toString()), user1, new String(reqBody.getBody()), null, reqBody.getContentType(), 404); + post(getChildrenUrl(UUID.randomUUID().toString()), user1, reqBody.getBody(), null, reqBody.getContentType(), 404); // Upload - response = post(getChildrenUrl(Nodes.PATH_MY), user1, new String(reqBody.getBody()), null, reqBody.getContentType(), 201); + response = post(getChildrenUrl(Nodes.PATH_MY), user1, reqBody.getBody(), null, reqBody.getContentType(), 201); Document document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class); // Check the upload response assertEquals(fileName, document.getName()); @@ -576,7 +575,7 @@ public class NodeApiTest extends AbstractBaseApiTest assertEquals(numOfNodes + 1, pagingResult.getCount().intValue()); // Upload the same file again to check the name conflicts handling - post(getChildrenUrl(Nodes.PATH_MY), user1, new String(reqBody.getBody()), null, reqBody.getContentType(), 409); + post(getChildrenUrl(Nodes.PATH_MY), user1, reqBody.getBody(), null, reqBody.getContentType(), 409); response = getAll(getChildrenUrl(Nodes.PATH_MY), user1, paging, 200); pagingResult = parsePaging(response.getJsonResponse()); @@ -588,13 +587,13 @@ public class NodeApiTest extends AbstractBaseApiTest .setAutoRename(true) .build(); - response = post(getChildrenUrl(Nodes.PATH_MY), user1, new String(reqBody.getBody()), null, reqBody.getContentType(), 201); + response = post(getChildrenUrl(Nodes.PATH_MY), user1, reqBody.getBody(), null, reqBody.getContentType(), 201); document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class); // Check the upload response assertEquals("quick-1.pdf", document.getName()); // upload the same file again, and request the path info to be present in the response - response = post(getChildrenUrl(Nodes.PATH_MY), user1, new String(reqBody.getBody()), "?select=path", reqBody.getContentType(), 201); + response = post(getChildrenUrl(Nodes.PATH_MY), user1, reqBody.getBody(), "?select=path", reqBody.getContentType(), 201); document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class); // Check the upload response assertEquals("quick-2.pdf", document.getName()); @@ -613,7 +612,7 @@ public class NodeApiTest extends AbstractBaseApiTest reqBody = MultiPartBuilder.create() .setFileData(new FileData(fileName2, file2, MimetypeMap.MIMETYPE_TEXT_PLAIN)) .build(); - post(getChildrenUrl(user1Home.getId()), user2, new String(reqBody.getBody()), null, reqBody.getContentType(), 403); + post(getChildrenUrl(user1Home.getId()), user2, reqBody.getBody(), null, reqBody.getContentType(), 403); response = getAll(getChildrenUrl(Nodes.PATH_MY), user1, paging, 200); pagingResult = parsePaging(response.getJsonResponse()); @@ -621,11 +620,11 @@ public class NodeApiTest extends AbstractBaseApiTest assertEquals("Access Denied. The file shouldn't have been uploaded.", numOfNodes + 3, pagingResult.getCount().intValue()); // User1 tries to upload a file into a document rather than a folder! - post(getChildrenUrl(document.getId()), user1, new String(reqBody.getBody()), null, reqBody.getContentType(), 400); + post(getChildrenUrl(document.getId()), user1, reqBody.getBody(), null, reqBody.getContentType(), 400); // Try to upload a file without defining the required formData reqBody = MultiPartBuilder.create().setAutoRename(true).build(); - post(getChildrenUrl(Nodes.PATH_MY), user1, new String(reqBody.getBody()), null, reqBody.getContentType(), 400); + post(getChildrenUrl(Nodes.PATH_MY), user1, reqBody.getBody(), null, reqBody.getContentType(), 400); // Test unsupported node type reqBody = MultiPartBuilder.create() @@ -633,7 +632,7 @@ public class NodeApiTest extends AbstractBaseApiTest .setAutoRename(true) .setNodeType("cm:link") .build(); - post(getChildrenUrl(user1Home.getId()), user1, new String(reqBody.getBody()), null, reqBody.getContentType(), 400); + post(getChildrenUrl(user1Home.getId()), user1, reqBody.getBody(), null, reqBody.getContentType(), 400); } @@ -662,7 +661,7 @@ public class NodeApiTest extends AbstractBaseApiTest .setFileData(new FileData(fileName, file, null)); MultiPartRequest reqBody = multiPartBuilder.build(); // Try to upload - response = post(getChildrenUrl(folderA_Ref), userOneN1.getId(), new String(reqBody.getBody()), null, reqBody.getContentType(), 201); + response = post(getChildrenUrl(folderA_Ref), userOneN1.getId(), reqBody.getBody(), null, reqBody.getContentType(), 201); Document document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class); // Check the upload response assertEquals(fileName, document.getName()); @@ -686,7 +685,7 @@ public class NodeApiTest extends AbstractBaseApiTest assertEquals(numOfNodes + 1, pagingResult.getCount().intValue()); // Upload the same file again to check the name conflicts handling - post(getChildrenUrl(folderA_Ref), userOneN1.getId(), new String(reqBody.getBody()), null, reqBody.getContentType(), 409); + post(getChildrenUrl(folderA_Ref), userOneN1.getId(), reqBody.getBody(), null, reqBody.getContentType(), 409); response = getAll(getChildrenUrl(folderA_Ref), userOneN1.getId(), paging, 200); pagingResult = parsePaging(response.getJsonResponse()); @@ -699,10 +698,10 @@ public class NodeApiTest extends AbstractBaseApiTest .setFileData(new FileData(fileName2, file2, MimetypeMap.MIMETYPE_TEXT_PLAIN)) .build(); // userTwoN1 tries to upload a new file into the folderA of userOneN1 - post(getChildrenUrl(folderA_Ref), userTwoN1.getId(), new String(reqBody.getBody()), null, reqBody.getContentType(), 403); + post(getChildrenUrl(folderA_Ref), userTwoN1.getId(), reqBody.getBody(), null, reqBody.getContentType(), 403); // Test upload with properties - response = post(getChildrenUrl(folderA_Ref), userOneN1.getId(), new String(reqBody.getBody()), null, reqBody.getContentType(), 201); + response = post(getChildrenUrl(folderA_Ref), userOneN1.getId(), reqBody.getBody(), null, reqBody.getContentType(), 201); document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class); // Check the upload response assertEquals(fileName2, document.getName()); @@ -723,7 +722,7 @@ public class NodeApiTest extends AbstractBaseApiTest .setProperties(props) .build(); - response = post(getChildrenUrl(folderA_Ref), userOneN1.getId(), new String(reqBody.getBody()), null, reqBody.getContentType(), 201); + response = post(getChildrenUrl(folderA_Ref), userOneN1.getId(), reqBody.getBody(), null, reqBody.getContentType(), 201); document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class); // Check the upload response // "quick-2-1.txt" => fileName2 + autoRename @@ -744,7 +743,7 @@ public class NodeApiTest extends AbstractBaseApiTest .setProperties(props) .build(); // Prop prefix is unknown - post(getChildrenUrl(folderA_Ref), userOneN1.getId(), new String(reqBody.getBody()), null, reqBody.getContentType(), 400); + post(getChildrenUrl(folderA_Ref), userOneN1.getId(), reqBody.getBody(), null, reqBody.getContentType(), 400); } /** @@ -803,7 +802,7 @@ public class NodeApiTest extends AbstractBaseApiTest UserInfo expectedUser = new UserInfo(user1, user1+" "+user1); - String postUrl = "nodes/"+myFilesNodeRef.getId()+"/children"; + String postUrl = getChildrenUrl(myFilesNodeRef); Folder f1 = new Folder(); f1.setName("f1"); @@ -859,10 +858,10 @@ public class NodeApiTest extends AbstractBaseApiTest Folder f3 = new Folder(); f3.setName("f3"); f3.setNodeType("cm:folder"); - post("nodes/"+personNodeRef.getId()+"/children", user1, toJsonAsStringNonNull(f3), 400); + post(getChildrenUrl(personNodeRef), user1, toJsonAsStringNonNull(f3), 400); // -ve test - unknown parent folder node id - post("nodes/"+UUID.randomUUID().toString()+"/children", user1, toJsonAsStringNonNull(f3), 404); + post(getChildrenUrl(UUID.randomUUID().toString()), user1, toJsonAsStringNonNull(f3), 404); // -ve test - duplicate name post(postUrl, user1, toJsonAsStringNonNull(f1), 409); @@ -883,7 +882,7 @@ public class NodeApiTest extends AbstractBaseApiTest UserInfo expectedUser = new UserInfo(user1, user1+" "+user1); - String postUrl = "nodes/"+myFilesNodeRef.getId()+"/children"; + String postUrl = getChildrenUrl(myFilesNodeRef); Document d1 = new Document(); d1.setName("d1.txt"); @@ -954,10 +953,10 @@ public class NodeApiTest extends AbstractBaseApiTest Document d3 = new Document(); d3.setName("d3.txt"); d3.setNodeType("cm:content"); - post("nodes/"+personNodeRef.getId()+"/children", user1, toJsonAsStringNonNull(d3), 400); + post(getChildrenUrl(personNodeRef), user1, toJsonAsStringNonNull(d3), 400); // -ve test - unknown parent folder node id - post("nodes/"+UUID.randomUUID().toString()+"/children", user1, toJsonAsStringNonNull(d3), 404); + post(getChildrenUrl(UUID.randomUUID().toString()), user1, toJsonAsStringNonNull(d3), 404); // -ve test - duplicate name post(postUrl, user1, toJsonAsStringNonNull(d1), 409); @@ -978,7 +977,7 @@ public class NodeApiTest extends AbstractBaseApiTest UserInfo expectedUser = new UserInfo(user1, user1+" "+user1); - String postUrl = "nodes/"+myFilesNodeRef.getId()+"/children"; + String postUrl = getChildrenUrl(myFilesNodeRef); String folderName = "My Folder"; @@ -1195,7 +1194,7 @@ public class NodeApiTest extends AbstractBaseApiTest assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, docResp.getContent().getMimeType()); // Update content & Download URL - final String url = "nodes/" + docResp.getId() + "/content"; + final String url = URL_NODES + docResp.getId() + "/content"; // Update the empty node's content String content = "The quick brown fox jumps over the lazy dog."; @@ -1204,10 +1203,10 @@ public class NodeApiTest extends AbstractBaseApiTest BinaryPayload payload = new BinaryPayload(file, MimetypeMap.MIMETYPE_TEXT_PLAIN); // Try to update a folder! - putBinary("nodes/" + f1_nodeId + "/content", user1, payload, null, null, 400); + putBinary(URL_NODES + f1_nodeId + "/content", user1, payload, null, null, 400); // Try to update a non-existent file - putBinary("nodes/" + UUID.randomUUID().toString() + "/content", user1, payload, null, null, 404); + putBinary(URL_NODES + UUID.randomUUID().toString() + "/content", user1, payload, null, null, 404); // Update the empty file response = putBinary(url, user1, payload, null, null, 200); @@ -1250,6 +1249,81 @@ public class NodeApiTest extends AbstractBaseApiTest // TODO add test for file/folder links - creating, getting, listing, deleting + /** + * Tests download of file/content. + *

GET:

+ * {@literal :/alfresco/api/-default-/public/alfresco/versions/1/nodes//content} + */ + @Test + public void testDownloadFileContent() throws Exception + { + // + // Test plain text + // + + String fileName = "quick-1.txt"; + File file = getResourceFile(fileName); + + MultiPartBuilder multiPartBuilder = MultiPartBuilder.create() + .setFileData(new FileData(fileName, file, MimetypeMap.MIMETYPE_TEXT_PLAIN)); + MultiPartRequest reqBody = multiPartBuilder.build(); + + // Upload text content + HttpResponse response = post(getChildrenUrl(Nodes.PATH_MY), user1, reqBody.getBody(), null, reqBody.getContentType(), 201); + Document document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class); + + String contentNodeId = document.getId(); + + // Check the upload response + assertEquals(fileName, document.getName()); + ContentInfo contentInfo = document.getContent(); + assertNotNull(contentInfo); + assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, contentInfo.getMimeType()); + + // Download text content - by default with Content-Disposition header + response = getSingle(NodesEntityResource.class, user1, contentNodeId+"/content", null, 200); + + String textContent = response.getResponse(); + assertEquals("The quick brown fox jumps over the lazy dog", textContent); + assertEquals("attachment; filename=\"quick-1.txt\"; filename*=UTF-8''quick-1.txt", response.getHeaders().get("Content-Disposition")); + + + // + // Test binary (eg. PDF) + // + + fileName = "quick.pdf"; + file = getResourceFile(fileName); + byte[] originalBytes = Files.readAllBytes(Paths.get(file.getAbsolutePath())); + + multiPartBuilder = MultiPartBuilder.create() + .setFileData(new FileData(fileName, file, MimetypeMap.MIMETYPE_PDF)); + reqBody = multiPartBuilder.build(); + + // Upload binary content + response = post(getChildrenUrl(Nodes.PATH_MY), user1, reqBody.getBody(), null, reqBody.getContentType(), 201); + document = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class); + + contentNodeId = document.getId(); + + // Check the upload response + assertEquals(fileName, document.getName()); + contentInfo = document.getContent(); + assertNotNull(contentInfo); + assertEquals(MimetypeMap.MIMETYPE_PDF, contentInfo.getMimeType()); + + // Download binary content (as bytes) - without Content-Disposition header (attachment=false) + Map params = new LinkedHashMap<>(); + params.put("attachment", "false"); + + response = getSingle(NodesEntityResource.class, user1, contentNodeId+"/content", params, 200); + + byte[] bytes = response.getResponseAsBytes(); + + assertArrayEquals(originalBytes, bytes); + assertNull(response.getHeaders().get("Content-Disposition")); + } + private String getChildrenUrl(NodeRef parentNodeRef) { return getChildrenUrl(parentNodeRef.getId()); @@ -1257,7 +1331,7 @@ public class NodeApiTest extends AbstractBaseApiTest private String getChildrenUrl(String parentId) { - return "nodes/" + parentId + "/children"; + return URL_NODES + parentId + "/children"; } private File getResourceFile(String fileName) throws FileNotFoundException diff --git a/source/test-java/org/alfresco/rest/api/tests/client/HttpResponse.java b/source/test-java/org/alfresco/rest/api/tests/client/HttpResponse.java index 640a2331b2..7986dcc264 100644 --- a/source/test-java/org/alfresco/rest/api/tests/client/HttpResponse.java +++ b/source/test-java/org/alfresco/rest/api/tests/client/HttpResponse.java @@ -1,35 +1,56 @@ +/* + * Copyright (C) 2005-2015 Alfresco Software Limited. + * + * This file is part of Alfresco + * + * 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 . + */ + package org.alfresco.rest.api.tests.client; import java.util.Map; import org.apache.commons.httpclient.HttpMethod; +import org.apache.commons.httpclient.HttpMethodBase; import org.apache.commons.httpclient.URIException; +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.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.util.EncodingUtil; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; public class HttpResponse { - private HttpMethod method; + protected HttpMethod method; private String user; - private String response; + private byte[] responseBytes; private Map headers; private long time; - - public HttpResponse(HttpMethod method, String user, String response, Map headers, long time) + + public HttpResponse(HttpMethod method, String user, byte[] responseBytes, Map headers, long time) { - super(); this.method = method; this.user = user; this.time = time; this.headers = headers; - this.response = response; + this.responseBytes = responseBytes; } public int getStatusCode() @@ -39,7 +60,27 @@ public class HttpResponse public String getResponse() { - return response; + if (responseBytes != null) + { + if (method instanceof HttpMethodBase) + { + // mimic method.getResponseBodyAsString + return EncodingUtil.getString(responseBytes, ((HttpMethodBase)method).getResponseCharSet()); + } + else + { + return new String(responseBytes); + } + } + else + { + return null; + } + } + + public byte[] getResponseAsBytes() + { + return responseBytes; } public Map getHeaders() @@ -52,53 +93,69 @@ public class HttpResponse StringBuilder sb = new StringBuilder(); String requestType = null; - String requestBody = null; - if(method instanceof GetMethod) + RequestEntity requestEntity = null; + + if (method instanceof GetMethod) { requestType = "GET"; } else if (method instanceof PutMethod) { requestType = "PUT"; - RequestEntity requestEntity = ((PutMethod) method).getRequestEntity(); - if (requestEntity instanceof StringRequestEntity) - { - StringRequestEntity stringRequestEntity = (StringRequestEntity) requestEntity; - requestBody = stringRequestEntity.getContent(); - } - else if (requestEntity != null) - { - requestBody = requestEntity.toString(); - } + requestEntity = ((PutMethod) method).getRequestEntity(); } - else if(method instanceof PostMethod) + else if (method instanceof PostMethod) { requestType = "POST"; - StringRequestEntity requestEntity = (StringRequestEntity)((PostMethod)method).getRequestEntity(); - if(requestEntity != null) - { - requestBody = requestEntity.getContent(); - } + requestEntity = ((PostMethod)method).getRequestEntity(); } - else if(method instanceof DeleteMethod) + else if (method instanceof DeleteMethod) { requestType = "DELETE"; } try { - sb.append(requestType + " request " + method.getURI()); - sb.append("\n"); + sb.append(requestType).append(" request ").append(method.getURI()).append("\n"); } catch (URIException e) { } - sb.append(requestBody != null ? " \nbody = " + requestBody + "\n" : ""); - sb.append("user " + user); - sb.append("\n"); - sb.append("returned " + method.getStatusCode() + " and took " + time + "ms"); - sb.append("\n"); - sb.append("Response content " + response); + + if (requestEntity != null) + { + sb.append("\nRequest body: "); + if (requestEntity instanceof StringRequestEntity) + { + sb.append(((StringRequestEntity)requestEntity).getContent()); + } + else if (requestEntity instanceof ByteArrayRequestEntity) + { + sb.append(" << ").append(((ByteArrayRequestEntity)requestEntity).getContent().length).append(" bytes >>"); + } + sb.append("\n"); + } + + sb.append("user ").append(user).append("\n"); + sb.append("returned ").append(method.getStatusCode()).append(" and took ").append(time).append("ms").append("\n"); + + String contentType = method.getResponseHeader("Content-Type").getValue(); + sb.append("Response content type: ").append(contentType).append("\n"); + + if (contentType != null) + { + sb.append("\nResponse body: "); + if (contentType.startsWith("text/plain") || contentType.startsWith("application/json")) + { + sb.append(getResponse()); + } + else + { + sb.append(" << ").append(getResponseAsBytes().length).append(" bytes >>"); + } + sb.append("\n"); + } + return sb.toString(); } @@ -108,9 +165,10 @@ public class HttpResponse try { - if(response != null && response instanceof String) + String response = getResponse(); + if (response != null) { - Object object = new JSONParser().parse((String)response); + Object object = new JSONParser().parse(response); if(object instanceof JSONObject) { return (JSONObject) object; diff --git a/source/test-java/org/alfresco/rest/api/tests/client/PublicApiClient.java b/source/test-java/org/alfresco/rest/api/tests/client/PublicApiClient.java index 75651d35ec..b2bbd5c79b 100644 --- a/source/test-java/org/alfresco/rest/api/tests/client/PublicApiClient.java +++ b/source/test-java/org/alfresco/rest/api/tests/client/PublicApiClient.java @@ -452,6 +452,18 @@ public class PublicApiClient return response; } + + public HttpResponse post(String scope, String entityCollectionName, Object entityId, String relationCollectionName, Object relationshipEntityId, + byte[] body, String contentType) throws IOException + { + HttpResponse response = client.post(getRequestContext(), scope, entityCollectionName, entityId, relationCollectionName, + relationshipEntityId != null ? relationshipEntityId.toString() : null, body, contentType); + + logger.debug(response.toString()); + + return response; + } + public HttpResponse post(String urlSuffix, String body) throws IOException { HttpResponse response = client.post(getRequestContext(), urlSuffix, body); 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 fda4d85bf4..0facff8780 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 @@ -47,6 +47,7 @@ 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.methods.ByteArrayRequestEntity; import org.apache.commons.httpclient.methods.DeleteMethod; import org.apache.commons.httpclient.methods.GetMethod; import org.apache.commons.httpclient.methods.HeadMethod; @@ -245,7 +246,7 @@ public class PublicApiHttpClient } } - return new HttpResponse(method, rq.getRunAsUser(), method.getResponseBodyAsString(), headersMap, (end - start)); + return new HttpResponse(method, rq.getRunAsUser(), method.getResponseBody(), headersMap, (end - start)); } @Override @@ -506,6 +507,32 @@ 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 byte[] body, String contentType) throws IOException + { + return post(rq, scope, 1, entityCollectionName, entityId, relationCollectionName, relationshipEntityId, body, contentType); + } + + public HttpResponse post(final RequestContext rq, final String scope, final int version, final String entityCollectionName, final Object entityId, + final String relationCollectionName, final Object relationshipEntityId, final byte[] 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/octet-stream"; + } + ByteArrayRequestEntity requestEntity = new ByteArrayRequestEntity(body, contentType); + req.setRequestEntity(requestEntity); + } + return submitRequest(req, rq); + } + public HttpResponse delete(final Class c, final RequestContext rq, final Object entityId, final Object relationshipEntityId) throws IOException { RestApiEndpoint endpoint = new RestApiEndpoint(c, rq.getNetworkId(), entityId, relationshipEntityId, null);