KickJava   Java API By Example, From Geeks To Geeks.

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


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.InputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.InetAddress JavaDoc;
25 import java.net.UnknownHostException JavaDoc;
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.URLConnection JavaDoc;
29 import javax.servlet.ServletException JavaDoc;
30 import javax.servlet.http.*;
31
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.URLMapper;
34 import org.openide.util.NbBundle;
35 import org.openide.util.SharedClassObject;
36
37 import javax.servlet.ServletOutputStream JavaDoc;
38
39 /**
40  *
41  * @author Radim Kubacki
42  */

43 public class WrapperServlet extends NbBaseServlet {
44
45     private static final long serialVersionUID = 8009602136746998361L;
46     
47     /** Creates new WrapperServlet */
48     public WrapperServlet () {
49     }
50     
51     /** Processes the request for both HTTP GET and POST methods
52      * @param request servlet request
53      * @param response servlet response
54      */

55     protected void processRequest (HttpServletRequest request, HttpServletResponse response)
56     throws ServletException JavaDoc, java.io.IOException JavaDoc {
57         if (!checkAccess(request)) {
58             response.sendError(HttpServletResponse.SC_FORBIDDEN,
59                                NbBundle.getMessage(WrapperServlet.class, "MSG_HTTP_FORBIDDEN"));
60             return;
61         }
62         // output your page here
63
//String path = request.getPathInfo ();
64
ServletOutputStream JavaDoc out = response.getOutputStream ();
65         try {
66             String JavaDoc requestURL = getRequestURL(request);
67             //String requestURL = request.getRequestURL().toString(); this method is only in Servlet API 2.3
68
URLMapper serverMapper = new HttpServerURLMapper();
69             FileObject files[] = serverMapper.getFileObjects(new URL JavaDoc(requestURL));
70             if ((files == null) || (files.length != 1)) {
71                 throw new IOException JavaDoc();
72             }
73             URL JavaDoc internal = URLMapper.findURL(files[0], URLMapper.INTERNAL);
74             URLConnection JavaDoc conn = internal.openConnection();
75             
76             response.setContentType(conn.getContentType ()); // NOI18N
77
// PENDING: copy all info - headers, length, encoding, ...
78

79             InputStream JavaDoc in = conn.getInputStream ();
80             byte [] buff = new byte [256];
81             int len;
82
83             while ((len = in.read (buff)) != -1) {
84                 out.write (buff, 0, len);
85                 out.flush();
86             }
87             in.close ();
88
89         }
90         catch (MalformedURLException JavaDoc ex) {
91             try {
92                 response.sendError (HttpServletResponse.SC_NOT_FOUND,
93                                    NbBundle.getMessage(WrapperServlet.class, "MSG_HTTP_NOT_FOUND"));
94             }
95             catch (IOException JavaDoc ex2) {}
96         }
97         catch (IOException JavaDoc ex) {
98             try {
99                 response.sendError (HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
100             }
101             catch (IOException JavaDoc ex2) {}
102         }
103         finally {
104             try { out.close(); } catch (Exception JavaDoc ex) {}
105         }
106     }
107
108     private String JavaDoc getRequestURL(HttpServletRequest request) throws UnknownHostException JavaDoc, MalformedURLException JavaDoc {
109         HttpServerSettings settings = HttpServerSettings.getDefault();
110
111         String JavaDoc pi = request.getPathInfo();
112         if (pi.startsWith("/")) { // NOI18N
113
pi = pi.substring(1);
114         }
115         URL JavaDoc reconstructedURL = new URL JavaDoc ("http", // NOI18N
116
InetAddress.getLocalHost ().getHostName (),
117                               settings.getPort (),
118                               settings.getWrapperBaseURL () + pi.toString());
119         return reconstructedURL.toExternalForm();
120     }
121
122     /**
123     * Returns a short description of the servlet.
124     */

125     public String JavaDoc getServletInfo() {
126         return NbBundle.getMessage(WrapperServlet.class, "MSG_WrapperServletDescr");
127     }
128
129 }
130
Popular Tags