Search in sources :

Example 6 with WebScriptRequest

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

the class RecognizedParamsExtractorTest method findPagingTest.

@Test
public void findPagingTest() {
    WebScriptRequest request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn("34");
    when(request.getParameter("maxItems")).thenReturn("50");
    Paging pagin = findPaging(request);
    assertNotNull(pagin);
    assertTrue(pagin.getSkipCount() == 34);
    assertTrue(pagin.getMaxItems() == 50);
    request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn(null);
    when(request.getParameter("maxItems")).thenReturn(null);
    pagin = findPaging(request);
    assertNotNull(pagin);
    assertTrue(pagin.getSkipCount() == Paging.DEFAULT_SKIP_COUNT);
    assertTrue(pagin.getMaxItems() == Paging.DEFAULT_MAX_ITEMS);
    request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn("55");
    pagin = findPaging(request);
    assertNotNull(pagin);
    assertTrue(pagin.getSkipCount() == 55);
    assertTrue(pagin.getMaxItems() == Paging.DEFAULT_MAX_ITEMS);
    request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn(null);
    when(request.getParameter("maxItems")).thenReturn("45");
    pagin = findPaging(request);
    assertNotNull(pagin);
    assertTrue(pagin.getMaxItems() == 45);
    assertTrue(pagin.getSkipCount() == Paging.DEFAULT_SKIP_COUNT);
    request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn("apple");
    when(request.getParameter("maxItems")).thenReturn("pear");
    try {
        pagin = findPaging(request);
        fail("Should not get here.");
    } catch (InvalidArgumentException iae) {
        // Must throw this exceptions
        assertNotNull(iae);
    }
    request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn("0");
    when(request.getParameter("maxItems")).thenReturn("0");
    try {
        pagin = findPaging(request);
        fail("Should not get here.");
    } catch (InvalidArgumentException iae) {
        // Must throw this exceptions
        assertNotNull(iae);
    }
    // Test Case cloud-2198
    request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn("0");
    when(request.getParameter("maxItems")).thenReturn("a");
    try {
        pagin = findPaging(request);
        fail("Should not get here.");
    } catch (InvalidArgumentException iae) {
        // Must throw this exceptions
        assertNotNull(iae);
    }
    request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn("s");
    when(request.getParameter("maxItems")).thenReturn("5");
    try {
        pagin = findPaging(request);
        fail("Should not get here.");
    } catch (InvalidArgumentException iae) {
        // Must throw this exceptions
        assertNotNull(iae);
    }
    request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn("0");
    when(request.getParameter("maxItems")).thenReturn("-2");
    try {
        pagin = findPaging(request);
        fail("Should not get here.");
    } catch (InvalidArgumentException iae) {
        // Must throw this exceptions
        assertNotNull(iae);
    }
    request = mock(WebScriptRequest.class);
    when(request.getParameter("skipCount")).thenReturn("-3");
    when(request.getParameter("maxItems")).thenReturn("5");
    try {
        pagin = findPaging(request);
        fail("Should not get here.");
    } catch (InvalidArgumentException iae) {
        // Must throw this exceptions
        assertNotNull(iae);
    }
    request = mock(WebScriptRequest.class);
    when(request.getParameter("maxItems")).thenReturn("5");
    pagin = findPaging(request);
    assertNotNull(pagin);
    assertTrue("skip count defaults to 0", pagin.getSkipCount() == Paging.DEFAULT_SKIP_COUNT);
// End of Test Case cloud-2198
}
Also used : WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) Paging(org.alfresco.rest.framework.resource.parameters.Paging) Test(org.junit.Test)

Example 7 with WebScriptRequest

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

the class RecognizedParamsExtractorTest method paramsTest.

