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
}
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);
}
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;
}
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);
}
}
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);
}
}
Aggregations