001: /*
002: Copyright (C) 2004 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.http;
034:
035: import javax.servlet.*;
036: import javax.servlet.http.*;
037:
038: import java.sql.DriverManager;
039: import java.sql.SQLException;
040: import java.sql.Connection;
041:
042: import com.knowgate.debug.DebugFile;
043: import com.knowgate.misc.Environment;
044: import com.knowgate.addrbook.Fellow;
045:
046: /**
047: * <p>Get RFC 2426 vCard from Fellow or Contact</p>
048: * @author Sergio Montoro ten
049: * @version 2.1
050: */
051:
052: public class HttpVCardServlet extends HttpServlet {
053:
054: // -----------------------------------------------------------
055:
056: private boolean isVoid(String sParam) {
057: if (null == sParam)
058: return true;
059: else
060: return (sParam.length() == 0);
061: }
062:
063: /**
064: * <p>Initialize Servlet Parameters</p>
065: * Take Database Driver, Conenction URL and User from /WEB-INF/web.xml.<br>
066: * If any parameter is not found then look it up at hipergate.cnf Properties
067: * file using Environment singleton.
068: * @throws ServletException
069: * @throws UnavailableException If jdbcDriverClassName parameter is not found
070: * and driver property at hipergate.cnf is not found or if jdbcURL parameter
071: * is not found and dburl property at hipergate.cnf is not found.
072: * @see com.knowgate.misc.Environment
073: */
074:
075: public void init() throws ServletException {
076: ServletConfig config = getServletConfig();
077:
078: jdbcDriverClassName = config
079: .getInitParameter("jdbcDriverClassName");
080: jdbcURL = config.getInitParameter("jdbcURL");
081: dbUserName = config.getInitParameter("dbUserName");
082: dbUserPassword = config.getInitParameter("dbUserPassword");
083:
084: if (isVoid(jdbcDriverClassName) || isVoid(jdbcURL)
085: || isVoid(dbUserName) || isVoid(dbUserPassword)) {
086: java.util.Properties env = Environment
087: .getProfile("hipergate");
088:
089: if (isVoid(jdbcDriverClassName))
090: jdbcDriverClassName = env.getProperty("driver");
091:
092: if (isVoid(jdbcURL))
093: jdbcURL = env.getProperty("dburl");
094:
095: if (isVoid(dbUserName))
096: dbUserName = env.getProperty("dbuser");
097:
098: if (isVoid(dbUserPassword))
099: dbUserPassword = env.getProperty("dbpassword");
100: }
101:
102: if (jdbcDriverClassName == null || jdbcURL == null) {
103: throw new UnavailableException("Init params missing");
104: }
105: } // init()
106:
107: // -----------------------------------------------------------
108:
109: /**
110: * <p>Send Fellow vCard to HttpServletResponse OutputStream</p>
111: * @throws IOException
112: * @throws ServletException
113: */
114: public void doGet(HttpServletRequest request,
115: HttpServletResponse response) throws java.io.IOException,
116: ServletException {
117: boolean bFound;
118: Class oDriver;
119: Connection oConn = null;
120: Fellow oFlw = new Fellow();
121: String sPKFld, sPKVal, vCard, sNick = null;
122:
123: if (DebugFile.trace) {
124: DebugFile.writeln("Begin HttpVCardServlet.doGet");
125: DebugFile.incIdent();
126: }
127:
128: try {
129: oDriver = Class.forName(jdbcDriverClassName);
130: } catch (ClassNotFoundException ignore) {
131: oDriver = null;
132: if (DebugFile.trace)
133: DebugFile.writeln("Class.forName("
134: + jdbcDriverClassName + ") : "
135: + ignore.getMessage());
136: response.sendError(
137: HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
138: "Database driver not found");
139: }
140:
141: if (null == oDriver)
142: return;
143:
144: ServletOutputStream oOut = response.getOutputStream();
145:
146: try {
147: if (DebugFile.trace)
148: DebugFile.writeln("DriverManager.getConnection("
149: + jdbcURL + ",...)");
150:
151: oConn = DriverManager.getConnection(jdbcURL, dbUserName,
152: dbUserPassword);
153:
154: sPKFld = (request.getParameter("pk_field") != null ? request
155: .getParameter("pk_field")
156: : "null");
157: sPKVal = (request.getParameter("pk_value") != null ? request
158: .getParameter("pk_value")
159: : "null");
160:
161: if (DebugFile.trace)
162: DebugFile.writeln("pk_field = " + sPKFld);
163:
164: if (sPKFld.equalsIgnoreCase("gu_fellow")) {
165: bFound = oFlw.load(oConn, new Object[] { sPKVal });
166:
167: if (!oFlw.isNull("tx_nickname"))
168: sNick = oFlw.getString("tx_nickname");
169: else
170: sNick = sPKVal;
171: } else
172: bFound = false;
173:
174: if (bFound) {
175:
176: if (DebugFile.trace) {
177: DebugFile
178: .writeln("response.setContentType(\"application/directory\")");
179: DebugFile
180: .writeln("response.setHeader(\"Content-Disposition\", \"attachment; filename=\""
181: + sNick + ".vcf\"");
182: }
183:
184: // Send some basic http headers to support binary d/l.
185: response.setContentType("application/directory");
186: response.setHeader("Content-Disposition",
187: "attachment; filename=\"" + sNick + ".vcf\"");
188:
189: oOut.print(oFlw.vCard(oConn));
190:
191: oOut.flush();
192: } // fi (bFound)
193:
194: if (!bFound) {
195: if (DebugFile.trace)
196: DebugFile
197: .writeln("SQLException: Cannot find requested document");
198:
199: response.sendError(HttpServletResponse.SC_NOT_FOUND,
200: "Cannot find requested document");
201: }
202:
203: oConn.close();
204: oConn = null;
205: } catch (SQLException e) {
206: bFound = false;
207:
208: if (DebugFile.trace)
209: DebugFile.writeln("SQLException: " + e.getMessage());
210:
211: response.sendError(
212: HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e
213: .getMessage());
214: }
215: try {
216: if (null != oConn)
217: if (!oConn.isClosed())
218: oConn.close();
219: } catch (SQLException e) {
220: }
221:
222: if (DebugFile.trace) {
223: DebugFile.decIdent();
224: DebugFile.writeln("End HttpVCardServlet().doGet()");
225: }
226: } // doGet()
227:
228: // **********************************************************
229: // * Variables privadas
230:
231: private String jdbcDriverClassName;
232: private String jdbcURL;
233: private String dbUserName;
234: private String dbUserPassword;
235: }
|