Search in sources :

Example 1 with Request

use of org.springframework.extensions.webscripts.TestWebScriptServer.Request in project alfresco-remote-api by Alfresco.

the class LocalWebScriptConnectorServiceImpl method executeRequest.

/**
 * Executes the specified request, and return the response
 */
public RemoteConnectorResponse executeRequest(RemoteConnectorRequest request) throws IOException, AuthenticationException, RemoteConnectorClientException, RemoteConnectorServerException {
    // Convert the request object
    RemoteConnectorRequestImpl requestImpl = (RemoteConnectorRequestImpl) request;
    Request req = new Request(request.getMethod(), request.getURL());
    req.setType(request.getContentType());
    if (request.getRequestBody() != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        requestImpl.getRequestBody().writeRequest(baos);
        req.setBody(baos.toByteArray());
    }
    // Log
    if (logger.isInfoEnabled())
        logger.info("Performing local " + request.getMethod() + " request to " + request.getURL());
    // Capture the user details, as they may be changed during the request processing
    Authentication fullAuth = AuthenticationUtil.getFullAuthentication();
    String runAsUser = AuthenticationUtil.getRunAsUser();
    // If they've specified Authentication details in the request, clear our security context
    // and switch to that user, to avoid our context confusing the real request
    Header authHeader = null;
    Map<String, String> headers = new HashMap<String, String>();
    for (Header header : request.getRequestHeaders()) {
        if (header.getName().equals("Authorization")) {
            authHeader = header;
        }
        headers.put(header.getName(), header.getValue());
    }
    if (authHeader != null) {
        AuthenticationUtil.clearCurrentSecurityContext();
        if (logger.isDebugEnabled())
            logger.debug("HTTP Authorization found for the request, clearing security context, Auth is " + authHeader);
    }
    req.setHeaders(headers);
    // Execute the request against the WebScript Test Framework
    Response resp;
    try {
        resp = helper.sendRequest(req, -1);
    } catch (Exception e) {
        throw new AlfrescoRuntimeException("Problem requesting", e);
    }
    // Reset the user details, now we're done performing the request
    AuthenticationUtil.setFullAuthentication(fullAuth);
    if (runAsUser != null && !runAsUser.equals(fullAuth.getName())) {
        AuthenticationUtil.setRunAsUser(runAsUser);
    }
    // Log
    if (logger.isInfoEnabled())
        logger.info("Response to request was " + resp.getStatus() + " - " + resp);
    // Check the status for specific typed exceptions
    if (resp.getStatus() == Status.STATUS_UNAUTHORIZED) {
        throw new AuthenticationException("Not Authorized to access this resource");
    }
    if (resp.getStatus() == Status.STATUS_FORBIDDEN) {
        throw new AuthenticationException("Forbidden to access this resource");
    }
    // Check for failures where we don't care about the response body
    if (resp.getStatus() >= 500 && resp.getStatus() <= 599) {
        throw new RemoteConnectorServerException(resp.getStatus(), "(not available)");
    }
    // Convert the response into our required format
    String charset = null;
    String contentType = resp.getContentType();
    if (contentType != null && contentType.contains("charset=")) {
        int splitAt = contentType.indexOf("charset=") + "charset=".length();
        charset = contentType.substring(splitAt);
    }
    InputStream body = new ByteArrayInputStream(resp.getContentAsByteArray());
    // TODO Can't easily get the list...
    Header[] respHeaders = new Header[0];
    RemoteConnectorResponse response = new RemoteConnectorResponseImpl(request, contentType, charset, resp.getStatus(), respHeaders, body);
    // If it's a client error, let them know what went wrong
    if (resp.getStatus() >= 400 && resp.getStatus() <= 499) {
        throw new RemoteConnectorClientException(resp.getStatus(), "(not available)", response);
    }
    // Otherwise return the response for processing
    return response;
}
Also used : HashMap(java.util.HashMap) AuthenticationException(org.alfresco.repo.security.authentication.AuthenticationException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) RemoteConnectorRequest(org.alfresco.service.cmr.remoteconnector.RemoteConnectorRequest) Request(org.springframework.extensions.webscripts.TestWebScriptServer.Request) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RemoteConnectorResponse(org.alfresco.service.cmr.remoteconnector.RemoteConnectorResponse) ParseException(org.json.simple.parser.ParseException) AuthenticationException(org.alfresco.repo.security.authentication.AuthenticationException) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) RemoteConnectorClientException(org.alfresco.service.cmr.remoteconnector.RemoteConnectorClientException) RemoteConnectorServerException(org.alfresco.service.cmr.remoteconnector.RemoteConnectorServerException) Response(org.springframework.extensions.webscripts.TestWebScriptServer.Response) WebScriptServletResponse(org.springframework.extensions.webscripts.servlet.WebScriptServletResponse) RemoteConnectorResponse(org.alfresco.service.cmr.remoteconnector.RemoteConnectorResponse) RemoteConnectorClientException(org.alfresco.service.cmr.remoteconnector.RemoteConnectorClientException) Header(org.apache.commons.httpclient.Header) ByteArrayInputStream(java.io.ByteArrayInputStream) Authentication(net.sf.acegisecurity.Authentication) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) RemoteConnectorServerException(org.alfresco.service.cmr.remoteconnector.RemoteConnectorServerException)

