use of org.springframework.extensions.webscripts.servlet.WebScriptServletRequest in project alfresco-remote-api by Alfresco.
the class RemoteAuthenticatorFactoryTest method testLogInWithNonExistingPerson.
@Test
public void testLogInWithNonExistingPerson() {
// Random non existing person
final String username = GUID.generate();
// Mock a request with a username in the header
HttpServletRequest mockHttpRequest = mock(HttpServletRequest.class);
when(mockHttpRequest.getHeader("X-Alfresco-Remote-User")).thenReturn(username);
when(mockHttpRequest.getScheme()).thenReturn("http");
WebScriptServletRequest mockRequest = mock(WebScriptServletRequest.class);
when(mockRequest.getHttpServletRequest()).thenReturn(mockHttpRequest);
HttpServletResponse mockHttpResponse = mock(HttpServletResponse.class);
WebScriptServletResponse mockResponse = mock(WebScriptServletResponse.class);
when(mockResponse.getHttpServletResponse()).thenReturn(mockHttpResponse);
Authenticator authenticator = remoteUserAuthenticatorFactory.create(mockRequest, mockResponse);
assertTrue("The non existing user should be authenticated.", authenticator.authenticate(RequiredAuthentication.user, false));
assertTrue("The user should be auto created.", personService.personExists(username));
}
use of org.springframework.extensions.webscripts.servlet.WebScriptServletRequest 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.servlet.WebScriptServletRequest 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);
}
}
use of org.springframework.extensions.webscripts.servlet.WebScriptServletRequest in project alfresco-remote-api by Alfresco.
the class PostContentCommandProcessor 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) {
logger.debug("post content start");
// 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);
if (webScriptServletRequest == null) {
resp.setStatus(Status.STATUS_BAD_REQUEST);
return Status.STATUS_BAD_REQUEST;
}
HttpServletRequest servletRequest = webScriptServletRequest.getHttpServletRequest();
// Read the transfer id from the request
String transferId = servletRequest.getParameter("transferId");
if ((transferId == null) || !ServletFileUpload.isMultipartContent(servletRequest)) {
resp.setStatus(Status.STATUS_BAD_REQUEST);
return Status.STATUS_BAD_REQUEST;
}
try {
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(servletRequest);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
if (!item.isFormField()) {
logger.debug("got content Mime Part : " + name);
receiver.saveContent(transferId, item.getName(), item.openStream());
}
}
// WebScriptServletRequest alfRequest = (WebScriptServletRequest)req;
// String[] names = alfRequest.getParameterNames();
// for(String name : names)
// {
// FormField item = alfRequest.getFileField(name);
//
// if(item != null)
// {
// logger.debug("got content Mime Part : " + name);
// receiver.saveContent(transferId, item.getName(), item.getInputStream());
// }
// else
// {
// //TODO - should this be an exception?
// logger.debug("Unable to get content for Mime Part : " + name);
// }
// }
logger.debug("success");
resp.setStatus(Status.STATUS_OK);
} catch (Exception ex) {
logger.debug("exception caught", ex);
if (transferId != null) {
logger.debug("ending transfer", ex);
receiver.end(transferId);
}
if (ex instanceof TransferException) {
throw (TransferException) ex;
}
throw new TransferException(MSG_CAUGHT_UNEXPECTED_EXCEPTION, ex);
}
resp.setStatus(Status.STATUS_OK);
return Status.STATUS_OK;
}
use of org.springframework.extensions.webscripts.servlet.WebScriptServletRequest in project alfresco-remote-api by Alfresco.
the class PostSnapshotCommandProcessor 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) {
int result = Status.STATUS_OK;
// 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);
if (webScriptServletRequest == null) {
logger.debug("bad request, not assignable from");
resp.setStatus(Status.STATUS_BAD_REQUEST);
return Status.STATUS_BAD_REQUEST;
}
// We can't use the WebScriptRequest version of getParameter, since that may cause the content stream
// to be parsed. Get hold of the raw HttpServletRequest and work with that.
HttpServletRequest servletRequest = webScriptServletRequest.getHttpServletRequest();
// Read the transfer id from the request
String transferId = servletRequest.getParameter("transferId");
if ((transferId == null) || !ServletFileUpload.isMultipartContent(servletRequest)) {
logger.debug("bad request, not multipart");
resp.setStatus(Status.STATUS_BAD_REQUEST);
return Status.STATUS_BAD_REQUEST;
}
try {
logger.debug("about to upload manifest file");
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter = upload.getItemIterator(servletRequest);
while (iter.hasNext()) {
FileItemStream item = iter.next();
if (!item.isFormField() && TransferCommons.PART_NAME_MANIFEST.equals(item.getFieldName())) {
logger.debug("got manifest file");
receiver.saveSnapshot(transferId, item.openStream());
}
}
logger.debug("success");
resp.setStatus(Status.STATUS_OK);
OutputStream out = resp.getOutputStream();
resp.setContentType("text/xml");
resp.setContentEncoding("utf-8");
receiver.generateRequsite(transferId, out);
out.close();
} catch (Exception ex) {
logger.debug("exception caught", ex);
if (transferId != null) {
logger.debug("ending transfer", ex);
receiver.end(transferId);
}
if (ex instanceof TransferException) {
throw (TransferException) ex;
}
throw new TransferException(MSG_CAUGHT_UNEXPECTED_EXCEPTION, ex);
}
return result;
}
Aggregations