KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > wstore > Click


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Smart Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.wstore;
15
16 import javax.servlet.*;
17 import javax.servlet.http.*;
18 import java.io.*;
19 import java.util.*;
20 import java.sql.*;
21
22 import org.compiere.www.*;
23 import org.compiere.model.*;
24 import org.compiere.util.*;
25
26 /**
27  * Click Counter.
28  * Counts the click and forwards.
29  * <code>
30     http://www.compiere.com/wstore/click?target=www.yahoo.com
31     http://www.compiere.com/wstore/click/www.yahoo.com
32     http://www.compiere.com/wstore/click?www.yahoo.com
33  * </code>
34  *
35  * @author Jorg Janke
36  * @version $Id: Click.java,v 1.3 2003/10/04 03:58:26 jjanke Exp $
37  */

38 public class Click extends HttpServlet implements Runnable JavaDoc
39 {
40     /** Logging */
41     private Logger log = Logger.getCLogger(getClass());
42
43     /** Name */
44     static public final String JavaDoc NAME = "click";
45
46     /** Target Parameter */
47     static public final String JavaDoc PARA_TARGET = "target";
48     static public final String JavaDoc DEFAULT_TARGET = "http://www.compiere.org/";
49
50     /** Requests */
51     private List m_requests = Collections.synchronizedList(new ArrayList());
52
53     /**
54      * Initialize global variables
55      *
56      * @param config Configuration
57      * @throws ServletException
58      */

59     public void init(ServletConfig config)
60         throws ServletException
61     {
62         super.init(config);
63         if (!WEnv.initWeb(config))
64             throw new ServletException("Click.init");
65     } // init
66

67     /**
68      * Get Servlet information
69      * @return Info
70      */

71     public String JavaDoc getServletInfo()
72     {
73         return "Compiere Click Servlet";
74     } // getServletInfo
75

76     /**
77      * Clean up resources
78      */

79     public void destroy()
80     {
81         log.debug("destroy");
82     } // destroy
83

84     /*************************************************************************/
85
86     /**
87      * Process the HTTP Get request.
88      *
89      * @param request request
90      * @param response response
91      * @throws ServletException
92      * @throws IOException
93      */

94     public void doGet(HttpServletRequest request, HttpServletResponse response)
95         throws ServletException, IOException
96     {
97         String JavaDoc url = getTargetURL(request);
98         response.sendRedirect(url);
99         response.flushBuffer();
100         // async start
101
log.debug("redirect - " + url);
102         m_requests.add(request);
103         new Thread JavaDoc(this).start();
104     } // doGet
105

106     /**
107      * Process the HTTP Post request
108      *
109      * @param request request
110      * @param response response
111      * @throws ServletException
112      * @throws IOException
113      */

114     public void doPost(HttpServletRequest request, HttpServletResponse response)
115         throws ServletException, IOException
116     {
117         doGet (request, response);
118     } // doPost
119

120     /**
121      * Get Target URL.
122      * 1 - target parameter
123      * 3 - parameter
124      * 2 - path
125      * @param request request
126      * @return URL
127      */

128     private String JavaDoc getTargetURL (HttpServletRequest request)
129     {
130         // Get Named Parameter - /click?target=www...
131
String JavaDoc url = request.getParameter(PARA_TARGET);
132         // Check parameters - /click?www...
133
if (url == null || url.length() == 0)
134         {
135             Enumeration e = request.getParameterNames ();
136             if (e.hasMoreElements ())
137                 url = (String JavaDoc)e.nextElement ();
138         }
139         // Check Path - /click/www...
140
if (url == null || url.length() == 0)
141         {
142             url = request.getPathInfo ();
143             if (url != null)
144                 url = url.substring(1); // cut off initial /
145
}
146         // Still nothing
147
if (url == null || url.length() == 0)
148             url = DEFAULT_TARGET;
149         // add http protocol
150
if (url.indexOf("://") == -1)
151             url = "http://" + url;
152         return url;
153     } // getTargetURL
154

155
156     /*************************************************************************/
157
158     /**
159      * Async Process
160      */

161     public void run()
162     {
163         long time = System.currentTimeMillis();
164         // get Request
165
HttpServletRequest request = null;
166         if (m_requests.size() > 0)
167             request = (HttpServletRequest)m_requests.remove(0);
168         if (request == null)
169         {
170             log.error("run - nothing in queue");
171             return;
172         }
173
174         //
175
Properties ctx = JSPEnv.getCtx(request);
176         String JavaDoc url = getTargetURL(request);
177         //
178
MClick mc = new MClick (ctx, url);
179         mc.setRemote_Addr(request.getRemoteAddr());
180         mc.setRemote_Host(request.getRemoteHost());
181         String JavaDoc ref = request.getHeader("referer");
182         if (ref == null || ref.length() == 0)
183             ref = request.getRequestURL().toString();
184         mc.setReferrer(ref);
185         //
186
mc.setAcceptLanguage(request.getHeader("accept-language"));
187         mc.setUserAgent(request.getHeader("user-agent"));
188         //
189
HttpSession session = request.getSession(false);
190         if (session != null)
191         {
192             WebUser wu = (WebUser)session.getAttribute (WebUser.NAME);
193             if (wu != null)
194             {
195                 mc.setEmail (wu.getEmail());
196                 mc.setAD_User_ID (wu.getAD_User_ID());
197             }
198         }
199         mc.save();
200         log.debug("run - end " + (System.currentTimeMillis()-time) + "ms");
201     } // run
202

203 } // Click
204
Popular Tags