06-01-2011 04:26 PM
06-15-2011 06:14 AM
06-27-2011 07:56 AM
06-27-2011 09:04 AM
04-22-2013 05:46 AM
07-22-2011 05:44 AM
07-25-2011 04:08 PM
public String upload(File file, String siteId, String containerId, String uploadDirectory) {
String json = null;
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpHost targetHost = new HttpHost("localhost", 8080, "http");
try {
HttpPost httppost = new HttpPost("/alfresco/service/api/upload?alf_ticket="
+ this.ticket);
FileBody bin = new FileBody(file);
StringBody siteid = new StringBody(siteId);
StringBody containerid = new StringBody(containerId);
StringBody uploaddirectory = new StringBody(uploadDirectory);
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("filedata", bin);
reqEntity.addPart("siteid", siteid);
reqEntity.addPart("containerid", containerid);
reqEntity.addPart("uploaddirectory", uploaddirectory);
httppost.setEntity(reqEntity);
log.debug("executing request:" + httppost.getRequestLine());
HttpResponse response = httpclient.execute(targetHost, httppost);
HttpEntity resEntity = response.getEntity();
log.debug("response status:" + response.getStatusLine());
if (resEntity != null) {
log.debug("response content length:"
+ resEntity.getContentLength());
json = EntityUtils.toString(resEntity);
log.debug("response content:" + json);
}
EntityUtils.consume(resEntity);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
httpclient.getConnectionManager().shutdown();
}
return json;
}
07-26-2011 04:59 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;
}
}
06-27-2012 07:59 AM
curl -v -X POST -F filedata=@myUpload.doc -F siteid=mysite -F containerid=documentLibrary -F uploaddirectory=/uploads http://username:password@localhost:8080/alfresco/service/api/upload
This will upload into documentLibrary/uploads within the specific Share site (mysite). Note that the user performing the upload must be a member of that Share site (and presumably needs at least Contributor access?).07-29-2012 09:50 PM
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.