1 | /*
|
---|
2 | * To change this template, choose Tools | Templates
|
---|
3 | * and open the template in the editor.
|
---|
4 | */
|
---|
5 | package web.servlets;
|
---|
6 |
|
---|
7 | import java.io.BufferedReader;
|
---|
8 | import java.io.BufferedWriter;
|
---|
9 | import java.io.File;
|
---|
10 | import java.io.FileInputStream;
|
---|
11 | import java.io.FileOutputStream;
|
---|
12 | import java.io.IOException;
|
---|
13 | import java.io.InputStreamReader;
|
---|
14 | import java.io.OutputStreamWriter;
|
---|
15 | import java.io.Reader;
|
---|
16 | import java.io.Writer;
|
---|
17 | import javax.servlet.RequestDispatcher;
|
---|
18 | import javax.servlet.ServletConfig;
|
---|
19 | import javax.servlet.ServletException;
|
---|
20 | import javax.servlet.http.HttpServlet;
|
---|
21 | import javax.servlet.http.HttpServletRequest;
|
---|
22 | import javax.servlet.http.HttpServletResponse;
|
---|
23 | import monogram.plugin.PluginConfiguration;
|
---|
24 | import monogram.plugin.PluginManager;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | *
|
---|
28 | * @author OEM
|
---|
29 | */
|
---|
30 | public class DirectInput extends HttpServlet {
|
---|
31 |
|
---|
32 | private final String UNEXPECTED_ERROR = "An unexpected error has occurred. Please try again or contact the web administrator if the problem persists.";
|
---|
33 | private final String UTF8_ENCODING = "utf-8";
|
---|
34 | public static boolean DI; // This will be used in a test in the mongramRestorer to add html to highligth the changes on the webpage output.
|
---|
35 | private File tmpdir;
|
---|
36 |
|
---|
37 | @Override
|
---|
38 | public void init(ServletConfig config) throws ServletException {
|
---|
39 | super.init(config);
|
---|
40 | tmpdir = new File((String) config.getServletContext().getAttribute("tmpdir"));
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
|
---|
45 | * @param request servlet request
|
---|
46 | * @param response servlet response
|
---|
47 | * @throws ServletException if a servlet-specific error occurs
|
---|
48 | * @throws IOException if an I/O error occurs
|
---|
49 | */
|
---|
50 | protected void processRequest(HttpServletRequest request, HttpServletResponse response)
|
---|
51 | throws ServletException, IOException {
|
---|
52 | }
|
---|
53 |
|
---|
54 | // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
|
---|
55 | /**
|
---|
56 | * Handles the HTTP <code>GET</code> method.
|
---|
57 | * @param request servlet request
|
---|
58 | * @param response servlet response
|
---|
59 | * @throws ServletException if a servlet-specific error occurs
|
---|
60 | * @throws IOException if an I/O error occurs
|
---|
61 | */
|
---|
62 | @Override
|
---|
63 | protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
---|
64 | throws ServletException, IOException {
|
---|
65 | processRequest(request, response);
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Handles the HTTP <code>POST</code> method.
|
---|
70 | * @param request servlet request
|
---|
71 | * @param response servlet response
|
---|
72 | * @throws ServletException if a servlet-specific error occurs
|
---|
73 | * @throws IOException if an I/O error occurs
|
---|
74 | */
|
---|
75 | @Override
|
---|
76 | protected void doPost(HttpServletRequest request, HttpServletResponse response)
|
---|
77 | throws ServletException, IOException {
|
---|
78 | request.setCharacterEncoding("utf-8");
|
---|
79 | DI = true;
|
---|
80 | final String fragment = request.getParameter("fragment");
|
---|
81 | final String lang = request.getParameter("lang");
|
---|
82 | final String preserveMacrons = request.getParameter("preserveExistingMacrons") != null ? request.getParameter("preserveExistingMacrons") : "false";
|
---|
83 | final String options = request.getParameter("options");
|
---|
84 | final String path = "/jsp" + (lang.equals("en") ? "/en" : "/mi");
|
---|
85 | //Write the input to a temporary file.
|
---|
86 | File file = File.createTempFile("mi-tmp-", ".txt", tmpdir);
|
---|
87 | write(file, "utf-8", fragment);
|
---|
88 |
|
---|
89 | //Create a fileview
|
---|
90 | PluginConfiguration configuration = new PluginConfiguration();
|
---|
91 | configuration.setFile(file);
|
---|
92 | configuration.setCharsetEncoding("utf-8");
|
---|
93 | configuration.setFileType(".txt");
|
---|
94 | configuration.setPreserveExistingMacrons(preserveMacrons.equals("true"));
|
---|
95 | //Restore the file.
|
---|
96 | PluginManager pluginManager = new PluginManager(tmpdir);
|
---|
97 | File restoredFile = null;
|
---|
98 | try {
|
---|
99 | restoredFile = pluginManager.run(configuration);
|
---|
100 | //Read the file into a String then delete
|
---|
101 | String restoredFragment = read(restoredFile, "utf-8");
|
---|
102 |
|
---|
103 | request.setAttribute("fragment2", restoredFragment);
|
---|
104 | request.setAttribute("old", fragment);
|
---|
105 | request.setAttribute("options", options);
|
---|
106 | request.setAttribute("preserveMacrons", preserveMacrons);
|
---|
107 | RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher(path + "/main.jsp");
|
---|
108 | dispatcher.forward(request, response);
|
---|
109 | } catch (Exception e) {
|
---|
110 | request.setAttribute("errorMessage", UNEXPECTED_ERROR);
|
---|
111 | forward(path + "/error.jsp", request, response);
|
---|
112 | } finally {
|
---|
113 | file.delete();
|
---|
114 | if (restoredFile != null) {
|
---|
115 | restoredFile.delete();
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Forwards a request from a servlet to another resource on the server.
|
---|
122 | * @param path Path to forward to.
|
---|
123 | * @param request
|
---|
124 | * @param response
|
---|
125 | * @throws ServletException
|
---|
126 | * @throws IOException
|
---|
127 | */
|
---|
128 | private void forward(String path, HttpServletRequest request, HttpServletResponse response)
|
---|
129 | throws ServletException, IOException {
|
---|
130 | final RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(path);
|
---|
131 | dispatcher.forward(request, response);
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Writes the fragment to the file.
|
---|
136 | * @param file The file to write to.
|
---|
137 | * @param fragment The String to write to the file.
|
---|
138 | */
|
---|
139 | private void write(File file, String charsetEncoding, String fragment) {
|
---|
140 | BufferedWriter out = null;
|
---|
141 | try {
|
---|
142 | out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charsetEncoding));
|
---|
143 | out.write(fragment);
|
---|
144 | } catch (IOException e) {
|
---|
145 | e.printStackTrace();
|
---|
146 | } finally {
|
---|
147 | close(out);
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Reads the contents of the file.
|
---|
153 | * @param file The file to read.
|
---|
154 | * @param charsetEncoding The character set encoding of the file.
|
---|
155 | * @return The contents of the file.
|
---|
156 | */
|
---|
157 | private String read(File file, String charsetEncoding) {
|
---|
158 | final StringBuilder buffer = new StringBuilder();
|
---|
159 | BufferedReader reader = null;
|
---|
160 | try {
|
---|
161 | reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetEncoding));
|
---|
162 | final char[] chars = new char[1024];
|
---|
163 | int numRead = 0;
|
---|
164 | while ((numRead = reader.read(chars)) > -1) {
|
---|
165 | buffer.append(String.valueOf(chars, 0, numRead));
|
---|
166 | }
|
---|
167 | } catch (IOException e) {
|
---|
168 | e.printStackTrace();
|
---|
169 | } finally {
|
---|
170 | close(reader);
|
---|
171 | }
|
---|
172 | return buffer.toString();
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Close the writer.
|
---|
177 | * @param writer The Writer to close.
|
---|
178 | */
|
---|
179 | private void close(Writer writer) {
|
---|
180 | if (writer != null) {
|
---|
181 | try {
|
---|
182 | writer.close();
|
---|
183 | } catch (IOException e) {
|
---|
184 | e.printStackTrace();
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Close the reader.
|
---|
191 | * @param reader The Reader to close.
|
---|
192 | */
|
---|
193 | private void close(Reader reader) {
|
---|
194 | if (reader != null) {
|
---|
195 | try {
|
---|
196 | reader.close();
|
---|
197 | } catch (IOException e) {
|
---|
198 | e.printStackTrace();
|
---|
199 | }
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * Returns a short description of the servlet.
|
---|
205 | * @return a String containing servlet description
|
---|
206 | */
|
---|
207 | @Override
|
---|
208 | public String getServletInfo() {
|
---|
209 | return "Short description";
|
---|
210 | }// </editor-fold>
|
---|
211 | }
|
---|