@Test
public void paramsTest() {
    Map<String, List<String>> mockParams = new HashMap<String, List<String>>();
    mockParams.put("age", Arrays.asList("23", "45"));
    mockParams.put("name", Arrays.asList("fred"));
    WebScriptRequest request = mockRequest(mockParams);
    Map<String, String[]> params = getRequestParameters(request);
    assertNotNull(params);
    Params paramObj = ParamsExtender.valueOf(params);
    assertNotNull(paramObj);
    String aValue = paramObj.getParameter("age");
    assertEquals("23", aValue);
    aValue = paramObj.getParameter("name");
    assertEquals("fred", aValue);
}
Also used : WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) HashMap(java.util.HashMap) Params(org.alfresco.rest.framework.resource.parameters.Params) List(java.util.List) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 8 with WebScriptRequest

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

the class ExecutionTests method mockRequest.

private WebScriptRequest mockRequest(Map<String, String> templateVars, final Map<String, List<String>> params) {
    final String[] paramNames = params.keySet().toArray(new String[] {});
    WebScriptRequest request = mock(WebScriptRequest.class);
    when(request.getServiceMatch()).thenReturn(new Match(null, templateVars, null));
    when(request.getParameterNames()).thenReturn(paramNames);
    when(request.getParameterValues(anyString())).thenAnswer(new Answer<String[]>() {

        @Override
        public String[] answer(InvocationOnMock invocation) throws Throwable {
            Object[] args = invocation.getArguments();
            return params.get((String) args[0]).toArray(new String[] {});
        }
    });
    return request;
}
Also used : WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Matchers.anyString(org.mockito.Matchers.anyString) Match(org.springframework.extensions.webscripts.Match)

Example 9 with WebScriptRequest

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

the class AbortTransferCommandProcessor method process.

/*
     * (non-Javadoc)
     * 
     * @see org.alfresco.repo.web.scripts.transfer.CommandProcessor#process(org.alfresco .web.scripts.WebScriptRequest,
     * org.alfresco.web.scripts.WebScriptResponse)
     */
public int process(WebScriptRequest req, WebScriptResponse resp) {
    String transferRecordId = null;
    // Read the transfer id from the request
    // Unwrap to a WebScriptServletRequest if we have one
    // TODO: Why is this necessary?
    WebScriptServletRequest webScriptServletRequest = null;
    WebScriptRequest current = req;
    do {
        if (current instanceof WebScriptServletRequest) {
            webScriptServletRequest = (WebScriptServletRequest) current;
            current = null;
        } else if (current instanceof WrappingWebScriptRequest) {
            current = ((WrappingWebScriptRequest) req).getNext();
        } else {
            current = null;
        }
    } while (current != null);
    HttpServletRequest servletRequest = webScriptServletRequest.getHttpServletRequest();
    String transferId = servletRequest.getParameter("transferId");
    if ((transferId == null)) {
        resp.setStatus(Status.STATUS_BAD_REQUEST);
        return Status.STATUS_BAD_REQUEST;
    }
    try {
        logger.debug("abort transfer:" + transferId);
        receiver.cancel(transferId);
        // return the unique transfer id (the lock id)
        StringWriter stringWriter = new StringWriter(300);
        JSONWriter jsonWriter = new JSONWriter(stringWriter);
        jsonWriter.startObject();
        jsonWriter.writeValue("transferId", transferRecordId);
        jsonWriter.endObject();
        String response = stringWriter.toString();
        resp.setContentType("application/json");
        resp.setContentEncoding("UTF-8");
        int length = response.getBytes("UTF-8").length;
        resp.addHeader("Content-Length", "" + length);
        resp.setStatus(Status.STATUS_OK);
        resp.getWriter().write(response);
        return Status.STATUS_OK;
    } catch (Exception ex) {
        logger.debug("caught exception", ex);
        if (ex instanceof TransferException) {
            throw (TransferException) ex;
        }
        throw new TransferException(MSG_CAUGHT_UNEXPECTED_EXCEPTION, ex);
    }
}
Also used : WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest) WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONWriter(org.springframework.extensions.webscripts.json.JSONWriter) TransferException(org.alfresco.service.cmr.transfer.TransferException) StringWriter(java.io.StringWriter) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) TransferException(org.alfresco.service.cmr.transfer.TransferException)

Example 10 with WebScriptRequest

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

