KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > http > HttpVCardServlet


1 /*
2   Copyright (C) 2004 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.http;
34
35 import javax.servlet.*;
36 import javax.servlet.http.*;
37
38 import java.sql.DriverManager JavaDoc;
39 import java.sql.SQLException JavaDoc;
40 import java.sql.Connection JavaDoc;
41
42 import com.knowgate.debug.DebugFile;
43 import com.knowgate.misc.Environment;
44 import com.knowgate.addrbook.Fellow;
45
46 /**
47  * <p>Get RFC 2426 vCard from Fellow or Contact</p>
48  * @author Sergio Montoro ten
49  * @version 2.1
50  */

51
52 public class HttpVCardServlet extends HttpServlet {
53
54  // -----------------------------------------------------------
55

56  private boolean isVoid(String JavaDoc sParam) {
57    if (null==sParam)
58      return true;
59    else
60      return (sParam.length()==0);
61  }
62
63  /**
64   * <p>Initialize Servlet Parameters</p>
65   * Take Database Driver, Conenction URL and User from /WEB-INF/web.xml.<br>
66   * If any parameter is not found then look it up at hipergate.cnf Properties
67   * file using Environment singleton.
68   * @throws ServletException
69   * @throws UnavailableException If jdbcDriverClassName parameter is not found
70   * and driver property at hipergate.cnf is not found or if jdbcURL parameter
71   * is not found and dburl property at hipergate.cnf is not found.
72   * @see com.knowgate.misc.Environment
73   */

74
75  public void init() throws ServletException {
76    ServletConfig config = getServletConfig();
77
78    jdbcDriverClassName = config.getInitParameter("jdbcDriverClassName");
79    jdbcURL = config.getInitParameter("jdbcURL");
80    dbUserName = config.getInitParameter("dbUserName");
81    dbUserPassword = config.getInitParameter("dbUserPassword");
82
83    if (isVoid(jdbcDriverClassName) || isVoid(jdbcURL) || isVoid(dbUserName) || isVoid(dbUserPassword)) {
84      java.util.Properties JavaDoc env = Environment.getProfile("hipergate");
85
86      if (isVoid(jdbcDriverClassName))
87        jdbcDriverClassName = env.getProperty("driver");
88
89      if (isVoid(jdbcURL))
90        jdbcURL = env.getProperty("dburl");
91
92      if (isVoid(dbUserName))
93        dbUserName = env.getProperty("dbuser");
94
95      if (isVoid(dbUserPassword))
96        dbUserPassword = env.getProperty("dbpassword");
97    }
98
99    if (jdbcDriverClassName == null || jdbcURL == null) {
100      throw new UnavailableException("Init params missing");
101    }
102  } // init()
103

104  // -----------------------------------------------------------
105

106  /**
107   * <p>Send Fellow vCard to HttpServletResponse OutputStream</p>
108   * @throws IOException
109   * @throws ServletException
110   */

111  public void doGet(HttpServletRequest request, HttpServletResponse response)
112     throws java.io.IOException JavaDoc, ServletException
113     {
114     boolean bFound;
115     Class JavaDoc oDriver;
116     Connection JavaDoc oConn = null;
117     Fellow oFlw = new Fellow();
118     String JavaDoc sPKFld, sPKVal, vCard, sNick = null;
119
120      if (DebugFile.trace) {
121        DebugFile.writeln("Begin HttpVCardServlet.doGet");
122        DebugFile.incIdent();
123      }
124
125     try {
126       oDriver = Class.forName(jdbcDriverClassName);
127     }
128     catch (ClassNotFoundException JavaDoc ignore) {
129       oDriver = null;
130       if (DebugFile.trace) DebugFile.writeln("Class.forName(" + jdbcDriverClassName + ") : " + ignore.getMessage());
131       response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Database driver not found");
132     }
133
134     if (null==oDriver) return;
135
136     ServletOutputStream oOut = response.getOutputStream();
137
138     try {
139       if (DebugFile.trace) DebugFile.writeln("DriverManager.getConnection(" + jdbcURL + ",...)");
140
141       oConn = DriverManager.getConnection(jdbcURL,dbUserName,dbUserPassword);
142
143       sPKFld = (request.getParameter("pk_field")!=null ? request.getParameter("pk_field") : "null");
144       sPKVal = (request.getParameter("pk_value")!=null ? request.getParameter("pk_value") : "null");
145
146       if (DebugFile.trace) DebugFile.writeln("pk_field = " + sPKFld);
147
148       if (sPKFld.equalsIgnoreCase("gu_fellow")) {
149         bFound = oFlw.load(oConn, new Object JavaDoc[]{sPKVal});
150
151         if (!oFlw.isNull("tx_nickname"))
152           sNick = oFlw.getString("tx_nickname");
153         else
154           sNick = sPKVal;
155       }
156       else
157         bFound = false;
158
159       if (bFound) {
160
161         if (DebugFile.trace) {
162           DebugFile.writeln("response.setContentType(\"application/directory\")");
163           DebugFile.writeln("response.setHeader(\"Content-Disposition\", \"attachment; filename=\"" + sNick + ".vcf\"");
164         }
165
166         // Send some basic http headers to support binary d/l.
167
response.setContentType("application/directory");
168         response.setHeader("Content-Disposition", "attachment; filename=\"" + sNick + ".vcf\"");
169
170         oOut.print(oFlw.vCard(oConn));
171
172         oOut.flush();
173       } // fi (bFound)
174

175       if (!bFound) {
176         if (DebugFile.trace) DebugFile.writeln("SQLException: Cannot find requested document");
177
178         response.sendError(HttpServletResponse.SC_NOT_FOUND, "Cannot find requested document");
179       }
180
181       oConn.close();
182       oConn = null;
183     }
184     catch (SQLException JavaDoc e) {
185       bFound = false;
186
187       if (DebugFile.trace) DebugFile.writeln("SQLException: " + e.getMessage());
188
189       response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
190     }
191     try { if(null!=oConn) if(!oConn.isClosed()) oConn.close(); } catch (SQLException JavaDoc e) { }
192
193     if (DebugFile.trace) {
194       DebugFile.decIdent();
195       DebugFile.writeln("End HttpVCardServlet().doGet()");
196     }
197   } // doGet()
198

199   // **********************************************************
200
// * Variables privadas
201

202   private String JavaDoc jdbcDriverClassName;
203   private String JavaDoc jdbcURL;
204   private String JavaDoc dbUserName;
205   private String JavaDoc dbUserPassword;
206 }
207
Popular Tags