Search in sources :

Example 61 with MultipartRequestEntity

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;
}
Also used : HashMap(java.util.HashMap) PostMethod(org.apache.commons.httpclient.methods.PostMethod) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) ZohoToken(com.openkm.extension.dao.bean.ZohoToken) Document(com.openkm.bean.Document) OKMDocument(com.openkm.api.OKMDocument) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) OKMException(com.openkm.frontend.client.OKMException) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) HttpClient(org.apache.commons.httpclient.HttpClient)

Example 62 with MultipartRequestEntity

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();
    }
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) HttpClient(org.apache.commons.httpclient.HttpClient) HttpException(org.apache.commons.httpclient.HttpException)

Example 63 with MultipartRequestEntity

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());
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) StringPart(org.apache.commons.httpclient.methods.multipart.StringPart) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart)

Aggregations

MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)63 Part (org.apache.commons.httpclient.methods.multipart.Part)54 PostMethod (org.apache.commons.httpclient.methods.PostMethod)53 FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)52 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)44 IOException (java.io.IOException)24 HttpClient (org.apache.commons.httpclient.HttpClient)20 File (java.io.File)16 ArrayList (java.util.ArrayList)16 HttpMethodParams (org.apache.commons.httpclient.params.HttpMethodParams)9 Test (org.junit.Test)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 ByteArrayPartSource (org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource)7 GetMethod (org.apache.commons.httpclient.methods.GetMethod)6 Element (org.jdom.Element)6 HttpException (org.apache.commons.httpclient.HttpException)5 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)5 Map (java.util.Map)4 Header (org.apache.commons.httpclient.Header)4 NameValuePair (org.apache.commons.httpclient.NameValuePair)4