Custom HttpClient for RemoteConverter of Documents4j

1k Views Asked by At

I am using a RemoteConverter from a jBoss web application to a standalone server built as the default server-standalone included into documents4j project.

Inside jboss I've got an old version of required libraries httpclient-4.0.1.jar and related httpcore-4.0.1.jar so I'm facing with a lot of ClassDefNotFoundException caused by the different version of the jar loaded by JVM.

There is a specific problem with HttpClientConnectionManager object that is not available yet in version

To avoid this problem I'd like to bluild a custom http client for the standalone-server, because, due to the previous problems, it's not possible for me to use Jersey.

Has someone build a different client for that standalone-server? What are the specs to build a custom RemoteClient?

UPDATE 1

After a little bit of analysis with the help of a sniffing tool, I figured out the composition of the message, so I've just ended a custom HttpClient for that server as following:

    File wordFile = new File("C:/temp/test.docx");
    InputStream targetStream = new FileInputStream(wordFile);

    URL url = new URL("http://localhost:9998");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/vnd.com.documents4j.any-msword");
    conn.setRequestProperty("Accept", "application/pdf");
    conn.setRequestProperty("Converter-Job-Priority", "1000");


    OutputStream os = conn.getOutputStream();
    os.write(IOUtils.toByteArray(targetStream));
    os.flush();

    if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
        throw new RuntimeException("Failed : HTTP error code : "
            + conn.getResponseCode());
    }

    BufferedReader br = new BufferedReader(new InputStreamReader(
            (conn.getInputStream())));
    FileWriter ostream = new FileWriter("C:/temp/test.pdf");
    BufferedWriter out = new BufferedWriter(ostream);
    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
        out.write(output+"\n");
    }
    br.close();
    out.close();
    os.close();
    conn.disconnect();

Now I've got another problem, if I try to open just created test.pdf file it is all white but with the right number of pages. If I open the file with a text editor and analyze the beginning and the end of file I found the following chars:

%PDF-1.5
%µµµµ
1 0 obj  
[...]
startxref
1484122
%%EOF

It seems to be a good PDF file.

There is something else to do with that file received from REST Server?

1

There are 1 best solutions below

0
On BEST ANSWER

Before answering your question: I would recommend you to rather shade the dependency then to reimplement it.

I can imagine that you are facing an encoding issue when implementing your own service. A BufferedReader translates the incoming data to character data. Do not read data by character line but treat it as binary. Instead of Writer classes use the Stream classes. This is why the meta data is treated correctly as it is represented by characters but the actual data is illegal as it is binary information:

InputStream in = conn.getInputStream();
OutputStream out = new FileOutputStream("C:/temp/test.pdf");
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) != -1) {
    out.write(buffer, 0, len);
}