the class CommitTransferCommandProcessor method process.

/*
     * (non-Javadoc)
     * 
     * @see org.alfresco.repo.web.scripts.transfer.CommandProcessor#process(org.alfresco .web.scripts.WebScriptRequest,
     * org.alfresco.web.scripts.WebScriptResponse)
     */
public int process(WebScriptRequest req, WebScriptResponse resp) {
    // Read the transfer id from the request
    // Unwrap to a WebScriptServletRequest if we have one
    WebScriptServletRequest webScriptServletRequest = null;
    WebScriptRequest current = req;
    do {
        if (current instanceof WebScriptServletRequest) {
            webScriptServletRequest = (WebScriptServletRequest) current;
            current = null;
        } else if (current instanceof WrappingWebScriptRequest) {
            current = ((WrappingWebScriptRequest) req).getNext();
        } else {
            current = null;
        }
    } while (current != null);
    HttpServletRequest servletRequest = webScriptServletRequest.getHttpServletRequest();
    String transferId = servletRequest.getParameter("transferId");
    if ((transferId == null)) {
        logger.debug("transferId is missing");
        resp.setStatus(Status.STATUS_BAD_REQUEST);
        return Status.STATUS_BAD_REQUEST;
    }
    try {
        receiver.commitAsync(transferId);
        // return the unique transfer id (the lock id)
        StringWriter stringWriter = new StringWriter(300);
        JSONWriter jsonWriter = new JSONWriter(stringWriter);
        jsonWriter.startObject();
        jsonWriter.writeValue("transferId", transferId);
        jsonWriter.endObject();
        String response = stringWriter.toString();
        resp.setContentType("application/json");
        resp.setContentEncoding("UTF-8");
        int length = response.getBytes("UTF-8").length;
        resp.addHeader("Content-Length", "" + length);
        resp.setStatus(Status.STATUS_OK);
        resp.getWriter().write(response);
        return Status.STATUS_OK;
    } catch (Exception ex) {
        if (logger.isDebugEnabled()) {
            logger.debug("caught exception :" + ex.toString(), ex);
        }
        if (ex instanceof TransferException) {
            throw (TransferException) ex;
        }
        throw new TransferException(MSG_CAUGHT_UNEXPECTED_EXCEPTION, ex);
    }
}
Also used : WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest) WebScriptRequest(org.springframework.extensions.webscripts.WebScriptRequest) WrappingWebScriptRequest(org.springframework.extensions.webscripts.WrappingWebScriptRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONWriter(org.springframework.extensions.webscripts.json.JSONWriter) TransferException(org.alfresco.service.cmr.transfer.TransferException) StringWriter(java.io.StringWriter) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) TransferException(org.alfresco.service.cmr.transfer.TransferException)

Aggregations

WebScriptRequest (org.springframework.extensions.webscripts.WebScriptRequest)23 WrappingWebScriptRequest (org.springframework.extensions.webscripts.WrappingWebScriptRequest)10 WebScriptServletRequest (org.springframework.extensions.webscripts.servlet.WebScriptServletRequest)9 HashMap (java.util.HashMap)8 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 Test (org.junit.Test)8 Match (org.springframework.extensions.webscripts.Match)8 Params (org.alfresco.rest.framework.resource.parameters.Params)7 TransferException (org.alfresco.service.cmr.transfer.TransferException)7 Content (org.springframework.extensions.surf.util.Content)6 StringReader (java.io.StringReader)5 Matchers.anyString (org.mockito.Matchers.anyString)4 StringWriter (java.io.StringWriter)3 List (java.util.List)3 UnsupportedResourceOperationException (org.alfresco.rest.framework.core.exceptions.UnsupportedResourceOperationException)3 Farmer (org.alfresco.rest.framework.tests.api.mocks.Farmer)3 ResourceWebScriptPost (org.alfresco.rest.framework.webscripts.ResourceWebScriptPost)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 JSONWriter (org.springframework.extensions.webscripts.json.JSONWriter)3 File (java.io.File)2