KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > httpserver > NbBaseServlet


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.httpserver;
21
22 import java.io.IOException JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.net.InetAddress JavaDoc;
25 import java.util.Set JavaDoc;
26 import javax.servlet.*;
27 import javax.servlet.http.*;
28
29 import org.openide.util.NbBundle;
30 import org.openide.ErrorManager;
31 import org.openide.util.SharedClassObject;
32
33 /** Base servlet for servlets which access NetBeans Open APIs
34 *
35 * @author Petr Jiricka
36 * @version 0.11 May 5, 1999
37 */

38 public abstract class NbBaseServlet extends HttpServlet {
39
40     /** Initializes the servlet. */
41     public void init() throws ServletException {
42     }
43
44     /** Processes the request for both HTTP GET and POST methods
45     * @param request servlet request
46     * @param response servlet response
47     */

48     protected abstract void processRequest(HttpServletRequest request, HttpServletResponse response)
49     throws ServletException, java.io.IOException JavaDoc;
50
51     /** Performs the HTTP GET operation.
52     * @param request servlet request
53     * @param response servlet response
54     */

55     protected void doGet(HttpServletRequest request, HttpServletResponse response)
56     throws ServletException, java.io.IOException JavaDoc {
57         processRequest(request, response);
58     }
59
60     /** Performs the HTTP POST operation.
61     * @param request servlet request
62     * @param response servlet response
63     */

64     protected void doPost(HttpServletRequest request, HttpServletResponse response)
65     throws ServletException, java.io.IOException JavaDoc {
66         processRequest(request, response);
67     }
68
69     /**
70     * Returns a short description of the servlet.
71     */

72     public String JavaDoc getServletInfo() {
73         return NbBundle.getBundle(NbBaseServlet.class).getString("MSG_BaseServletDescr");
74     }
75
76     /** Checks whether access should be permitted according to HTTP Server module access settings
77     * (localhost/anyhost, granted addesses)
78     * @return true if access is granted
79     */

80     protected boolean checkAccess(HttpServletRequest request) throws IOException JavaDoc {
81
82         HttpServerSettings settings = HttpServerSettings.getDefault();
83         if (settings == null)
84             return false;
85
86         if (settings.getHostProperty ().getHost ().equals(HttpServerSettings.ANYHOST))
87             return true;
88
89         Set JavaDoc hs = settings.getGrantedAddressesSet();
90
91         if (hs.contains(request.getRemoteAddr().trim()))
92             return true;
93
94         String JavaDoc pathI = request.getPathInfo();
95         if (pathI == null)
96             pathI = ""; // NOI18N
97
// ask user
98
try {
99             String JavaDoc address = request.getRemoteAddr().trim();
100             if (settings.allowAccess(InetAddress.getByName(address), pathI)) return true;
101         } catch (Exception JavaDoc ex) {
102             ErrorManager.getDefault().notify(ex);
103             return false;
104         }
105
106         return false;
107     }
108
109 }
110
Popular Tags