source: other-projects/the-macronizer/trunk/src/java/web/servlets/DirectInput.java@ 29855

Last change on this file since 29855 was 29855, checked in by davidb, 9 years ago

John's code after refactoring by Tom over the summer of 2014/2015

File size: 7.5 KB
Line 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package web.servlets;
6
7import java.io.BufferedReader;
8import java.io.BufferedWriter;
9import java.io.File;
10import java.io.FileInputStream;
11import java.io.FileOutputStream;
12import java.io.IOException;
13import java.io.InputStreamReader;
14import java.io.OutputStreamWriter;
15import java.io.Reader;
16import java.io.Writer;
17import javax.servlet.RequestDispatcher;
18import javax.servlet.ServletConfig;
19import javax.servlet.ServletException;
20import javax.servlet.http.HttpServlet;
21import javax.servlet.http.HttpServletRequest;
22import javax.servlet.http.HttpServletResponse;
23import monogram.plugin.PluginConfiguration;
24import monogram.plugin.PluginManager;
25
26/**
27 *
28 * @author OEM
29 */
30public 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 System.out.println("test 1");
100 restoredFile = pluginManager.run(configuration);
101 //Read the file into a String then delete
102 System.out.println("test done");
103 String restoredFragment = read(restoredFile, "utf-8");
104
105 request.setAttribute("fragment2", restoredFragment);
106 request.setAttribute("old", fragment);
107 request.setAttribute("options", options);
108 request.setAttribute("preserveMacrons", preserveMacrons);
109 RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher(path + "/main.jsp");
110 dispatcher.forward(request, response);
111 } catch (Exception e) {
112 request.setAttribute("errorMessage", UNEXPECTED_ERROR);
113 forward(path + "/error.jsp", request, response);
114 } finally {
115 file.delete();
116 restoredFile.delete();
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}
Note: See TracBrowser for help on using the repository browser.