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