use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity in project document-management-system by openkm.
the class ZohoServlet method sendToZoho.
/**
* sendToZoho
*/
private Map<String, String> sendToZoho(String zohoUrl, String nodeUuid, String lang) throws PathNotFoundException, AccessDeniedException, RepositoryException, DatabaseException, IOException, OKMException, LockException {
Map<String, String> result = new HashMap<String, String>();
File tmp = null;
try {
String path = OKMRepository.getInstance().getNodePath(null, nodeUuid);
String fileName = PathUtils.getName(path);
tmp = File.createTempFile("okm", ".tmp");
InputStream is = OKMDocument.getInstance().getContent(null, path, false);
Document doc = OKMDocument.getInstance().getProperties(null, path);
FileOutputStream fos = new FileOutputStream(tmp);
IOUtils.copy(is, fos);
fos.flush();
fos.close();
String id = UUID.randomUUID().toString();
String saveurl = Config.APPLICATION_BASE + "/extension/ZohoFileUpload";
Part[] parts = { new FilePart("content", tmp), new StringPart("apikey", Config.ZOHO_API_KEY), new StringPart("output", "url"), new StringPart("mode", "normaledit"), new StringPart("filename", fileName), new StringPart("skey", Config.ZOHO_SECRET_KEY), new StringPart("lang", lang), new StringPart("id", id), new StringPart("format", getFormat(doc.getMimeType())), new StringPart("saveurl", saveurl) };
PostMethod filePost = new PostMethod(zohoUrl);
filePost.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
int status = client.executeMethod(filePost);
if (status == HttpStatus.SC_OK) {
log.debug("OK: " + filePost.getResponseBodyAsString());
ZohoToken zot = new ZohoToken();
zot.setId(id);
zot.setUser(getThreadLocalRequest().getRemoteUser());
zot.setNode(nodeUuid);
zot.setCreation(Calendar.getInstance());
ZohoTokenDAO.create(zot);
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(filePost.getResponseBodyAsStream()));
String line;
while ((line = rd.readLine()) != null) {
if (line.startsWith("URL=")) {
result.put("url", line.substring(4));
result.put("id", id);
break;
}
}
rd.close();
} else {
String error = HttpStatus.getStatusText(status) + "\n\n" + filePost.getResponseBodyAsString();
log.error("ERROR: {}", error);
throw new OKMException(ErrorCode.get(ErrorCode.ORIGIN_OKMZohoService, ErrorCode.CAUSE_Zoho), error);
}
} finally {
FileUtils.deleteQuietly(tmp);
}
return result;
}
use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity in project document-management-system by openkm.
the class DocConverter method remoteConvert.
/**
* Handle remote OpenOffice server conversion
*/
public void remoteConvert(String uri, File inputFile, String srcMimeType, File outputFile, String dstMimeType) throws ConversionException {
PostMethod post = new PostMethod(uri);
try {
Part[] parts = { new FilePart(inputFile.getName(), inputFile), new StringPart("src_mime", srcMimeType), new StringPart("dst_mime", dstMimeType), new StringPart("okm_uuid", Repository.getUuid()) };
post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
HttpClient httpclient = new HttpClient();
int rc = httpclient.executeMethod(post);
log.info("Response Code: {}", rc);
if (rc == HttpStatus.SC_OK) {
FileOutputStream fos = new FileOutputStream(outputFile);
BufferedInputStream bis = new BufferedInputStream(post.getResponseBodyAsStream());
IOUtils.copy(bis, fos);
bis.close();
fos.close();
} else {
throw new IOException("Error in conversion: " + rc);
}
} catch (HttpException e) {
throw new ConversionException("HTTP exception", e);
} catch (FileNotFoundException e) {
throw new ConversionException("File not found exeption", e);
} catch (IOException e) {
throw new ConversionException("IO exception", e);
} finally {
post.releaseConnection();
}
}
use of org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity in project lobcder by skoulouzis.
the class Utils method postFile.
public void postFile(File file, String url) throws IOException {
System.err.println("post:" + file.getName() + " to: " + url);
PostMethod method = new PostMethod(url);
Part[] parts = { new StringPart("param_name", "value"), new FilePart(file.getName(), file) };
MultipartRequestEntity requestEntity = new MultipartRequestEntity(parts, method.getParams());
method.setRequestEntity(requestEntity);
int status = client.executeMethod(method);
assertTrue(status == HttpStatus.SC_CREATED || status == HttpStatus.SC_OK);
// HttpPost httppost = new HttpPost(uri.toASCIIString());
// MultipartEntity mpEntity = new MultipartEntity();
// ContentBody cbFile = new FileBody(file, "image/jpeg");
// mpEntity.addPart("userfile", cbFile);
//
//
// httppost.setEntity(mpEntity);
// System.out.println("executing request " + httppost.getRequestLine());
// CloseableHttpResponse response = httpclient.execute(httppost);
//
//
// System.out.println(response.getStatusLine());
}
Aggregations