11package com .payneteasy .libreofficewrapper .server .servlet ;
22
3+ import javax .servlet .http .HttpServlet ;
4+ import javax .servlet .http .HttpServletRequest ;
5+ import javax .servlet .http .HttpServletResponse ;
6+ import java .io .IOException ;
7+ import java .util .HashSet ;
8+ import java .util .Set ;
9+
310import com .artofsolving .jodconverter .DefaultDocumentFormatRegistry ;
411import com .artofsolving .jodconverter .DocumentConverter ;
512import com .artofsolving .jodconverter .DocumentFamily ;
916import com .artofsolving .jodconverter .openoffice .converter .StreamOpenOfficeDocumentConverter ;
1017import com .payneteasy .libreofficewrapper .server .config .ILibreofficeServiceConfiguration ;
1118import com .payneteasy .startup .parameters .StartupParametersFactory ;
12-
13- import java .io .IOException ;
14- import java .util .HashSet ;
15- import java .util .Set ;
16-
17- import javax .servlet .http .HttpServlet ;
18- import javax .servlet .http .HttpServletRequest ;
19- import javax .servlet .http .HttpServletResponse ;
20-
2119import org .slf4j .Logger ;
2220import org .slf4j .LoggerFactory ;
2321
22+ @ SuppressWarnings ("squid:S1989" )
2423public class LibreofficeConverterServlet extends HttpServlet {
2524
25+ private static final String TEXT_PLAIN_CONTENT_TYPE = "text/plain;charset=UTF-8" ;
26+
2627 private final Logger logger = LoggerFactory .getLogger (LibreofficeConverterServlet .class );
2728
2829 private final DefaultDocumentFormatRegistry formatRegistry = new DefaultDocumentFormatRegistry ();
2930
3031 private final Set <String > availableDocumentFormats = new HashSet <>();
3132 private final Set <String > availableOutputFormats = new HashSet <>();
3233
33- private final ILibreofficeServiceConfiguration libreofficeServiceConfiguration ;
34+ private final ILibreofficeServiceConfiguration libreofficeServiceConfiguration =
35+ StartupParametersFactory .getStartupParameters (ILibreofficeServiceConfiguration .class );
3436
3537 public LibreofficeConverterServlet () {
36- libreofficeServiceConfiguration = StartupParametersFactory .getStartupParameters (ILibreofficeServiceConfiguration .class );
37-
38- // docx and xlsx formats are not in DefaultFormatRegistry, add manually
39- final DocumentFormat docxFormat = new DocumentFormat ("DOCX" , DocumentFamily .TEXT , "application/vnd.openxmlformats-officedocument.wordprocessingml.document" , "docx" );
40- formatRegistry .addDocumentFormat (docxFormat );
41- final DocumentFormat xlsxFormat = new DocumentFormat ("XLSX" , DocumentFamily .SPREADSHEET , "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" , "xlsx" );
42- formatRegistry .addDocumentFormat (xlsxFormat );
38+ formatRegistry .addDocumentFormat (
39+ new DocumentFormat (
40+ "DOCX" ,
41+ DocumentFamily .TEXT ,
42+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ,
43+ "docx"
44+ )
45+ );
46+
47+ formatRegistry .addDocumentFormat (
48+ new DocumentFormat (
49+ "XLSX" ,
50+ DocumentFamily .SPREADSHEET ,
51+ "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ,
52+ "xlsx"
53+ )
54+ );
4355
4456 availableDocumentFormats .add ("csv" );
4557 availableDocumentFormats .add ("doc" );
@@ -49,45 +61,96 @@ public LibreofficeConverterServlet() {
4961
5062 availableOutputFormats .add ("pdf" );
5163
52- logger .info ("Libreoffice server configuration: host='{}', port={}" , libreofficeServiceConfiguration .getLibreofficeHost (), libreofficeServiceConfiguration .getLibreofficePort ());
53- logger .info ("Available document convert formats: {} -> {} " , availableDocumentFormats , availableOutputFormats );
64+ logger .info (
65+ "Libreoffice server configuration: host='{}', port={}\n " +
66+ "Available document convert formats: {} -> {}" ,
67+ libreofficeServiceConfiguration .getLibreofficeHost (),
68+ libreofficeServiceConfiguration .getLibreofficePort (),
69+ availableDocumentFormats ,
70+ availableOutputFormats
71+ );
5472 }
5573
5674 @ Override
57- protected void doPost (final HttpServletRequest req , final HttpServletResponse resp ) throws IOException {
58- logger .info ("Received POST request to {}" , req .getServletPath ());
59- final String inputFormat = req .getParameter ("inputFormat" );
60- final String outputFormat = req .getParameter ("outputFormat" );
75+ protected void doPost (
76+ HttpServletRequest request ,
77+ HttpServletResponse response
78+ ) throws IOException {
79+ logger .info ("Received POST request to {}" , request .getServletPath ());
80+
81+ final String inputFormat = request .getParameter ("inputFormat" );
82+ final String outputFormat = request .getParameter ("outputFormat" );
6183
6284 if (inputFormat == null || !availableDocumentFormats .contains (inputFormat )) {
63- resp .setStatus (HttpServletResponse .SC_BAD_REQUEST );
64- resp .setContentType ("text/plain;charset=UTF-8" );
65- resp .getWriter ().print ("Invalid input document format " + inputFormat + ", expected one of " + availableDocumentFormats .toString ());
85+ response .setStatus (HttpServletResponse .SC_BAD_REQUEST );
86+ response .setContentType (TEXT_PLAIN_CONTENT_TYPE );
87+ response
88+ .getWriter ()
89+ .printf (
90+ "Invalid input document format %s, expected one of %s" ,
91+ inputFormat ,
92+ availableDocumentFormats
93+ );
6694 return ;
6795 }
96+
6897 if (outputFormat == null || !availableOutputFormats .contains (outputFormat )) {
69- resp .setStatus (HttpServletResponse .SC_BAD_REQUEST );
70- resp .setContentType ("text/plain;charset=UTF-8" );
71- resp .getWriter ().print ("Invalid output document format " + outputFormat + ", expected one of " + availableDocumentFormats .toString ());
98+ response .setStatus (HttpServletResponse .SC_BAD_REQUEST );
99+ response .setContentType (TEXT_PLAIN_CONTENT_TYPE );
100+ response
101+ .getWriter ()
102+ .printf (
103+ "Invalid output document format %s, expected one of %s" ,
104+ outputFormat ,
105+ availableOutputFormats
106+ );
72107 return ;
73108 }
74109
75110 logger .info ("Converting request input from {} to {}" , inputFormat , outputFormat );
76- resp .setContentType (formatRegistry .getFormatByFileExtension (outputFormat ).getMimeType ());
111+
112+ response .setContentType (
113+ formatRegistry
114+ .getFormatByFileExtension (outputFormat )
115+ .getMimeType ()
116+ );
77117
78118 OpenOfficeConnection connection = null ;
79119 try {
80- connection = new SocketOpenOfficeConnection (libreofficeServiceConfiguration .getLibreofficeHost (), libreofficeServiceConfiguration .getLibreofficePort ());
120+ connection = new SocketOpenOfficeConnection (
121+ libreofficeServiceConfiguration .getLibreofficeHost (),
122+ libreofficeServiceConfiguration .getLibreofficePort ()
123+ );
124+
81125 connection .connect ();
82- final DocumentConverter converter = new StreamOpenOfficeDocumentConverter (connection , formatRegistry );
83- converter .convert (req .getInputStream (), formatRegistry .getFormatByFileExtension (inputFormat ), resp .getOutputStream (), formatRegistry .getFormatByFileExtension (outputFormat ));
126+
127+ final DocumentConverter converter = new StreamOpenOfficeDocumentConverter (
128+ connection ,
129+ formatRegistry
130+ );
131+
132+ converter .convert (
133+ request .getInputStream (),
134+ formatRegistry .getFormatByFileExtension (inputFormat ),
135+ response .getOutputStream (),
136+ formatRegistry .getFormatByFileExtension (outputFormat )
137+ );
138+
84139 logger .info ("Converted document from {} to {}" , inputFormat , outputFormat );
85- } catch (final Exception e ) {
140+ } catch (Exception e ) {
86141 logger .error ("Cannot convert file to {} format" , outputFormat , e );
87- resp .reset ();
88- resp .setStatus (HttpServletResponse .SC_INTERNAL_SERVER_ERROR );
89- resp .setContentType ("text/plain;charset=UTF-8" );
90- resp .getWriter ().print ("Unexpected error while converting document from " + inputFormat + " to " + outputFormat + ": " + e .getMessage ());
142+
143+ response .reset ();
144+ response .setStatus (HttpServletResponse .SC_INTERNAL_SERVER_ERROR );
145+ response .setContentType (TEXT_PLAIN_CONTENT_TYPE );
146+ response
147+ .getWriter ()
148+ .printf (
149+ "Unexpected error while converting document from %s to %s: %s" ,
150+ inputFormat ,
151+ outputFormat ,
152+ e .getMessage ()
153+ );
91154 } finally {
92155 if (connection != null && connection .isConnected ()) {
93156 connection .disconnect ();
0 commit comments