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

Last change on this file since 31962 was 31962, checked in by rmw36, 7 years ago

Macronizer changes: 1. adding an ethical disclaimer to the main web page (temporarily still in English for the Maori jsp file too, but Te Taka will be translating this.) 2. Adding more logging to the macronizer. This required setting up a log4j.properties file (and adding its jar file copied from GS3), and modifying the existing java code DirectInput and FileUpload to make calls to logger. Modifications to build.xml to copy the new template log4j.properties.in file into web/WEB-INF/classes where the log4j.props needs to live. 3. Corrected the svn mime-type property on build.xml so that it's no longer mistaken for a binary file

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