KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bluecubs > xinco > server > XincoPublisherServlet


1 /**
2  *Copyright 2006 blueCubs.com
3  *
4  *Licensed under the Apache License, Version 2.0 (the "License");
5  *you may not use this file except in compliance with the License.
6  *You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *Unless required by applicable law or agreed to in writing, software
11  *distributed under the License is distributed on an "AS IS" BASIS,
12  *WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *See the License for the specific language governing permissions and
14  *limitations under the License.
15  *
16  *************************************************************
17  * This project supports the blueCubs vision of giving back
18  * to the community in exchange for free software!
19  * More information on: http://www.bluecubs.org
20  *************************************************************
21  *
22  * Name: XincoPublisherServlet
23  *
24  * Description: publisher servlet
25  *
26  * Original Author: Alexander Manes
27  * Date: 2004
28  *
29  * Modifications:
30  *
31  * Who? When? What?
32  * - - -
33  *
34  *************************************************************
35  */

36
37 package com.bluecubs.xinco.server;
38
39 import java.io.*;
40 import java.sql.ResultSet JavaDoc;
41 import java.sql.Statement JavaDoc;
42 import javax.servlet.*;
43 import javax.servlet.http.*;
44 import com.bluecubs.xinco.core.*;
45 import com.bluecubs.xinco.add.*;
46 import com.bluecubs.xinco.core.server.*;
47
48 public class XincoPublisherServlet extends HttpServlet {
49     
50     /** Initializes the servlet.
51      */

52     public void init(ServletConfig config) throws ServletException {
53         super.init(config);
54     }
55     
56     /** Destroys the servlet.
57      */

58     public void destroy() {
59     }
60     
61     /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
62      * @param request servlet request
63      * @param response servlet response
64      */

65     protected synchronized void processRequest(HttpServletRequest request, HttpServletResponse response)
66     throws ServletException, IOException {
67         
68         int i = 0;
69         int j = 0;
70         String JavaDoc request_path;
71         String JavaDoc request_path_array[];
72         XincoDBManager dbm;
73         boolean fileDownload = false;
74         int core_data_id = 0;
75         XincoCoreDataServer xcd = null;
76         XincoCoreDataTypeAttribute xcdta = null;
77         XincoAddAttribute xaa = null;
78         boolean printList = false;
79         boolean browseFolder = false;
80         String JavaDoc temp_url = "";
81         String JavaDoc temp_server_url = "";
82         boolean isPublic = false;
83         
84         //connect to db
85
try {
86             dbm = new XincoDBManager();
87         } catch (Exception JavaDoc e) {
88             //start output
89
response.setContentType("text/html");
90             PrintWriter out = response.getWriter();
91             out.println(e.toString());
92             return;
93         }
94
95         //get requested data
96
if (request.getParameter("MainMenu") == null) {
97             request_path = request.getPathInfo();
98             if (request_path != null) {
99                 request_path_array = request_path.split("/");
100                 if (!(request_path_array.length > 1)) {
101                     core_data_id = 0;
102                 } else {
103                     try {
104                         core_data_id = Integer.parseInt(request_path_array[1]);
105                         xcd = new XincoCoreDataServer(core_data_id, dbm);
106                         isPublic = false;
107                         //check status (5 = published)
108
if (xcd.getStatus_number() == 5) {
109                             isPublic = true;
110                         } else {
111                             //check read permission for group "public"
112
for (i=0;i<xcd.getXinco_core_acl().size();i++) {
113                                 if ((((XincoCoreACE)xcd.getXinco_core_acl().elementAt(i)).getXinco_core_group_id() == 3)
114                                         && ((XincoCoreACE)xcd.getXinco_core_acl().elementAt(i)).isRead_permission()) {
115                                     isPublic = true;
116                                     break;
117                                 }
118                             }
119                         }
120                         if (!isPublic) {
121                             core_data_id = -1;
122                         }
123                     } catch (Exception JavaDoc e) {
124                         core_data_id = -1;
125                     }
126                 }
127             } else {
128                 core_data_id = 0;
129             }
130         } else {
131             core_data_id = 0;
132             if (request.getParameter("MainMenu").compareTo("list") == 0) {
133                 printList = true;
134             } else if ((request.getParameter("MainMenu").compareTo("browse") == 0)) {
135                 browseFolder = true;
136             }
137         }
138         //check data type
139
if (core_data_id > 0) {
140             if (xcd.getXinco_core_data_type().getId() == 1) {
141                 fileDownload = true;
142             } else {
143                 fileDownload = false;
144             }
145         }
146         
147         //generate specific output
148
if (fileDownload) { // begin FILE output
149

150             try {
151                 response.setContentType("unknown/unknown");
152                 OutputStream out = response.getOutputStream();
153                 
154                 FileInputStream in = new FileInputStream(dbm.config.FileRepositoryPath + core_data_id);
155                 byte[] buf = new byte[4096];
156                 int len;
157                 while ((len = in.read(buf)) > 0) {
158                     out.write(buf, 0, len);
159                 }
160                 in.close();
161             } catch (Exception JavaDoc e) {
162                 System.out.println(e);
163             }
164             
165             //end FILE output
166

167         } else { // begin HTML output
168

169             //start output
170
response.setContentType("text/html");
171             PrintWriter out = response.getWriter();
172             
173             //show header
174
out.println("<html>");
175             out.println("<head>");
176             if (core_data_id == 0) {
177                 out.println("<title>XincoPublisher</title>");
178                 out.println("<link rel=\"stylesheet\" HREF=\"xincostyle.css\" type=\"text/css\"/>");
179             }
180             if (core_data_id > 0) {
181                 out.println("<title>" + xcd.getDesignation() + "</title>");
182                 out.println("<link rel=\"stylesheet\" HREF=\"../../xincostyle.css\" type=\"text/css\"/>");
183             }
184             out.println("</head>");
185             out.println("<body>");
186             out.println("<center>");
187             out.println("<span class=\"text\">");
188                     
189             out.println("");
190     
191             //show main menu
192
if (core_data_id == 0) {
193                 out.println("<br>");
194                 out.println("<table border=\"0\" width=\"750\" cellspacing=\"10\" cellpadding=\"0\">");
195                 out.println("<tr>");
196                 out.println("<td class=\"text\" width=\"100\"><img SRC=\"blueCubsSmall.gif\" border=\"0\"/></td>");
197                 out.println("<td class=\"bigtext\" width=\"650\">XincoPublisher</td>");
198                 out.println("</tr>");
199                 out.println("</table>");
200                 out.println("<br>");
201                 out.println("<table border=\"0\" cellspacing=\"10\" cellpadding=\"0\">");
202                 if (printList) {
203                     try {
204                         XincoCoreDataServer xdata_temp = null;
205                         Statement JavaDoc stmt = dbm.con.createStatement();
206                         ResultSet JavaDoc rs = stmt.executeQuery("SELECT DISTINCT xcd.id, xcd.designation FROM xinco_core_data xcd, xinco_core_ace xca WHERE xcd.id=xca.xinco_core_data_id AND (xcd.status_number=5 OR (xca.xinco_core_group_id=3 AND xca.read_permission=1)) ORDER BY xcd.designation");
207                         while (rs.next()) {
208                             xdata_temp = new XincoCoreDataServer(rs.getInt("id"), dbm);
209                             temp_server_url = request.getRequestURL().toString();
210                             temp_url = "";
211                             //file = 1
212
if (xdata_temp.getXinco_core_data_type().getId() == 1) {
213                                 temp_url = ((XincoAddAttribute)xdata_temp.getXinco_add_attributes().elementAt(0)).getAttrib_varchar();
214                             } else {
215                                 temp_url = xdata_temp.getDesignation();
216                             }
217                             out.println("<tr>");
218                             out.println("<td class=\"text\">" + xdata_temp.getDesignation() + " (" + xdata_temp.getXinco_core_data_type().getDesignation() + " | " + xdata_temp.getXinco_core_language().getSign() + ")" + "</td>");
219                             out.println("<td class=\"text\"><a HREF=\"" + "XincoPublisher/" + xdata_temp.getId() + "/" + temp_url + "\" target=\"_blank\">" + temp_server_url + "/" + xdata_temp.getId() + "/" + temp_url + "</a></td>");
220                             out.println("</tr>");
221                             out.flush();
222                         }
223                         stmt.close();
224                     } catch (Exception JavaDoc sqle) {
225                     }
226                 } else if (browseFolder) {
227                     try {
228                         XincoCoreNodeServer xnode_temp = null;
229                         XincoCoreNodeServer xnode_temp2 = null;
230                         XincoCoreDataServer xdata_temp = null;
231                         String JavaDoc temp_path = null;
232                         String JavaDoc temp_path2 = null;
233                         int temp_xcn_id = 0;
234                         
235                         if (!(request.getParameter("FolderId") == null)) {
236                             temp_xcn_id = Integer.parseInt(request.getParameter("FolderId"));
237                             xnode_temp = new XincoCoreNodeServer(temp_xcn_id, dbm);
238                             //check read permission for group "public"
239
isPublic = false;
240                             for (i=0;i<xnode_temp.getXinco_core_acl().size();i++) {
241                                 if ((((XincoCoreACE)xnode_temp.getXinco_core_acl().elementAt(i)).getXinco_core_group_id() == 3)
242                                         && ((XincoCoreACE)xnode_temp.getXinco_core_acl().elementAt(i)).isRead_permission()) {
243                                     isPublic = true;
244                                     break;
245                                 }
246                             }
247                             if (isPublic) {
248                                 xnode_temp.fillXincoCoreNodes(dbm);
249                                 xnode_temp.fillXincoCoreData(dbm);
250                                 // print current path
251
if (!(request.getParameter("Path") == null)) {
252                                     temp_path = request.getParameter("Path");
253                                     temp_path = new String JavaDoc(new sun.misc.BASE64Decoder().decodeBuffer(temp_path));
254                                     out.println("<tr>");
255                                     out.println("<td colspan=\"2\" class=\"text\"><b>Path:</b> " + temp_path + "</td>");
256                                     out.println("</tr>");
257                                     out.println("<tr>");
258                                     out.println("<td colspan=\"2\" class=\"text\">&nbsp;</td>");
259                                     out.println("</tr>");
260                                     out.flush();
261                                 } else {
262                                     temp_path = null;
263                                 }
264                                 // list public folders
265
out.println("<tr>");
266                                 out.println("<td colspan=\"2\" class=\"text\"><b>Public Sub-Folders:</b></td>");
267                                 out.println("</tr>");
268                                 out.flush();
269                                 for (i=0;i<xnode_temp.getXinco_core_nodes().size();i++) {
270                                     xnode_temp2 = new XincoCoreNodeServer(((XincoCoreNodeServer)xnode_temp.getXinco_core_nodes().elementAt(i)).getId(), dbm);
271                                     isPublic = false;
272                                     //check read permission for group "public"
273
for (j=0;j<xnode_temp2.getXinco_core_acl().size();j++) {
274                                         if ((((XincoCoreACE)xnode_temp2.getXinco_core_acl().elementAt(j)).getXinco_core_group_id() == 3)
275                                                 && ((XincoCoreACE)xnode_temp2.getXinco_core_acl().elementAt(j)).isRead_permission()) {
276                                             isPublic = true;
277                                             break;
278                                         }
279                                     }
280                                     if (isPublic) {
281                                         if (temp_path != null) {
282                                             temp_path2 = temp_path + " / " + xnode_temp2.getDesignation() + " (" + xnode_temp2.getXinco_core_language().getSign() + ")";
283                                             temp_path2 = new sun.misc.BASE64Encoder().encode(temp_path2.getBytes());
284                                             temp_path2 = "&Path=" + temp_path2;
285                                         } else {
286                                             temp_path2 = "";
287                                         }
288                                         out.println("<tr>");
289                                         out.println("<td class=\"text\">&nbsp;</td>");
290                                         out.println("<td class=\"text\"><a HREF=\"" + "XincoPublisher?MainMenu=browse&FolderId=" + xnode_temp2.getId() + temp_path2 + "\">[" + xnode_temp2.getDesignation() + " (" + xnode_temp2.getXinco_core_language().getSign() + ")" + "]</a></td>");
291                                         out.println("</tr>");
292                                         out.flush();
293                                     }
294                                 }
295                                 out.println("<tr>");
296                                 out.println("<td colspan=\"2\" class=\"text\">&nbsp;</td>");
297                                 out.println("</tr>");
298                                 out.flush();
299                                 // list public data
300
out.println("<tr>");
301                                 out.println("<td colspan=\"2\" class=\"text\"><b>Public Data:</b></td>");
302                                 out.println("</tr>");
303                                 out.flush();
304                                 for (i=0;i<xnode_temp.getXinco_core_data().size();i++) {
305                                     xdata_temp = new XincoCoreDataServer(((XincoCoreDataServer)xnode_temp.getXinco_core_data().elementAt(i)).getId(), dbm);
306                                     isPublic = false;
307                                     //check status (5 = published)
308
if (xdata_temp.getStatus_number() == 5) {
309                                         isPublic = true;
310                                     } else {
311                                         //check read permission for group "public"
312
for (j=0;j<xdata_temp.getXinco_core_acl().size();j++) {
313                                             if ((((XincoCoreACE)xdata_temp.getXinco_core_acl().elementAt(j)).getXinco_core_group_id() == 3)
314                                                     && ((XincoCoreACE)xdata_temp.getXinco_core_acl().elementAt(j)).isRead_permission()) {
315                                                 isPublic = true;
316                                                 break;
317                                             }
318                                         }
319                                     }
320                                     if (isPublic) {
321                                         temp_server_url = request.getRequestURL().toString();
322                                         temp_url = "";
323                                         //file = 1
324
if (xdata_temp.getXinco_core_data_type().getId() == 1) {
325                                             temp_url = ((XincoAddAttribute)xdata_temp.getXinco_add_attributes().elementAt(0)).getAttrib_varchar();
326                                         } else {
327                                             temp_url = xdata_temp.getDesignation();
328                                         }
329                                         out.println("<tr>");
330                                         out.println("<td class=\"text\">" + xdata_temp.getDesignation() + " (" + xdata_temp.getXinco_core_data_type().getDesignation() + " | " + xdata_temp.getXinco_core_language().getSign() + ")" + "</td>");
331                                         out.println("<td class=\"text\"><a HREF=\"" + "XincoPublisher/" + xdata_temp.getId() + "/" + temp_url + "\" target=\"_blank\">" + temp_server_url + "/" + xdata_temp.getId() + "/" + temp_url + "</a></td>");
332                                         out.println("</tr>");
333                                         out.flush();
334                                     }
335                                 }
336                             }
337                         }
338                     } catch (Exception JavaDoc sqle) {
339                     }
340                 } else {
341                     out.println("<tr>");
342                     out.println("<td class=\"text\" colspan=\"2\"><a HREF=\"XincoPublisher?MainMenu=list\" class=\"link\"><b>List</b></a> all publicly available data!</td>");
343                     out.println("</tr>");
344                     out.println("<tr>");
345                     out.println("<td class=\"text\" colspan=\"2\"><a HREF=\"XincoPublisher?MainMenu=browse&FolderId=1&Path=" + (new sun.misc.BASE64Encoder().encode((new String JavaDoc("xincoRoot")).getBytes())) + "\" class=\"link\"><b>Browse</b></a> the repository for publicly available data!</td>");
346                     out.println("</tr>");
347                 }
348                 out.println("<tr>");
349                 out.println("<td class=\"text\" colspan=\"2\">&nbsp;<br><br></td>");
350                 out.println("</tr>");
351                 out.println("<tr>");
352                 out.println("<td class=\"text\" colspan=\"2\"><b>HowTo:</b><br>When linking to published documents, use the following URL Syntax:<br><b>http://[server_name]:[port]/xinco/XincoPublisher/[id]/[designation|filename]</b></td>");
353                 out.println("</tr>");
354                 out.println("</table>");
355             }
356             if (core_data_id > 0) {
357
358                 out.println("<br>");
359                 out.println("<table border=\"0\" cellspacing=\"10\" cellpadding=\"0\">");
360                 out.println("<tr>");
361                 //out.println("<td class=\"text\">Designation:</td>");
362
out.println("<td class=\"bigtext\" colspan=\"2\">" + xcd.getDesignation() + "</td>");
363                 out.println("</tr>");
364                 out.println("<tr>");
365                 out.println("<td class=\"text\" colspan=\"2\">&nbsp;</td>");
366                 out.println("</tr>");
367
368                 //print additional attributes
369
for (i=0;i<xcd.getXinco_add_attributes().size();i++) {
370                     xcdta = ((XincoCoreDataTypeAttribute)xcd.getXinco_core_data_type().getXinco_core_data_type_attributes().elementAt(i));
371                     xaa = ((XincoAddAttribute)xcd.getXinco_add_attributes().elementAt(i));
372                     out.println("<tr>");
373                     out.println("<td class=\"text\" valign=\"top\"><b>" + xcdta.getDesignation() + ":</b></td>");
374                     if (xcdta.getData_type().toLowerCase().compareTo("int") == 0) {
375                         out.println("<td class=\"text\"><pre>" + xaa.getAttrib_int() + "</pre></td>");
376                     }
377                     if (xcdta.getData_type().toLowerCase().compareTo("unsignedint") == 0) {
378                         out.println("<td class=\"text\"><pre>" + xaa.getAttrib_unsignedint() + "</pre></td>");
379                     }
380                     if (xcdta.getData_type().toLowerCase().compareTo("double") == 0) {
381                         out.println("<td class=\"text\"><pre>" + xaa.getAttrib_double() + "</pre></td>");
382                     }
383                     if (xcdta.getData_type().toLowerCase().compareTo("varchar") == 0) {
384                         out.println("<td class=\"text\"><pre>" + xaa.getAttrib_varchar() + "</pre></td>");
385                     }
386                     if (xcdta.getData_type().toLowerCase().compareTo("text") == 0) {
387                         out.println("<td class=\"text\"><pre>" + xaa.getAttrib_text() + "</pre></td>");
388                     }
389                     if (xcdta.getData_type().toLowerCase().compareTo("datetime") == 0) {
390                         out.println("<td class=\"text\"><pre>" + xaa.getAttrib_datetime() + "</pre></td>");
391                     }
392                     out.println("</tr>");
393                 }
394                 
395                 out.println("</table>");
396                 
397             }
398
399             //show footer
400
if (core_data_id == 0) {
401                 out.println("<br><br><br>");
402                 out.println("<table border=\"0\" cellspacing=\"10\" cellpadding=\"0\">");
403                 out.println("<tr>");
404                 out.println("<td class=\"text\">&nbsp;</td>");
405                 out.println("<td class=\"text\">&copy; 2004-2006, <a HREF=\"http://www.bluecubs.com\" target=\"_blank\" class=\"link\">blueCubs.com</a> and <a HREF=\"http://www.xinco.org\" target=\"_blank\" class=\"link\">xinco.org</a></td>");
406                 out.println("</tr>");
407                 out.println("</table>");
408             }
409         
410             out.println("</span>");
411             out.println("</center>");
412             out.println("</body>");
413             out.println("</html>");
414     
415             out.close();
416             
417         } //end HTML output
418

419         //close db connection
420
try {
421             dbm.con.close();
422         } catch (Exception JavaDoc e) {
423         }
424         
425     }
426     
427     /** Handles the HTTP <code>GET</code> method.
428      * @param request servlet request
429      * @param response servlet response
430      */

431     protected void doGet(HttpServletRequest request, HttpServletResponse response)
432     throws ServletException, IOException {
433         processRequest(request, response);
434     }
435     
436     /** Handles the HTTP <code>POST</code> method.
437      * @param request servlet request
438      * @param response servlet response
439      */

440     protected void doPost(HttpServletRequest request, HttpServletResponse response)
441     throws ServletException, IOException {
442         processRequest(request, response);
443     }
444     
445     /** Returns a short description of the servlet.
446      */

447     public String JavaDoc getServletInfo() {
448         return "Servlet of xinco";
449     }
450     
451 }
452
Popular Tags