12-14-2012 12:56 AM
public class UploadWebScript extends DeclarativeWebScript {
private static Log log = LogFactory.getLog(UploadWebScript.class);
private NodeService nodeService;
private ContentService contentService;
private TransactionService transactionService;
private DataDictionaryHelper dataDictionaryHelper;
private MimetypeService mimetypeService;
public void setNodeService(NodeService nodeService) {
this.nodeService = nodeService;
}
public void setContentService(ContentService contentService) {
this.contentService = contentService;
}
public void setTransactionService(TransactionService transactionService) {
this.transactionService = transactionService;
}
public void setDataDictionaryHelper(DataDictionaryHelper dataDictionaryHelper) {
this.dataDictionaryHelper = dataDictionaryHelper;
}
public void setMimetypeService(MimetypeService mimetypeService) {
this.mimetypeService = mimetypeService;
}
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status) {
I18NUtil.setLocale(Locale.ENGLISH);
Map<String, Object> model = new HashMap<String, Object>();
UserTransaction tx = transactionService.getUserTransaction(false);
try {
tx.begin();
NodeRef newNode = createNewNode(req);
tx.commit();
model.put("newNode", newNode);
} catch (RollbackException e) {
log.warn(e);
status.setCode(500);
status.setMessage(e.getLocalizedMessage());
status.setException(e);
status.setRedirect(true);
} catch (Exception e) {
log.warn(e);
status.setCode(500);
status.setMessage(e.getLocalizedMessage());
status.setException(e);
status.setRedirect(true);
try {
tx.rollback();
} catch (Exception e1) {
log.error("Transaction rollback failed");
log.error(e1);
}
}
return model;
}
private NodeRef createNewNode(WebScriptRequest req) {
if (log.isDebugEnabled()) {
log.debug("Creating new node of type '" + req.getParameter("type") + "' under node " + req.getParameter("uuid"));
}
Map<String, String> params = getRequestParametersAsMap(req);
Map<QName, Serializable> properties = dataDictionaryHelper.convertParametersToProperties(params);
NodeRef parent = new NodeRef(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE, req.getParameter("uuid"));
QName contentTypeQname = QName.createQName(YOUR_CUSTOM_NAMESPACE, req.getParameter("type"));
FormData fd = (FormData) req.parseContent();
FormField[] fields = fd.getFields();
for (FormField field : fields) {
if (field.getIsFile()) {
String name = field.getFilename();
properties.put(ContentModel.PROP_NAME, name);
ChildAssociationRef children = nodeService.createNode(parent, ContentModel.ASSOC_CONTAINS,
DataDictionaryHelper.getAssocNameFromFileName(name), contentTypeQname, properties);
NodeRef newNode = children.getChildRef();
ContentWriter writer = contentService.getWriter(newNode, ContentModel.PROP_CONTENT, true);
writer.setMimetype(mimetypeService.guessMimetype(name));
writer.putContent(field.getInputStream());
return newNode;
}
}
log.warn("Request did not have a file");
return null;
}
private Map<String, String> getRequestParametersAsMap(WebScriptRequest req) {
Map<String, String> params = new HashMap<String, String>();
for (String name : req.getParameterNames()) {
params.put(name, req.getParameter(name));
}
return params;
}
}
12-14-2012 03:36 AM
public static QName getAssocNameFromFileName(String name) {
// TODO: convert name to ISOwhatever encoding
return QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, name);
}
Regards,12-14-2012 04:04 AM
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.