mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
Merged HEAD (5.2) to 5.2.N (5.2.1)
126380 jkaabimofrad: Merged FILE-FOLDER-API (5.2.0) to HEAD (5.2) 120084 jkaabimofrad: RA-640, RA-681: made "update node content" API to return the default JSON representation of the file node. Also added a test. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@126726 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -35,6 +35,7 @@ import org.alfresco.rest.api.tests.RepoService.TestPerson;
|
||||
import org.alfresco.rest.api.tests.RepoService.TestSite;
|
||||
import org.alfresco.rest.api.tests.client.HttpResponse;
|
||||
import org.alfresco.rest.api.tests.client.PublicApiClient;
|
||||
import org.alfresco.rest.api.tests.client.PublicApiHttpClient.BinaryPayload;
|
||||
import org.alfresco.rest.api.tests.client.RequestContext;
|
||||
import org.alfresco.service.cmr.site.SiteVisibility;
|
||||
|
||||
@@ -147,6 +148,27 @@ public abstract class AbstractBaseApiTest extends EnterpriseTestApi
|
||||
return response;
|
||||
}
|
||||
|
||||
protected HttpResponse putBinary(String url, int version, String runAsUser, BinaryPayload payload, String queryString, Map<String, String> params,
|
||||
int expectedStatus) throws Exception
|
||||
{
|
||||
publicApiClient.setRequestContext(new RequestContext(runAsUser));
|
||||
if (queryString != null)
|
||||
{
|
||||
url += queryString;
|
||||
}
|
||||
|
||||
HttpResponse response = publicApiClient.putBinary(getScope(), version, url, null, null, null, payload, params);
|
||||
checkStatus(expectedStatus, response.getStatusCode());
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
protected HttpResponse putBinary(String url, String runAsUser, BinaryPayload payload, String queryString, Map<String, String> params,
|
||||
int expectedStatus) throws Exception
|
||||
{
|
||||
return putBinary(url, 1, runAsUser, payload, queryString, params, expectedStatus);
|
||||
}
|
||||
|
||||
protected HttpResponse delete(String url, String runAsUser, String entityId, int expectedStatus) throws Exception
|
||||
{
|
||||
publicApiClient.setRequestContext(new RequestContext(runAsUser));
|
||||
|
@@ -34,6 +34,7 @@ import org.alfresco.repo.model.Repository;
|
||||
import org.alfresco.repo.security.authentication.AuthenticationUtil;
|
||||
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
|
||||
import org.alfresco.rest.api.Nodes;
|
||||
import org.alfresco.rest.api.tests.client.PublicApiHttpClient.BinaryPayload;
|
||||
import org.alfresco.rest.api.tests.client.data.ContentInfo;
|
||||
import org.alfresco.rest.api.tests.client.data.Document;
|
||||
import org.alfresco.rest.api.tests.client.data.Folder;
|
||||
@@ -62,6 +63,7 @@ import org.alfresco.service.cmr.security.MutableAuthenticationService;
|
||||
import org.alfresco.service.cmr.security.PermissionService;
|
||||
import org.alfresco.service.cmr.security.PersonService;
|
||||
import org.alfresco.service.cmr.site.SiteVisibility;
|
||||
import org.alfresco.util.TempFileProvider;
|
||||
import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.codehaus.jackson.map.annotate.JsonSerialize;
|
||||
import org.junit.After;
|
||||
@@ -69,6 +71,7 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.util.ResourceUtils;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
@@ -130,7 +133,7 @@ public class NodeApiTest extends AbstractBaseApiTest
|
||||
repositoryHelper = applicationContext.getBean("repositoryHelper", Repository.class);
|
||||
jacksonUtil = new JacksonUtil(applicationContext.getBean("jsonHelper", JacksonHelper.class));
|
||||
permissionService = applicationContext.getBean("permissionService", PermissionService.class);
|
||||
|
||||
|
||||
user1 = createUser("user1" + System.currentTimeMillis());
|
||||
user2 = createUser("user2" + System.currentTimeMillis());
|
||||
// We just need to clean the on-premise-users,
|
||||
@@ -1153,6 +1156,96 @@ public class NodeApiTest extends AbstractBaseApiTest
|
||||
put("nodes", user1, fId, toJsonAsStringNonNull(fUpdate), null, 400);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests update file content
|
||||
* <p>PUT:</p>
|
||||
* {@literal <host>:<port>/alfresco/api/-default-/public/alfresco/versions/1/nodes/<nodeId>/content}
|
||||
*/
|
||||
@Test
|
||||
public void testUpdateFileWithBinaryUpload() throws Exception
|
||||
{
|
||||
AuthenticationUtil.setFullyAuthenticatedUser(user1);
|
||||
|
||||
NodeRef myFilesNodeRef = repositoryHelper.getUserHome(personService.getPerson(user1));
|
||||
|
||||
Folder f1 = new Folder();
|
||||
f1.setName("F1");
|
||||
f1.setNodeType("cm:folder");
|
||||
|
||||
HttpResponse response = post(getChildrenUrl(myFilesNodeRef), user1, toJsonAsStringNonNull(f1), 201);
|
||||
Folder folderResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Folder.class);
|
||||
assertEquals(f1.getName(), folderResp.getName());
|
||||
final String f1_nodeId = folderResp.getId();
|
||||
assertNotNull(f1_nodeId);
|
||||
|
||||
Document doc = new Document();
|
||||
final String docName = "testdoc";
|
||||
doc.setName(docName);
|
||||
doc.setNodeType("cm:content");
|
||||
ContentInfo contentInfo = new ContentInfo();
|
||||
contentInfo.setMimeType(MimetypeMap.MIMETYPE_TEXT_PLAIN);
|
||||
doc.setContent(contentInfo);
|
||||
|
||||
// create an empty file within F1 folder
|
||||
response = post(getChildrenUrl(f1_nodeId), user1, toJsonAsStringNonNull(doc), 201);
|
||||
Document docResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
|
||||
assertEquals(docName, docResp.getName());
|
||||
assertNotNull(docResp.getContent());
|
||||
assertEquals(0, docResp.getContent().getSizeInBytes().intValue());
|
||||
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, docResp.getContent().getMimeType());
|
||||
|
||||
// Update content & Download URL
|
||||
final String url = "nodes/" + docResp.getId() + "/content";
|
||||
|
||||
// Update the empty node's content
|
||||
String content = "The quick brown fox jumps over the lazy dog.";
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(content.getBytes());
|
||||
File file = TempFileProvider.createTempFile(inputStream, getClass().getSimpleName(), ".txt");
|
||||
BinaryPayload payload = new BinaryPayload(file, MimetypeMap.MIMETYPE_TEXT_PLAIN);
|
||||
|
||||
// Try to update a folder!
|
||||
putBinary("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);
|
||||
|
||||
// Update the empty file
|
||||
response = putBinary(url, user1, payload, null, null, 200);
|
||||
docResp = RestApiUtil.parseRestApiEntry(response.getJsonResponse(), Document.class);
|
||||
assertEquals(docName, docResp.getName());
|
||||
assertNull(docResp.getPath());
|
||||
assertNotNull(docResp.getContent());
|
||||
assertTrue(docResp.getContent().getSizeInBytes().intValue() > 0);
|
||||
assertEquals(MimetypeMap.MIMETYPE_TEXT_PLAIN, docResp.getContent().getMimeType());
|
||||
|
||||
// Download the file
|
||||
response = getSingle(url, user1, null, 200);
|
||||
assertEquals(content, response.getResponse());
|
||||
|
||||
// Update the node's content again. Also, change the mimeType and make the response to return path!
|
||||
file = getResourceFile("quick.pdf");
|
||||
payload = new BinaryPayload(file, MimetypeMap.MIMETYPE_PDF);
|
||||
|
||||
response = putBinary(url + "?select=path", user1, payload, null, null, 200);
|
||||
docResp = jacksonUtil.parseEntry(response.getJsonResponse(), Document.class);
|
||||
assertEquals(docName, docResp.getName());
|
||||
assertNotNull(docResp.getContent());
|
||||
assertTrue(docResp.getContent().getSizeInBytes().intValue() > 0);
|
||||
assertEquals(MimetypeMap.MIMETYPE_PDF, docResp.getContent().getMimeType());
|
||||
PathInfo pathInfo = docResp.getPath();
|
||||
assertNotNull(pathInfo);
|
||||
assertTrue(pathInfo.getIsComplete());
|
||||
List<ElementInfo> pathElements = pathInfo.getElements();
|
||||
assertNotNull(pathElements);
|
||||
assertTrue(pathElements.size() > 0);
|
||||
// check the last element is F1
|
||||
assertEquals(f1.getName(), pathElements.get(pathElements.size() - 1).getName());
|
||||
|
||||
// Download the file
|
||||
response = getSingle(url, user1, null, 200);
|
||||
assertNotNull(content, response.getResponse());
|
||||
}
|
||||
|
||||
// TODO add test to create multiple folders & empty files (within same parent folder)
|
||||
|
||||
// TODO add test for file/folder links - creating, getting, listing, deleting
|
||||
|
@@ -33,6 +33,7 @@ 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.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
@@ -81,16 +82,21 @@ public class HttpResponse
|
||||
{
|
||||
requestType = "GET";
|
||||
}
|
||||
else if(method instanceof PutMethod)
|
||||
{
|
||||
requestType = "PUT";
|
||||
StringRequestEntity requestEntity = (StringRequestEntity)((PutMethod)method).getRequestEntity();
|
||||
if(requestEntity != null)
|
||||
{
|
||||
requestBody = requestEntity.getContent();
|
||||
}
|
||||
}
|
||||
else if(method instanceof PostMethod)
|
||||
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();
|
||||
}
|
||||
}
|
||||
else if(method instanceof PostMethod)
|
||||
{
|
||||
requestType = "POST";
|
||||
StringRequestEntity requestEntity = (StringRequestEntity)((PostMethod)method).getRequestEntity();
|
||||
|
@@ -39,6 +39,7 @@ import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl;
|
||||
import org.alfresco.opencmis.CMISDispatcherRegistry.Binding;
|
||||
import org.alfresco.rest.api.tests.client.PublicApiHttpClient.BinaryPayload;
|
||||
import org.alfresco.rest.api.tests.client.data.Activities;
|
||||
import org.alfresco.rest.api.tests.client.data.Activity;
|
||||
import org.alfresco.rest.api.tests.client.data.CMISNode;
|
||||
@@ -548,7 +549,18 @@ public class PublicApiClient
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
public HttpResponse putBinary(String scope, int version, String entityCollectionName, Object entityId, String relationCollectionName,
|
||||
Object relationshipEntityId, BinaryPayload payload, Map<String, String> params) throws IOException
|
||||
{
|
||||
HttpResponse response = client.putBinary(getRequestContext(), scope, version, entityCollectionName, entityId, relationCollectionName, relationshipEntityId,
|
||||
payload, params);
|
||||
|
||||
logger.debug(response.toString());
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
public HttpResponse delete(String scope, String entityCollectionName, Object entityId, String relationCollectionName, Object relationshipEntityId) throws IOException
|
||||
{
|
||||
HttpResponse response = client.delete(getRequestContext(), scope, entityCollectionName, entityId, relationCollectionName, relationshipEntityId);
|
||||
|
@@ -26,7 +26,12 @@
|
||||
|
||||
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.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.text.MessageFormat;
|
||||
@@ -55,6 +60,7 @@ 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.logging.Log;
|
||||
@@ -570,6 +576,23 @@ public class PublicApiHttpClient
|
||||
return submitRequest(req, rq);
|
||||
}
|
||||
|
||||
public HttpResponse putBinary(final RequestContext rq, final String scope, final int version, final String entityCollectionName,
|
||||
final Object entityId, final String relationCollectionName, final Object relationshipEntityId, final BinaryPayload payload,
|
||||
final Map<String, String> params) throws IOException
|
||||
{
|
||||
RestApiEndpoint endpoint = new RestApiEndpoint(rq.getNetworkId(), scope, version, entityCollectionName, entityId, relationCollectionName,
|
||||
relationshipEntityId, params);
|
||||
String url = endpoint.getUrl();
|
||||
|
||||
PutMethod req = new PutMethod(url);
|
||||
if (payload != null)
|
||||
{
|
||||
BinaryRequestEntity requestEntity = new BinaryRequestEntity(payload.getFile(), payload.getMimeType(), payload.getCharset());
|
||||
req.setRequestEntity(requestEntity);
|
||||
}
|
||||
return submitRequest(req, rq);
|
||||
}
|
||||
|
||||
/*
|
||||
* Encapsulates information relating to a rest api end point, generating and
|
||||
* encoding urls based on the rest api implementation class.
|
||||
@@ -824,4 +847,100 @@ public class PublicApiHttpClient
|
||||
return url;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Jamal Kaabi-Mofrad
|
||||
*/
|
||||
public static class BinaryRequestEntity implements RequestEntity
|
||||
{
|
||||
private final File file;
|
||||
private final String mimeType;
|
||||
private final String charset;
|
||||
|
||||
public BinaryRequestEntity(File file, String mimeType, String charset)
|
||||
{
|
||||
this.file = file;
|
||||
this.mimeType = (mimeType == null) ? "application/octet-stream" : mimeType;
|
||||
this.charset = (charset == null) ? "UTF-8" : charset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isRepeatable()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeRequest(OutputStream out) throws IOException
|
||||
{
|
||||
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
|
||||
try
|
||||
{
|
||||
int len;
|
||||
byte[] buffer = new byte[8190];
|
||||
while ((len = inputStream.read(buffer)) != -1)
|
||||
{
|
||||
out.write(buffer, 0, len);
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
inputStream.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getContentLength()
|
||||
{
|
||||
return file.length();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getContentType()
|
||||
{
|
||||
return mimeType + "; " + charset;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Jamal Kaabi-Mofrad
|
||||
*/
|
||||
public static class BinaryPayload
|
||||
{
|
||||
private File file;
|
||||
private String mimeType;
|
||||
private String charset;
|
||||
|
||||
public BinaryPayload(File file, String mimeType, String charset)
|
||||
{
|
||||
this.file = file;
|
||||
this.mimeType = mimeType;
|
||||
this.charset = charset;
|
||||
}
|
||||
|
||||
public BinaryPayload(File file, String mimeType)
|
||||
{
|
||||
this(file, mimeType, null);
|
||||
}
|
||||
|
||||
public BinaryPayload(File file)
|
||||
{
|
||||
this(file, null, null);
|
||||
}
|
||||
|
||||
public File getFile()
|
||||
{
|
||||
return file;
|
||||
}
|
||||
|
||||
public String getMimeType()
|
||||
{
|
||||
return mimeType;
|
||||
}
|
||||
|
||||
public String getCharset()
|
||||
{
|
||||
return charset;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user