Example 2 with Request

use of org.springframework.extensions.webscripts.TestWebScriptServer.Request in project alfresco-remote-api by Alfresco.

the class NodeWebScripTest method testLinkCreation.

@SuppressWarnings("unchecked")
public void testLinkCreation() throws Exception {
    // Create a folder within the DocLib
    NodeRef siteDocLib = siteService.getContainer(TEST_SITE.getShortName(), SiteService.DOCUMENT_LIBRARY);
    String testFolder1Name = "testingLinkCreationFolder1";
    Map<QName, Serializable> testFolderProps = new HashMap<QName, Serializable>();
    testFolderProps.put(ContentModel.PROP_NAME, testFolder1Name);
    NodeRef testFolder1 = nodeService.createNode(siteDocLib, ContentModel.ASSOC_CONTAINS, QName.createQName("testingLinkCreationFolder1"), ContentModel.TYPE_FOLDER, testFolderProps).getChildRef();
    JSONObject jsonReq = null;
    JSONObject json = null;
    JSONArray jsonArray = new JSONArray();
    JSONArray jsonLinkNodes = null;
    JSONObject jsonLinkNode = null;
    // Create files in the testFolder1
    NodeRef testFile1 = createNode(testFolder1, "testingLinkCreationFile1", ContentModel.TYPE_CONTENT, AuthenticationUtil.getAdminUserName());
    NodeRef testFile2 = createNode(testFolder1, "testingLinkCreationFile2", ContentModel.TYPE_CONTENT, AuthenticationUtil.getAdminUserName());
    NodeRef testFile3 = createNode(testFolder1, "testingLinkCreationFile3", ContentModel.TYPE_CONTENT, AuthenticationUtil.getAdminUserName());
    // Create testFolder2 in the testFolder1
    String testFolder2Name = "testingLinkCreationFolder2";
    testFolderProps = new HashMap<QName, Serializable>();
    testFolderProps.put(ContentModel.PROP_NAME, testFolder2Name);
    NodeRef testFolder2 = nodeService.createNode(siteDocLib, ContentModel.ASSOC_CONTAINS, QName.createQName("testingLinkCreationFolder2"), ContentModel.TYPE_FOLDER, testFolderProps).getChildRef();
    // Create link to file1 in same folder - testFolder1
    Request req = new Request("POST", CREATE_LINK_API + testFile1.getStoreRef().getProtocol() + "/" + testFile1.getStoreRef().getIdentifier() + "/" + testFile1.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, testFolder1.toString());
    jsonArray.add(testFile1.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    jsonLinkNodes = (JSONArray) json.get("linkNodes");
    assertNotNull(jsonLinkNodes);
    assertEquals(1, jsonLinkNodes.size());
    assertEquals("true", json.get("overallSuccess"));
    assertEquals("1", json.get("successCount"));
    assertEquals("0", json.get("failureCount"));
    jsonLinkNode = (JSONObject) jsonLinkNodes.get(0);
    String nodeRef = (String) jsonLinkNode.get("nodeRef");
    NodeRef file1Link = new NodeRef(nodeRef);
    // Check that app:linked aspect is added on sourceNode
    assertEquals(true, nodeService.hasAspect(testFile1, ApplicationModel.ASPECT_LINKED));
    assertEquals(true, nodeService.exists(file1Link));
    nodeService.deleteNode(file1Link);
    assertEquals(false, nodeService.hasAspect(testFile1, ApplicationModel.ASPECT_LINKED));
    // Create link to testFolder2 in same folder (testFolder1)
    req = new Request("POST", CREATE_LINK_API + testFolder2.getStoreRef().getProtocol() + "/" + testFolder2.getStoreRef().getIdentifier() + "/" + testFolder2.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, testFolder1.toString());
    jsonArray = new JSONArray();
    jsonArray.add(testFolder2.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    jsonLinkNodes = (JSONArray) json.get("linkNodes");
    assertNotNull(jsonLinkNodes);
    assertEquals(1, jsonLinkNodes.size());
    assertEquals("true", json.get("overallSuccess"));
    assertEquals("1", json.get("successCount"));
    assertEquals("0", json.get("failureCount"));
    jsonLinkNode = (JSONObject) jsonLinkNodes.get(0);
    nodeRef = (String) jsonLinkNode.get("nodeRef");
    NodeRef folder2Link = new NodeRef(nodeRef);
    assertEquals(true, nodeService.hasAspect(testFolder2, ApplicationModel.ASPECT_LINKED));
    assertEquals(true, nodeService.exists(folder2Link));
    // create another link of testFolder2 in siteDocLib
    req = new Request("POST", CREATE_LINK_API + testFolder2.getStoreRef().getProtocol() + "/" + testFolder2.getStoreRef().getIdentifier() + "/" + testFolder2.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, siteDocLib.toString());
    jsonArray = new JSONArray();
    jsonArray.add(testFolder2.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    jsonLinkNodes = (JSONArray) json.get("linkNodes");
    assertNotNull(jsonLinkNodes);
    assertEquals(1, jsonLinkNodes.size());
    assertEquals("true", json.get("overallSuccess"));
    assertEquals("1", json.get("successCount"));
    assertEquals("0", json.get("failureCount"));
    jsonLinkNode = (JSONObject) jsonLinkNodes.get(0);
    nodeRef = (String) jsonLinkNode.get("nodeRef");
    NodeRef folder2Link2 = new NodeRef(nodeRef);
    // delete folder2Link and check that aspect exists since we have another
    // link for testFolder2
    nodeService.deleteNode(folder2Link);
    assertEquals(true, nodeService.hasAspect(testFolder2, ApplicationModel.ASPECT_LINKED));
    nodeService.deleteNode(folder2Link2);
    assertEquals(false, nodeService.hasAspect(testFolder2, ApplicationModel.ASPECT_LINKED));
    // Create link to testFile1, testFile2 and testFile3 in same testFolder1
    req = new Request("POST", CREATE_LINK_API + testFolder1.getStoreRef().getProtocol() + "/" + testFolder1.getStoreRef().getIdentifier() + "/" + testFolder1.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, testFolder1.toString());
    jsonArray = new JSONArray();
    jsonArray.add(testFile1.toString());
    jsonArray.add(testFile2.toString());
    jsonArray.add(testFile3.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    jsonLinkNodes = (JSONArray) json.get("linkNodes");
    assertNotNull(jsonLinkNodes);
    assertEquals(3, jsonLinkNodes.size());
    assertEquals("true", json.get("overallSuccess"));
    assertEquals("3", json.get("successCount"));
    assertEquals("0", json.get("failureCount"));
    NodeRef fileLink = null;
    List<NodeRef> fileLinks = new ArrayList<NodeRef>();
    for (int i = 0; i < jsonLinkNodes.size(); i++) {
        jsonLinkNode = (JSONObject) jsonLinkNodes.get(i);
        nodeRef = (String) jsonLinkNode.get("nodeRef");
        fileLink = new NodeRef(nodeRef);
        fileLinks.add(fileLink);
        assertEquals(true, nodeService.exists(fileLink));
    }
    // try to create another link in the same location - an exception should be thrown
    req = new Request("POST", CREATE_LINK_API + testFolder1.getStoreRef().getProtocol() + "/" + testFolder1.getStoreRef().getIdentifier() + "/" + testFolder1.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, testFolder1.toString());
    jsonArray = new JSONArray();
    jsonArray.add(testFile1.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_BAD_REQUEST));
    // delete all 3 files and check that the links are deleted too
    nodeService.deleteNode(testFile1);
    nodeService.deleteNode(testFile2);
    nodeService.deleteNode(testFile3);
    for (NodeRef linkNodeRef : fileLinks) {
        assertEquals(false, nodeService.exists(linkNodeRef));
    }
    // try create a link to a site - shouldn't be possible
    SiteInfo site2 = createSite("Site2TestingNodeCreateLink");
    NodeRef siteNodeRef = site2.getNodeRef();
    req = new Request("POST", CREATE_LINK_API + testFolder1.getStoreRef().getProtocol() + "/" + testFolder1.getStoreRef().getIdentifier() + "/" + testFolder1.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, testFolder1.toString());
    jsonArray = new JSONArray();
    jsonArray.add(siteNodeRef.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_BAD_REQUEST));
    // create a file and a link to that file inside the documentLibrary
    NodeRef site2DocLib = siteService.getContainer(TEST_SITE.getShortName(), SiteService.DOCUMENT_LIBRARY);
    NodeRef testFileSite2 = createNode(site2DocLib, "testingLinkCreationFileInSite2", ContentModel.TYPE_CONTENT, AuthenticationUtil.getAdminUserName());
    req = new Request("POST", CREATE_LINK_API + testFolder1.getStoreRef().getProtocol() + "/" + testFolder1.getStoreRef().getIdentifier() + "/" + testFolder1.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, site2DocLib.toString());
    jsonArray = new JSONArray();
    jsonArray.add(testFileSite2.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    // links are created with success, try to delete site2 - should succeed
    siteService.deleteSite(site2.getShortName());
    nodeArchiveService.purgeArchivedNode(nodeArchiveService.getArchivedNode(siteNodeRef));
    // Links can be created in Shared Files, My Files and Repository
    NodeRef testFile4 = createNode(testFolder1, "testingLinkCreationFile4", ContentModel.TYPE_CONTENT, AuthenticationUtil.getAdminUserName());
    req = new Request("POST", CREATE_LINK_API + testFile4.getStoreRef().getProtocol() + "/" + testFile4.getStoreRef().getIdentifier() + "/" + testFile4.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, "alfresco://company/shared");
    jsonArray = new JSONArray();
    jsonArray.add(testFile4.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    req = new Request("POST", CREATE_LINK_API + testFile4.getStoreRef().getProtocol() + "/" + testFile4.getStoreRef().getIdentifier() + "/" + testFile4.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, "alfresco://user/home");
    jsonArray = new JSONArray();
    jsonArray.add(testFile4.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    // create link in Repository as Admin
    AuthenticationUtil.setAdminUserAsFullyAuthenticatedUser();
    req = new Request("POST", CREATE_LINK_API + testFile4.getStoreRef().getProtocol() + "/" + testFile4.getStoreRef().getIdentifier() + "/" + testFile4.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, "alfresco://company/home");
    jsonArray = new JSONArray();
    jsonArray.add(testFile4.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    // all 3 links are created with success, delete all links
    req = new Request("DELETE", CREATE_LINK_API + testFile4.getStoreRef().getProtocol() + "/" + testFile4.getStoreRef().getIdentifier() + "/" + testFile4.getId() + "/delete");
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    // all links are removed with success marker aspect should be removed too
    assertEquals(false, nodeService.hasAspect(testFile4, ApplicationModel.ASPECT_LINKED));
    AuthenticationUtil.setFullyAuthenticatedUser(USER_ONE);
    // you should not be able to create links for locked nodes but you can delete links
    final NodeRef testFile5 = createNode(testFolder1, "testingLinkCreationFileWithLock", ContentModel.TYPE_CONTENT, AuthenticationUtil.getAdminUserName());
    req = new Request("POST", CREATE_LINK_API + testFile5.getStoreRef().getProtocol() + "/" + testFile5.getStoreRef().getIdentifier() + "/" + testFile5.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, testFolder1.toString());
    jsonArray = new JSONArray();
    jsonArray.add(testFile5.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    jsonLinkNodes = (JSONArray) json.get("linkNodes");
    assertNotNull(jsonLinkNodes);
    assertEquals(1, jsonLinkNodes.size());
    assertEquals("true", json.get("overallSuccess"));
    assertEquals("1", json.get("successCount"));
    assertEquals("0", json.get("failureCount"));
    jsonLinkNode = (JSONObject) jsonLinkNodes.get(0);
    nodeRef = (String) jsonLinkNode.get("nodeRef");
    NodeRef file5Link = new NodeRef(nodeRef);
    assertEquals(true, nodeService.hasAspect(testFile5, ApplicationModel.ASPECT_LINKED));
    assertEquals(true, nodeService.exists(file5Link));
    // checkout the node testFile5
    final NodeRef workingCopy = retryingTransactionHelper.doInTransaction(new RetryingTransactionCallback<NodeRef>() {

        public NodeRef execute() throws Exception {
            return checkOutCheckInService.checkout(testFile5);
        }
    });
    assertNotNull(workingCopy);
    // try to create link for working copy
    req = new Request("POST", CREATE_LINK_API + workingCopy.getStoreRef().getProtocol() + "/" + workingCopy.getStoreRef().getIdentifier() + "/" + workingCopy.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, testFolder1.toString());
    jsonArray = new JSONArray();
    jsonArray.add(workingCopy.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    // should fail since source node is working copy
    json = asJSON(sendRequest(req, Status.STATUS_BAD_REQUEST));
    // delete the link when source node is locked
    nodeService.deleteNode(file5Link);
    assertEquals(false, nodeService.hasAspect(testFile5, ApplicationModel.ASPECT_LINKED));
    // release the lock
    retryingTransactionHelper.doInTransaction(new RetryingTransactionCallback<Object>() {

        public Object execute() throws Exception {
            checkOutCheckInService.checkin(workingCopy, new HashMap<String, Serializable>());
            return null;
        }
    });
    assertEquals(false, nodeService.hasAspect(testFile5, ContentModel.ASPECT_LOCKABLE));
    // test that Consumers can create and delete links if they have only read permission
    NodeRef testFile6 = createNode(testFolder1, "testingLinkCreationFile6", ContentModel.TYPE_CONTENT, AuthenticationUtil.getAdminUserName());
    AuthenticationUtil.setFullyAuthenticatedUser(USER_TWO);
    assertTrue(permissionService.hasPermission(testFile6, PermissionService.READ) == AccessStatus.ALLOWED);
    assertTrue(permissionService.hasPermission(testFile6, PermissionService.WRITE) == AccessStatus.DENIED);
    // create another link of testFile1 in MyFiles
    req = new Request("POST", CREATE_LINK_API + testFile6.getStoreRef().getProtocol() + "/" + testFile6.getStoreRef().getIdentifier() + "/" + testFile6.getId());
    jsonReq = new JSONObject();
    jsonReq.put(DESTINATION_NODE_REF_PARAM, "alfresco://user/home");
    jsonArray = new JSONArray();
    jsonArray.add(testFile6.toString());
    jsonReq.put(MULTIPLE_FILES_PARAM, jsonArray);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    jsonLinkNodes = (JSONArray) json.get("linkNodes");
    assertNotNull(jsonLinkNodes);
    assertEquals(1, jsonLinkNodes.size());
    assertEquals("true", json.get("overallSuccess"));
    assertEquals("1", json.get("successCount"));
    assertEquals("0", json.get("failureCount"));
    jsonLinkNode = (JSONObject) jsonLinkNodes.get(0);
    nodeRef = (String) jsonLinkNode.get("nodeRef");
    NodeRef testFileSite3Link = new NodeRef(nodeRef);
    nodeService.exists(testFileSite3Link);
    assertEquals(true, nodeService.hasAspect(testFile6, ApplicationModel.ASPECT_LINKED));
}
Also used : SiteInfo(org.alfresco.service.cmr.site.SiteInfo) Serializable(java.io.Serializable) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) JSONArray(org.json.simple.JSONArray) Request(org.springframework.extensions.webscripts.TestWebScriptServer.Request) ArrayList(java.util.ArrayList) NodeRef(org.alfresco.service.cmr.repository.NodeRef) JSONObject(org.json.simple.JSONObject) JSONObject(org.json.simple.JSONObject)

Example 3 with Request

use of org.springframework.extensions.webscripts.TestWebScriptServer.Request in project alfresco-remote-api by Alfresco.

the class NodeWebScripTest method testFolderCreation.

@SuppressWarnings("unchecked")
public void testFolderCreation() throws Exception {
    // Create a folder within the DocLib
    NodeRef siteDocLib = siteService.getContainer(TEST_SITE.getShortName(), SiteService.DOCUMENT_LIBRARY);
    String testFolderName = "testing";
    Map<QName, Serializable> testFolderProps = new HashMap<QName, Serializable>();
    testFolderProps.put(ContentModel.PROP_NAME, testFolderName);
    NodeRef testFolder = nodeService.createNode(siteDocLib, ContentModel.ASSOC_CONTAINS, QName.createQName("testing"), ContentModel.TYPE_FOLDER, testFolderProps).getChildRef();
    String testNodeName = "aNEWfolder";
    String testNodeTitle = "aTITLEforAfolder";
    String testNodeDescription = "DESCRIPTIONofAfolder";
    JSONObject jsonReq = null;
    JSONObject json = null;
    NodeRef folder = null;
    // By NodeID
    Request req = new Request("POST", "/api/node/folder/" + testFolder.getStoreRef().getProtocol() + "/" + testFolder.getStoreRef().getIdentifier() + "/" + testFolder.getId());
    jsonReq = new JSONObject();
    jsonReq.put("name", testNodeName);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    assertNotNull(json.get("nodeRef"));
    folder = new NodeRef((String) json.get("nodeRef"));
    assertEquals(true, nodeService.exists(folder));
    assertEquals(testNodeName, nodeService.getProperty(folder, ContentModel.PROP_NAME));
    assertEquals(testNodeName, nodeService.getProperty(folder, ContentModel.PROP_TITLE));
    assertEquals(null, nodeService.getProperty(folder, ContentModel.PROP_DESCRIPTION));
    assertEquals(testFolder, nodeService.getPrimaryParent(folder).getParentRef());
    assertEquals(ContentModel.TYPE_FOLDER, nodeService.getType(folder));
    nodeService.deleteNode(folder);
    // In a Site Container
    req = new Request("POST", "/api/site/folder/" + TEST_SITE_NAME + "/" + SiteService.DOCUMENT_LIBRARY);
    jsonReq = new JSONObject();
    jsonReq.put("name", testNodeName);
    jsonReq.put("description", testNodeDescription);
    req.setBody(jsonReq.toString().getBytes());
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    assertNotNull(json.get("nodeRef"));
    folder = new NodeRef((String) json.get("nodeRef"));
    assertEquals(true, nodeService.exists(folder));
    assertEquals(testNodeName, nodeService.getProperty(folder, ContentModel.PROP_NAME));
    assertEquals(testNodeName, nodeService.getProperty(folder, ContentModel.PROP_TITLE));
    assertEquals(testNodeDescription, nodeService.getProperty(folder, ContentModel.PROP_DESCRIPTION));
    assertEquals(siteDocLib, nodeService.getPrimaryParent(folder).getParentRef());
    assertEquals(ContentModel.TYPE_FOLDER, nodeService.getType(folder));
    nodeService.deleteNode(folder);
    // A Child of a Site Container
    req = new Request("POST", "/api/site/folder/" + TEST_SITE_NAME + "/" + SiteService.DOCUMENT_LIBRARY + "/" + testFolderName);
    jsonReq = new JSONObject();
    jsonReq.put("name", testNodeName);
    jsonReq.put("title", testNodeTitle);
    jsonReq.put("description", testNodeDescription);
    req.setBody(jsonReq.toString().getBytes());
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    assertNotNull(json.get("nodeRef"));
    folder = new NodeRef((String) json.get("nodeRef"));
    assertEquals(true, nodeService.exists(folder));
    assertEquals(testNodeName, nodeService.getProperty(folder, ContentModel.PROP_NAME));
    assertEquals(testNodeTitle, nodeService.getProperty(folder, ContentModel.PROP_TITLE));
    assertEquals(testNodeDescription, nodeService.getProperty(folder, ContentModel.PROP_DESCRIPTION));
    assertEquals(testFolder, nodeService.getPrimaryParent(folder).getParentRef());
    assertEquals(ContentModel.TYPE_FOLDER, nodeService.getType(folder));
    nodeService.deleteNode(folder);
    // Type needs to be a subtype of folder
    // explicit cm:folder
    req = new Request("POST", "/api/site/folder/" + TEST_SITE_NAME + "/" + SiteService.DOCUMENT_LIBRARY + "/" + testFolderName);
    jsonReq = new JSONObject();
    jsonReq.put("name", testNodeName);
    jsonReq.put("type", "cm:folder");
    req.setBody(jsonReq.toString().getBytes());
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    assertNotNull(json.get("nodeRef"));
    folder = new NodeRef((String) json.get("nodeRef"));
    assertEquals(true, nodeService.exists(folder));
    assertEquals(testNodeName, nodeService.getProperty(folder, ContentModel.PROP_NAME));
    assertEquals(ContentModel.TYPE_FOLDER, nodeService.getType(folder));
    nodeService.deleteNode(folder);
    // cm:systemfolder extends from cm:folder
    req = new Request("POST", "/api/site/folder/" + TEST_SITE_NAME + "/" + SiteService.DOCUMENT_LIBRARY + "/" + testFolderName);
    jsonReq = new JSONObject();
    jsonReq.put("name", testNodeName);
    jsonReq.put("type", "cm:systemfolder");
    req.setBody(jsonReq.toString().getBytes());
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    assertNotNull(json.get("nodeRef"));
    folder = new NodeRef((String) json.get("nodeRef"));
    assertEquals(true, nodeService.exists(folder));
    assertEquals(testNodeName, nodeService.getProperty(folder, ContentModel.PROP_NAME));
    assertEquals(ContentModel.TYPE_SYSTEM_FOLDER, nodeService.getType(folder));
    nodeService.deleteNode(folder);
    // cm:content isn't allowed
    req = new Request("POST", "/api/site/folder/" + TEST_SITE_NAME + "/" + SiteService.DOCUMENT_LIBRARY + "/" + testFolderName);
    jsonReq = new JSONObject();
    jsonReq.put("name", testNodeName);
    jsonReq.put("type", "cm:content");
    req.setBody(jsonReq.toString().getBytes());
    sendRequest(req, Status.STATUS_BAD_REQUEST);
    // Check permissions - need to be Contributor
    AuthenticationUtil.setFullyAuthenticatedUser(USER_ONE);
    req = new Request("POST", "/api/node/folder/" + testFolder.getStoreRef().getProtocol() + "/" + testFolder.getStoreRef().getIdentifier() + "/" + testFolder.getId());
    jsonReq = new JSONObject();
    jsonReq.put("name", testNodeName);
    req.setBody(jsonReq.toString().getBytes());
    req.setType(MimetypeMap.MIMETYPE_JSON);
    json = asJSON(sendRequest(req, Status.STATUS_OK));
    assertNotNull(json.get("nodeRef"));
    folder = new NodeRef((String) json.get("nodeRef"));
    assertEquals(true, nodeService.exists(folder));
    nodeService.deleteNode(folder);
    AuthenticationUtil.setFullyAuthenticatedUser(USER_TWO);
    sendRequest(req, Status.STATUS_FORBIDDEN);
}
Also used : NodeRef(org.alfresco.service.cmr.repository.NodeRef) Serializable(java.io.Serializable) JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) QName(org.alfresco.service.namespace.QName) Request(org.springframework.extensions.webscripts.TestWebScriptServer.Request)

Aggregations

HashMap (java.util.HashMap)3 Request (org.springframework.extensions.webscripts.TestWebScriptServer.Request)3 Serializable (java.io.Serializable)2 NodeRef (org.alfresco.service.cmr.repository.NodeRef)2 QName (org.alfresco.service.namespace.QName)2 JSONObject (org.json.simple.JSONObject)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 Authentication (net.sf.acegisecurity.Authentication)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 AuthenticationException (org.alfresco.repo.security.authentication.AuthenticationException)1 RemoteConnectorClientException (org.alfresco.service.cmr.remoteconnector.RemoteConnectorClientException)1 RemoteConnectorRequest (org.alfresco.service.cmr.remoteconnector.RemoteConnectorRequest)1 RemoteConnectorResponse (org.alfresco.service.cmr.remoteconnector.RemoteConnectorResponse)1 RemoteConnectorServerException (org.alfresco.service.cmr.remoteconnector.RemoteConnectorServerException)1 SiteInfo (org.alfresco.service.cmr.site.SiteInfo)1 Header (org.apache.commons.httpclient.Header)1