use of org.springframework.extensions.webscripts.json.JSONWriter 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.json.JSONWriter in project alfresco-remote-api by Alfresco.
the class BeginTransferCommandProcessor 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 transferId = null;
try {
String[] fromRepositoryIdValues = req.getParameterValues(TransferCommons.PARAM_FROM_REPOSITORYID);
String[] transferToSelfValues = req.getParameterValues(TransferCommons.PARAM_ALLOW_TRANSFER_TO_SELF);
String[] editionValues = req.getParameterValues(TransferCommons.PARAM_VERSION_EDITION);
String[] majorValues = req.getParameterValues(TransferCommons.PARAM_VERSION_MAJOR);
String[] minorValues = req.getParameterValues(TransferCommons.PARAM_VERSION_MINOR);
String[] revisionValues = req.getParameterValues(TransferCommons.PARAM_VERSION_REVISION);
String[] rootFileTransfer = req.getParameterValues(TransferCommons.PARAM_ROOT_FILE_TRANSFER);
String fromRepositoryId = null;
if (fromRepositoryIdValues != null && fromRepositoryIdValues.length > 0) {
fromRepositoryId = fromRepositoryIdValues[0];
}
boolean transferToSelf = false;
if (transferToSelfValues != null && transferToSelfValues.length > 0) {
if (transferToSelfValues[0].equalsIgnoreCase("true")) {
transferToSelf = true;
}
}
String edition = "Unknown";
if (editionValues != null && editionValues.length > 0) {
edition = editionValues[0];
}
String major = "0";
if (majorValues != null && majorValues.length > 0) {
major = majorValues[0];
}
String minor = "0";
if (minorValues != null && minorValues.length > 0) {
minor = minorValues[0];
}
String revision = "0";
if (revisionValues != null && revisionValues.length > 0) {
revision = revisionValues[0];
}
TransferVersion fromVersion = new TransferVersionImpl(major, minor, revision, edition);
// It replaces the root node if transfers on file system
if (rootFileTransfer != null && rootFileTransfer.length > 0) {
receiver.setTransferRootNode(rootFileTransfer[0]);
}
// attempt to start the transfer
transferId = receiver.start(fromRepositoryId, transferToSelf, fromVersion);
// Create a temporary folder into which we can place transferred files
receiver.getStagingFolder(transferId);
TransferVersion version = receiver.getVersion();
// return the unique transfer id (the lock id)
StringWriter stringWriter = new StringWriter(1000);
JSONWriter jsonWriter = new JSONWriter(stringWriter);
jsonWriter.startObject();
jsonWriter.writeValue(TransferCommons.PARAM_TRANSFER_ID, transferId);
if (version != null) {
jsonWriter.writeValue(TransferCommons.PARAM_VERSION_EDITION, version.getEdition());
jsonWriter.writeValue(TransferCommons.PARAM_VERSION_MAJOR, version.getVersionMajor());
jsonWriter.writeValue(TransferCommons.PARAM_VERSION_MINOR, version.getVersionMinor());
jsonWriter.writeValue(TransferCommons.PARAM_VERSION_REVISION, version.getVersionRevision());
}
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);
logger.debug("transfer started" + transferId);
return 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);
}
}
use of org.springframework.extensions.webscripts.json.JSONWriter 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.json.JSONWriter in project alfresco-remote-api by Alfresco.
the class MessagesTransferCommandProcessor 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;
try {
// return the unique transfer id (the lock id)
StringWriter stringWriter = new StringWriter(300);
JSONWriter jsonWriter = new JSONWriter(stringWriter);
jsonWriter.startValue("data");
jsonWriter.startArray();
jsonWriter.startObject();
// TODO - clearly a dummy message for now.
jsonWriter.writeValue("message", "hello world");
jsonWriter.endObject();
jsonWriter.startObject();
// TODO - clearly a dummy message for now.
jsonWriter.writeValue("message", "message2");
jsonWriter.endObject();
jsonWriter.endArray();
jsonWriter.endValue();
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) {
receiver.end(transferRecordId);
if (ex instanceof TransferException) {
throw (TransferException) ex;
}
throw new TransferException(MSG_CAUGHT_UNEXPECTED_EXCEPTION, ex);
}
}
use of org.springframework.extensions.webscripts.json.JSONWriter in project alfresco-remote-api by Alfresco.
the class AbstractCalendarWebScript method addActivityEntry.
/**
* Generates an activity entry for the entry
*/
protected String addActivityEntry(String event, CalendarEntry entry, SiteInfo site, WebScriptRequest req, JSONObject json) {
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
String dateOpt = "?date=" + fmt.format(entry.getStart());
// What page is this for?
String page = req.getParameter("page");
if (page == null && json != null) {
if (json.containsKey("page")) {
page = (String) json.get("page");
}
}
if (page == null) {
// Default
page = "calendar";
}
try {
StringWriter activityJson = new StringWriter();
JSONWriter activity = new JSONWriter(activityJson);
activity.startObject();
activity.writeValue("title", entry.getTitle());
activity.writeValue("page", page + dateOpt);
activity.endObject();
activityService.postActivity("org.alfresco.calendar.event-" + event, site.getShortName(), CALENDAR_SERVICE_ACTIVITY_APP_NAME, activityJson.toString());
} catch (Exception e) {
// Warn, but carry on
logger.warn("Error adding event " + event + " to activities feed", e);
}
// Return the date we used
return dateOpt;
}
Aggregations