4A Server -  2.0
 All Classes Namespaces Files Functions Variables Enumerator
GetDocServlet.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: GetDocServlet.java
5  * Description: Servlet which makes available annotated copies of documents
6  */
7 
8 /**
9  * @file GetDocServlet.java
10  *
11  * @brief Servlet which makes available annotated copies of documents
12  */
13 
14 package cz.vutbr.fit.knot.annotations.comet;
15 
19 import java.io.IOException;
20 import java.io.PrintWriter;
21 import java.util.List;
22 import java.util.logging.Level;
23 import java.util.logging.Logger;
24 import javax.servlet.ServletException;
25 import javax.servlet.http.HttpServlet;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 
29 /**
30  * Servlet which makes available annotated copies of documents
31  *
32  * @brief Servlet which makes available annotated copies of documents
33  * @author idytrych
34  */
35 public class GetDocServlet extends HttpServlet {
36 
37  /**
38  * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
39  * Gets document from database and returns document content to the client.
40  *
41  * @param request servlet request
42  * @param response servlet response
43  * @throws ServletException if a servlet-specific error occurs
44  * @throws IOException if an I/O error occurs
45  */
46  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
47  throws ServletException, IOException {
48  response.setContentType("text/html;charset=UTF-8");
49  PrintWriter out = response.getWriter();
50  try {
51  String idStr = request.getParameter("id");
52  Integer id = null;
53  try {
54  id = Integer.parseInt(idStr);
55  } catch (NumberFormatException nfe) {
57  String msg = "NumberFormatException";
58  Logger.getLogger(GetDocServlet.class.getName()).log(Level.ALL, msg);
59  }
60  out.print(printError());
61  return;
62  }
63  AnnotDocument doc = null;
64  Object[] params = new Object[2];
65  params[0] = "id";
66  params[1] = id;
67  List dList = AppBean.getPersistenceManager().queryDB("AnnotDocument.findById", params);
68  if (dList != null && !dList.isEmpty()) {
69  doc = (AnnotDocument) dList.get(0);
70  }
71  if (doc == null) {
73  String msg = "Document not found.";
74  Logger.getLogger(GetDocServlet.class.getName()).log(Level.ALL, msg);
75  }
76  out.print(printError());
77  return;
78  }
79  out.print(doc.getContent());
80  } finally {
81  out.close();
82  }
83  } // processRequest()
84 
85  /**
86  * Creates error message for case document isn't found
87  *
88  * @return Simple HTML page with error message
89  */
90  private String printError() {
91  return "<html>"
92  + "<head>"
93  + "<title>Servlet GetDocServlet</title>"
94  + "</head>"
95  + "<body>"
96  + "<h1>Error - document not found</h1>"
97  + "<p>Requested document not found.</p>"
98  + "</body>"
99  + "</html>";
100  }
101 
102 
103  /**
104  * Handles the HTTP <code>GET</code> method.
105  *
106  * @param request servlet request
107  * @param response servlet response
108  * @throws ServletException if a servlet-specific error occurs
109  * @throws IOException if an I/O error occurs
110  */
111  @Override
112  protected void doGet(HttpServletRequest request, HttpServletResponse response)
113  throws ServletException, IOException {
114  processRequest(request, response);
115  }
116 
117  /**
118  * Handles the HTTP <code>POST</code> method.
119  *
120  * @param request servlet request
121  * @param response servlet response
122  * @throws ServletException if a servlet-specific error occurs
123  * @throws IOException if an I/O error occurs
124  */
125  @Override
126  protected void doPost(HttpServletRequest request, HttpServletResponse response)
127  throws ServletException, IOException {
128  processRequest(request, response);
129  }
130 
131  /**
132  * Returns a short description of the servlet.
133  *
134  * @return a String containing servlet description
135  */
136  @Override
137  public String getServletInfo() {
138  return "Servlet for getting annotated copy of document.";
139  }
140 
141 } // class GetDocServlet
void doGet(HttpServletRequest request, HttpServletResponse response)
Class representing annotated copy of document.
Singleton for storing global variables.
Definition: AppBean.java:47
void processRequest(HttpServletRequest request, HttpServletResponse response)
void doPost(HttpServletRequest request, HttpServletResponse response)
Servlet which makes available annotated copies of documents.