forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServletUtil.java
More file actions
174 lines (152 loc) · 6.68 KB
/
Copy pathServletUtil.java
File metadata and controls
174 lines (152 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*-*- mode: Java; tab-width:8 -*-*/
package php.java.servlet;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletResponse;
import php.java.bridge.Util;
import php.java.bridge.http.WriterOutputStream;
/*
* Copyright (C) 2003-2007 Jost Boekemeier
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* Miscellaneous servlet functions.
* @author jostb
*
*/
public class ServletUtil {
private ServletUtil() {}
/**
* Identical to context2.getRealPath(pathInfoCGI). On BEA
* WebLogic, which has a broken getRealPath() implementation, we
* use context2.getResource(pathInfoCGI)) instead.
* @param context2 The servlet context.
* @param pathInfoCGI may be "" or "/" for example.
* @return a valid path or null
*/
public static String getRealPath(ServletContext context2, String pathInfoCGI) {
String ret = context2.getRealPath(pathInfoCGI);
if(ret!=null) return ret;
// The following is the workaround for BEA WebLogic
if(!pathInfoCGI.startsWith("/")) {
pathInfoCGI = "/"+pathInfoCGI;
}
URL url = null;
try {
url = context2.getResource(pathInfoCGI);
} catch (MalformedURLException e) {
Util.printStackTrace(e);
}
if(url != null && !"file".equals(url.getProtocol())) url = null;
if (url == null) throw new IllegalStateException("Cannot access "+pathInfoCGI+" within the current web application. Please explode it: Unzip the application .war file into a directory and deploy the directory instead.");
ret = url.getPath();
return ret.replace('/', File.separatorChar);
}
/**
* Only for internal use.
*
* Returns the port# of the local port
* @param req The servlet request
* @return The local port or the value from the server port variable.
*/
public static int getLocalPort(ServletRequest req) {
int port = -1;
try {
port = req.getLocalPort();
} catch (Throwable t) {/*ignore*/}
if(port<=0) port = req.getServerPort();
return port;
}
public static boolean isJavaBridgeWc(String contextPath) {
return (contextPath!=null && contextPath.endsWith("JavaBridge"));
}
public static String nullsToBlanks(String s) {
return ServletUtil.nullsToString(s, "");
}
public static String nullsToString(String couldBeNull,
String subForNulls) {
return (couldBeNull == null ? subForNulls : couldBeNull);
}
public static String getHeaders (StringBuffer buf, Enumeration enumeration) {
while (enumeration.hasMoreElements()){
buf.append (enumeration.nextElement());
if (enumeration.hasMoreElements())
buf.append ("; ");
}
String result = buf.toString();
buf.setLength(0);
return result;
}
public static OutputStream getServletOutputStream(HttpServletResponse response) throws IOException {
try {
return response.getOutputStream();
} catch (IllegalStateException e) {
WriterOutputStream out = new WriterOutputStream(response.getWriter ());
out.setEncoding(response.getCharacterEncoding());
return out;
}
}
/**
* Return an mbean property.
* Example: <code>Util.getMBeanProperty("*:type=ThreadPool,name=http*", "maxThreads")</code> or
* <code>Util.getMBeanProperty("*:ServiceModule=*,J2EEServer=*,name=JettyWebConnector,j2eeType=*", "maxThreads");</code>
* @param pattern the pattern string
* @param property the property key
* @return the property value
*/
public static int getMBeanProperty(String pattern, String property) {
try {
Class objectNameClazz = Class.forName("javax.management.ObjectName");
Constructor constructor = objectNameClazz.getConstructor(new Class[]{String.class});
Object objectName = constructor.newInstance(new Object[]{pattern});
Class clazz = Class.forName("javax.management.MBeanServerFactory");
Method method = clazz.getMethod("findMBeanServer", new Class[]{String.class});
ArrayList servers = (ArrayList)method.invoke(clazz, new Object[]{null});
Object server = servers.get(0);
Class mBeanServerClazz = Class.forName("javax.management.MBeanServer");
clazz = Class.forName("javax.management.QueryExp");
method = mBeanServerClazz.getMethod("queryMBeans", new Class[]{objectNameClazz, clazz});
Set s = (Set)method.invoke(server, new Object[]{objectName, null});
Iterator ii = s.iterator();
if (ii.hasNext()) {
clazz = Class.forName("javax.management.ObjectInstance");
method = clazz.getMethod("getObjectName", Util.ZERO_PARAM);
objectName = method.invoke(ii.next(), Util.ZERO_ARG);
method = mBeanServerClazz.getMethod("getAttribute", new Class[]{objectNameClazz, String.class});
Object result = method.invoke(server, new Object[]{objectName, property});
return Integer.parseInt(String.valueOf(result));
}
} catch (Exception t) {
if (Util.logLevel>5) Util.printStackTrace(t);
}
return 0;
}
}