4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
GetAnnotServlet.java
Go to the documentation of this file.
1 /*
2  * Project: Server for annotations sharing
3  * Author: Ing. Jaroslav Dytrych idytrych@fit.vutbr.cz
4  * File: GetAnnotServlet.java
5  * Description: Servlet which makes available annotations
6  */
7 
8 /**
9  * @file GetAnnotServlet.java
10  *
11  * @brief Servlet which makes available annotations
12  */
13 
14 package cz.vutbr.fit.knot.annotations.comet;
15 
23 import java.io.IOException;
24 import java.io.PrintWriter;
25 import java.net.URISyntaxException;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.logging.Level;
30 import java.util.logging.Logger;
31 import javax.servlet.ServletException;
32 import javax.servlet.http.HttpServlet;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35 
36 /**
37  * Servlet which makes available annotations
38  * It accepts following parameters:
39  * out - type of output ("v1", "v2" or "sug")
40  * uri - URI of requested annotation
41  * doc - id of annotated document for which annotations should be retrieved
42  *
43  * @brief Servlet which makes available annotations
44  */
45 public class GetAnnotServlet extends HttpServlet {
46 
47  private static final int OUTPUT_TYPE_V1 = 0;
48  private static final int OUTPUT_TYPE_V2 = 1;
49  private static final int OUTPUT_TYPE_SUG = 2;
50 
51  /**
52  * Processes requests for both HTTP
53  * <code>GET</code> and
54  * <code>POST</code> methods.
55  *
56  * @param request servlet request
57  * @param response servlet response
58  * @throws ServletException if a servlet-specific error occurs
59  * @throws IOException if an I/O error occurs
60  */
61  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
62  throws ServletException, IOException {
63  response.setContentType("text/html;charset=UTF-8");
64  PrintWriter out = response.getWriter();
65  try {
66  String uriStr = request.getParameter("uri");
67  String idStr = null;
68  int outputType = OUTPUT_TYPE_V2;
69  if(uriStr != null && uriStr.startsWith(AppBean.getBaseUri() + "/serv/")){
70  idStr = uriStr.replace(AppBean.getBaseUri() + "/serv/", "");
71  } else if (uriStr != null && uriStr.startsWith(AppBean.getBaseAnnotUri())) {
72  idStr = uriStr.replace(AppBean.getBaseAnnotUri(), "");
73  outputType = OUTPUT_TYPE_V1;
74  }
75 
76  String reqType = request.getParameter("out");
77  if (reqType != null && reqType.equals("v1")) {
78  outputType = OUTPUT_TYPE_V1;
79  } else if (reqType != null && reqType.equals("v2")) {
80  outputType = OUTPUT_TYPE_V2;
81  } else if (reqType != null && reqType.equals("sug")) {
82  outputType = OUTPUT_TYPE_SUG;
83  }
84 
85  String reqUserStr = request.getParameter("user");
86  Integer reqUserID = null;
87  if (reqUserStr != null && !reqUserStr.isEmpty()) {
88  try {
89  reqUserID = Integer.parseInt(reqUserStr); // parse id
90  } catch (NumberFormatException numberFormatException) {
91  // It is not vald id - is it login?
92  Object[] params = new Object[2];
93  params[0] = "login";
94  params[1] = reqUserStr;
95  List uList = AppBean.getPersistenceManager().queryDB("User.findByLogin", params);
96  if (uList != null && !uList.isEmpty()) { // if user was found
97  User user = (User) uList.get(0);
98  reqUserID = user.getId();
99  } else {
100  reqUserID = null;
101  }
102  }
103  }
104 
105  Integer id = null;
106  if (idStr != null) {
107  try {
108  id = Integer.parseInt(idStr);
109  } catch (NumberFormatException nfe) {
111  String msg = "NumberFormatException";
112  Logger.getLogger(GetDocServlet.class.getName()).log(Level.ALL, msg);
113  }
114  out.print(printError());
115  return;
116  }
117  }
118 
119  String docStr = request.getParameter("doc");
120  Integer docId = null;
121  if (docStr != null && !docStr.isEmpty()) {
122  try {
123  docId = Integer.parseInt(docStr);
124  } catch (NumberFormatException nfe) {
126  String msg = "NumberFormatException";
127  Logger.getLogger(GetDocServlet.class.getName()).log(Level.ALL, msg);
128  }
129  out.print(printError());
130  return;
131  }
132  }
133 
134  String withCreatedStr = request.getParameter("withCreated");
135  boolean withCreated = false;
136  if (withCreatedStr != null && !withCreatedStr.isEmpty()) {
137  if (withCreatedStr.equalsIgnoreCase("true")) {
138  withCreated = true;
139  }
140  }
141 
142  AnnotDocument doc = null;
143  if (docId != null) {
144  Object[] params = new Object[2];
145  params[0] = "id";
146  params[1] = docId;
147  List dList = AppBean.getPersistenceManager().queryDB("AnnotDocument.findById", params);
148  if (dList != null && !dList.isEmpty()) {
149  doc = (AnnotDocument) dList.get(0);
150  }
151  if (doc == null) {
153  String msg = "Document was not found.";
154  Logger.getLogger(GetDocServlet.class.getName()).log(Level.ALL, msg);
155  }
156  out.print(printError());
157  return;
158  }
159  }
160 
161  String docUriStr = request.getParameter("docUri");
162  if (docUriStr != null && !docUriStr.isEmpty()) {
163  try {
164  docUriStr = CoreFuncModule.cleanUpURI(docUriStr);
165  } catch (URISyntaxException ex) {
167  String msg = "Wrong URI of document.";
168  Logger.getLogger(GetDocServlet.class.getName()).log(Level.ALL, msg);
169  }
170  out.print(printError());
171  return;
172  }
173  }
174  if (docUriStr != null && !docUriStr.isEmpty()) {
175  Object[] params = new Object[2];
176  params[0] = "uri";
177  params[1] = docUriStr;
178  List dList = AppBean.getPersistenceManager().queryDB("AnnotDocument.findByUri", params);
179  if (dList != null && !dList.isEmpty()) {
180  doc = (AnnotDocument) dList.get(0);
181  }
182  if (doc == null) {
184  String msg = "Document was not found.";
185  Logger.getLogger(GetDocServlet.class.getName()).log(Level.ALL, msg);
186  }
187  out.print(printError());
188  return;
189  }
190  }
191 
192  Annotation annot = null;
193  if (id != null) {
194  Object[] params = new Object[2];
195  params[0] = "id";
196  params[1] = id;
197  List dList = AppBean.getPersistenceManager().queryDB("Annotation.findById", params);
198  if (dList != null && !dList.isEmpty()) {
199  annot = (Annotation) dList.get(0);
200  }
201  if (annot == null) {
203  String msg = "Annotation was not found.";
204  Logger.getLogger(GetDocServlet.class.getName()).log(Level.ALL, msg);
205  }
206  out.print(printError());
207  return;
208  }
209  }
210 
211  List<Annotation> outList = new ArrayList<Annotation>();
212  if (annot == null && doc != null && reqUserID != null) {
213  outList = getAllAnnotationsForDocAndAuthor(doc, reqUserID);
214  } else if (annot == null && doc != null) {
215  outList = getAllAnnotationsForDoc(doc);
216  } else if (annot == null && reqUserID != null) {
217  outList = getAllAnnotationsForAuthor(reqUserID);
218  } else if (annot != null && doc == null) {
219  outList.add(annot);
220  } else if (annot != null && doc != null) {
221  if (annot.getSourceDocument().equals(doc)) {
222  outList.add(annot);
223  }
224  }
225 
226  StringBuilder output = new StringBuilder();
227 
228  if (outputType == OUTPUT_TYPE_V1) {
229  output.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<annotations ");
230  //Attach namespaces
231  for (int i = 0; i < Constants.nameSpaces[Constants.NAMESPACES_LOD_V1].length; i++) {
232  output.append(Constants.nameSpaces[Constants.NAMESPACES_LOD_V1][i]);
233  }
234  output.append(">");
235  Iterator<Annotation> annotIt = outList.iterator();
236  while (annotIt.hasNext()) {
237  Annotation a = annotIt.next();
238  output.append(a.toXMLString(true));
239  }
240  output.append("</annotations>");
241  } else if (outputType == OUTPUT_TYPE_V2) {
242  output.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<annotations ");
243  //Attach namespaces
244  for (int i = 0; i < Constants.nameSpaces[Constants.NAMESPACES_LOD_V2].length; i++) {
245  output.append(Constants.nameSpaces[Constants.NAMESPACES_LOD_V2][i]);
246  }
247  output.append(">");
248  Iterator<Annotation> annotIt = outList.iterator();
249  while (annotIt.hasNext()) {
250  Annotation a = annotIt.next();
251  output.append(a.toXMLStringV2(null, Localisation.LANGUAGE_ENG, false));
252  }
253  output.append("</annotations>");
254  } else if (outputType == OUTPUT_TYPE_SUG) {
255  output.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n");
256  output.append("<suggestion xmlns:geo=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">");
257  Iterator<Annotation> annotIt = outList.iterator();
258  while (annotIt.hasNext()) {
259  Annotation a = annotIt.next();
260  output.append(a.toSXMLString(true, withCreated));
261  }
262  output.append("</suggestion>");
263  } else {
264  out.print(printError());
265  }
266 
267  out.append(output);
268  } finally {
269  out.close();
270  }
271  }
272 
273  /**
274  * Returns all annotations for given document
275  *
276  * @param doc Document for which annotations should be returned
277  * @return Returns list of annotations for given document
278  */
279  @SuppressWarnings("unchecked")
281  // query database for annotations for synchronized document
282  Object[] params = new Object[2];
283  params[0] = "sourceDocumentId";
284  params[1] = doc.getId();
285  List aList = AppBean.getPersistenceManager().queryDB("Annotation.findBySourceDocumentID", params);
286  if (aList != null && !aList.isEmpty()) { // if some annotation was found
287  return aList;
288  }
289 
290  return new ArrayList<Annotation>();
291  } // getAllAnnotationsForDoc()
292 
293  /**
294  * Returns all annotations for given document and author
295  *
296  * @param doc Document for which annotations should be returned
297  * @param author Id of author of annotations
298  * @return Returns list of annotations for given document and author
299  */
300  @SuppressWarnings("unchecked")
301  private List<Annotation> getAllAnnotationsForDocAndAuthor(AnnotDocument doc, Integer author) {
302  // query database for annotations for synchronized document
303  Object[] params = new Object[4];
304  params[0] = "sourceDocumentId";
305  params[1] = doc.getId();
306  params[2] = "authorId";
307  params[3] = author.toString();
308  List aList = AppBean.getPersistenceManager().queryDB("Annotation.findBySourceDocumentIDAndAuthor", params);
309  if (aList != null && !aList.isEmpty()) { // if some annotation was found
310  return aList;
311  }
312 
313  return new ArrayList<Annotation>();
314  } // getAllAnnotationsForDocAndAuthor()
315 
316  /**
317  * Returns all annotations for given author
318  *
319  * @param author Id of author of annotations
320  * @return Returns list of annotations for given author
321  */
322  @SuppressWarnings("unchecked")
323  private List<Annotation> getAllAnnotationsForAuthor(Integer author) {
324  // query database for annotations for synchronized document
325  Object[] params = new Object[2];
326  params[0] = "authorId";
327  params[1] = author.toString();
328  List aList = AppBean.getPersistenceManager().queryDB("Annotation.findByAuthor", params);
329  if (aList != null && !aList.isEmpty()) { // if some annotation was found
330  return aList;
331  }
332 
333  return new ArrayList<Annotation>();
334  } // getAllAnnotationsForAuthor()
335 
336  /**
337  * Creates error message for case of bad request
338  *
339  * @return Simple HTML page with error message
340  */
341  private String printError() {
342  return "<html>"
343  + "<head>"
344  + "<title>Servlet GetAnnotServlet</title>"
345  + "</head>"
346  + "<body>"
347  + "<h1>Error - requested informations was not found</h1>"
348  + "<p>Requested informations was not found.</p>"
349  + "</body>"
350  + "</html>";
351  }
352 
353  /**
354  * Handles the HTTP
355  * <code>GET</code> method.
356  *
357  * @param request servlet request
358  * @param response servlet response
359  * @throws ServletException if a servlet-specific error occurs
360  * @throws IOException if an I/O error occurs
361  */
362  @Override
363  protected void doGet(HttpServletRequest request, HttpServletResponse response)
364  throws ServletException, IOException {
365  processRequest(request, response);
366  }
367 
368  /**
369  * Handles the HTTP
370  * <code>POST</code> method.
371  *
372  * @param request servlet request
373  * @param response servlet response
374  * @throws ServletException if a servlet-specific error occurs
375  * @throws IOException if an I/O error occurs
376  */
377  @Override
378  protected void doPost(HttpServletRequest request, HttpServletResponse response)
379  throws ServletException, IOException {
380  processRequest(request, response);
381  }
382 
383  /**
384  * Returns a short description of the servlet.
385  *
386  * @return a String containing servlet description
387  */
388  @Override
389  public String getServletInfo() {
390  return "Servlet for getting of annotations.";
391  }
392 } // public class GetAnnotServlet
void doPost(HttpServletRequest request, HttpServletResponse response)
List< Annotation > getAllAnnotationsForAuthor(Integer author)
Class representing annotated copy of document.
Document, annotations and types manipulation functionality.
Singleton for storing global variables.
Definition: AppBean.java:47
Servlet which makes available annotations.
void processRequest(HttpServletRequest request, HttpServletResponse response)
Class representing user.
Definition: User.java:51
List< Annotation > getAllAnnotationsForDoc(AnnotDocument doc)
List< Annotation > getAllAnnotationsForDocAndAuthor(AnnotDocument doc, Integer author)
Class responsible for localised strings.
void doGet(HttpServletRequest request, HttpServletResponse response)