KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > admingui > servlet > HandleQuickStartDocs


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.admingui.servlet;
25
26 import java.io.InputStreamReader JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.net.URL JavaDoc;
32
33 import javax.servlet.*;
34 import javax.servlet.http.*;
35 import javax.management.ObjectName JavaDoc;
36
37 import com.sun.enterprise.tools.admingui.util.Util;
38 import com.sun.enterprise.tools.admingui.util.MBeanUtil;
39 import com.sun.enterprise.tools.guiframework.exception.FrameworkException;
40
41 public class HandleQuickStartDocs extends HttpServlet {
42
43     public void init(ServletConfig config) throws ServletException {
44     super.init(config);
45     }
46
47     public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException JavaDoc {
48     doPost(req, res);
49     }
50
51
52     public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException JavaDoc {
53     String JavaDoc pathInfo = req.getPathInfo();
54     
55     if(pathInfo == null || pathInfo.length() == 0) {
56         //default.
57
pathInfo = "/QuickStart.html";
58     }
59         File JavaDoc fileIn = null;
60         FileInputStream JavaDoc streamIn = null;
61         
62         try {
63             //get requested file
64
fileIn = new File JavaDoc(getInstallDir()+pathInfo);
65             
66             if(!fileIn.exists()) {
67                 //falling back to english by default
68
fileIn = new File JavaDoc(getInstallDir()+"/QuickStart.html");
69             }
70             
71             //open input stream
72
if(fileIn.canRead())
73                 streamIn = new FileInputStream JavaDoc(fileIn);
74             else {
75                 res.sendError(400, req.getRequestURI());
76                 return;
77             
78             }
79             
80         } catch (Exception JavaDoc e)
81         {
82             res.sendError(400, req.getRequestURI());
83             return;
84         }
85         
86         //create byte array
87
byte[] data = new byte[(int)fileIn.length()];
88         if(data != null && (int)fileIn.length() > 0)
89         {
90             streamIn.read(data);
91             streamIn.close();
92         }
93         
94         if(req.getRequestURI().indexOf(".gif") > 0 || req.getRequestURI().indexOf(".jpg") > 0 || req.getRequestURI().indexOf(".jpeg") > 0 ) {
95             //get output stream
96
ServletOutputStream imageOut = res.getOutputStream();
97             
98             if(req.getRequestURI().indexOf(".gif") > 0)
99                 res.setContentType("image/gif");
100             else
101                 res.setContentType("image/jpeg");
102             //write binary data to output stream
103
imageOut.write(data);
104             imageOut.close();
105         } else {
106            if(req.getRequestURI().indexOf(".css") > 0) {
107                 res.setContentType("text/css");
108             } else {
109                res.setContentType("text/html");
110                  
111             }
112                 
113             //get handle to the output stream
114
ServletOutputStream textOut = res.getOutputStream();
115             textOut.write(data);
116             textOut.close();
117         }
118     }
119     
120     protected String JavaDoc getInstallDir() throws Exception JavaDoc {
121         String JavaDoc dir = "";
122         try {
123             Object JavaDoc[] params = new Object JavaDoc[]{"${com.sun.aas.installRoot}", "server", new Boolean JavaDoc("true")};
124             String JavaDoc[] types = new String JavaDoc[]{"java.lang.String", "java.lang.String", "boolean"};
125             Object JavaDoc installDir = MBeanUtil.invoke(
126             new ObjectName JavaDoc("com.sun.appserv:type=domain,category=config"), "resolveTokens", params, types);
127             if(dir != null)
128                 dir = installDir.toString();
129         } catch (Exception JavaDoc ex) {
130             throw ex;
131         }
132         return getDocsDir(dir);
133     }
134     
135     protected String JavaDoc getDocsDir(String JavaDoc dir){
136         return dir+"/docs";
137     }
138     
139 }
140
Popular Tags