1 package pl.psnc.dl.ege.webapp.servlet;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.PrintWriter;
6 import java.util.Set;
7
8 import javax.servlet.ServletException;
9 import javax.servlet.http.HttpServlet;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletResponse;
12
13 import org.apache.commons.fileupload.FileItemIterator;
14 import org.apache.commons.fileupload.FileItemStream;
15 import org.apache.commons.fileupload.servlet.ServletFileUpload;
16 import org.apache.log4j.Logger;
17
18 import pl.psnc.dl.ege.EGE;
19 import pl.psnc.dl.ege.EGEImpl;
20 import pl.psnc.dl.ege.exception.ValidatorException;
21 import pl.psnc.dl.ege.types.DataType;
22 import pl.psnc.dl.ege.types.ValidationResult;
23 import pl.psnc.dl.ege.webapp.request.Method;
24 import pl.psnc.dl.ege.webapp.request.RequestResolver;
25 import pl.psnc.dl.ege.webapp.request.RequestResolvingException;
26 import pl.psnc.dl.ege.webapp.request.ValidationRequestResolver;
27
28
29
30
31 public class ValidationServlet
32 extends HttpServlet
33 {
34
35 private static final Logger LOGGER = Logger
36 .getLogger(ValidationServlet.class);
37
38 private static final long serialVersionUID = 1L;
39
40
41
42
43
44 public ValidationServlet()
45 {
46 super();
47 }
48
49
50
51
52
53 protected void doGet(HttpServletRequest request,
54 HttpServletResponse response)
55 throws ServletException, IOException
56 {
57 try {
58
59 RequestResolver rr = new ValidationRequestResolver(request,
60 Method.GET);
61
62 printAvailableValidations(response, rr);
63 }
64 catch (RequestResolvingException ex) {
65 if (ex.getStatus().equals(
66 RequestResolvingException.Status.WRONG_METHOD)) {
67
68 response.sendError(405, ConversionServlet.R_WRONG_METHOD);
69 }
70 else {
71 throw new ServletException(ex);
72 }
73 }
74 }
75
76
77
78
79
80 private void printAvailableValidations(HttpServletResponse response,
81 RequestResolver rr)
82 throws ServletException
83 {
84 EGE ege = new EGEImpl();
85 try {
86 PrintWriter out = response.getWriter();
87 Set<DataType> dts = ege.returnSupportedValidationFormats();
88 if(dts.size() == 0){
89 response.setStatus(HttpServletResponse.SC_NO_CONTENT);
90 return;
91 }
92 response.setContentType("text/xml");
93 out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
94 out
95 .println("<validations xmlns:xlink=\"http://www.w3.org/1999/xlink\">");
96 String prefix = rr.getRequest().getRequestURL().toString()
97 + (rr.getRequest().getRequestURL().toString().endsWith(
98 RequestResolver.SLASH) ? "" : "/");
99 for (DataType dt : dts) {
100 out.println("<input-data-type id=\"" + dt.toString()
101 + "\" xlink:href=\"" + prefix + rr.encodeDataType(dt)
102 + "/\" />");
103 }
104 out.println("</validations>");
105 }
106 catch (IOException ex) {
107 throw new ServletException(ex);
108 }
109 }
110
111
112
113
114
115 protected void doPost(HttpServletRequest request,
116 HttpServletResponse response)
117 throws ServletException, IOException
118 {
119 try {
120 RequestResolver rr = new ValidationRequestResolver(request,
121 Method.POST);
122 DataType dt = (DataType) rr.getData();
123 performValidation(dt, rr, response);
124 }
125 catch (RequestResolvingException ex) {
126
127 if (ex.getStatus().equals(
128 RequestResolvingException.Status.BAD_REQUEST)) {
129 response.sendError(HttpServletResponse.SC_BAD_REQUEST);
130 }
131 else {
132 throw new ServletException(ex);
133 }
134 }
135 catch (ValidatorException ex){
136 response.sendError(HttpServletResponse.SC_BAD_REQUEST);
137 }
138 catch (Exception ex) {
139 throw new ServletException(ex);
140 }
141 }
142
143
144
145
146 private void performValidation(DataType dt, RequestResolver rr,
147 HttpServletResponse response)
148 throws Exception
149 {
150 EGE ege = new EGEImpl();
151 InputStream is = null;
152 if (ServletFileUpload.isMultipartContent(rr.getRequest())) {
153 try {
154 ServletFileUpload upload = new ServletFileUpload();
155 FileItemIterator iter = upload.getItemIterator(rr.getRequest());
156 while (iter.hasNext()) {
157 FileItemStream item = iter.next();
158 if (!item.isFormField()) {
159 is = item.openStream();
160
161 ValidationResult result = ege.performValidation(is, dt);
162 printValidationResult(response,result);
163 is.close();
164 }
165 }
166 }
167 catch(ValidatorException ex){
168 throw ex;
169 }
170 catch (Exception ex) {
171 LOGGER.error(ex.getMessage(), ex);
172 throw ex;
173 }
174 finally {
175 if (is != null) {
176 try {
177 is.close();
178 }
179 catch (IOException ex) {
180
181 }
182 }
183 }
184 }
185 }
186
187 public void printValidationResult(HttpServletResponse response, ValidationResult result) throws IOException{
188 PrintWriter out = response.getWriter();
189 response.setContentType("text/xml");
190 out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
191 out.println("<validation-result>");
192 out.println("<status>" + result.getStatus()
193 + "</status>");
194 out.println("<messages>");
195 int i = 1;
196 for (String msg : result.getMessages()) {
197 out.println("<message><![CDATA[" + msg + "]]></message>");
198 i++;
199 }
200 out.println("</messages>");
201 out.println("</validation-result>");
202 }
203
204
205 }