001: /*
002: * (C) Copyright 2000 - 2005 Nabh Information Systems, Inc.
003: *
004: * This program is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU General Public License
006: * as published by the Free Software Foundation; either version 2
007: * of the License, or (at your option) any later version.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019: package com.nabhinc.ws.server;
020:
021: import java.io.File;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.Writer;
025: import java.net.MalformedURLException;
026: import java.net.URL;
027: import java.util.Enumeration;
028:
029: import javax.servlet.ServletConfig;
030: import javax.servlet.ServletContext;
031:
032: import org.w3c.dom.Element;
033:
034: import com.nabhinc.util.StringUtil;
035: import com.nabhinc.util.XMLUtil;
036: import com.nabhinc.ws.spi.UserManager;
037:
038: /**
039: * Default implementation of <code>WebServiceContext</code> interface.
040: *
041: * @author Padmanabh Dabke
042: * (c) 2005 Nabh Information Systems, Inc. All Rights Reserved.
043: */
044: public class HTTPWebServiceContext implements WebServiceContext {
045: private String wscRootDir = null;
046: private String wscUnsecureURL = null;
047: private String wscSecureURL = null;
048: private String wscServerID = null;
049: private String wscDefaultOwner = null;
050: private UserManager wscUserManager = null;
051: private ServletConfig wscServletConfig = null;
052: private ServletContext wscServletContext = null;
053:
054: public HTTPWebServiceContext(String rootDir) {
055: wscRootDir = rootDir;
056: }
057:
058: /**
059: * Initializes this context based on XML configuration. It looks for
060: * the following XML elements to populate it's state:
061: * <ul>
062: * <li>secure-server-url - Secure URL for this server.</li>
063: * <li>unsecure-server-url - Unsercure URL for this server.</li>
064: * <li>server-id - Server ID (used in clustered environments)</li>
065: * <li>default-owner - User represented by this service when it makes
066: * calls on other services.</li>
067: * </ul>
068: * @param config XML configuration
069: * @param sConfig Servlet configuration
070: */
071: public void init(Element config, ServletConfig sConfig) {
072: wscServletConfig = sConfig;
073: wscServletContext = sConfig.getServletContext();
074: wscSecureURL = XMLUtil.getSubElementText(config,
075: "secure-server-url");
076: wscUnsecureURL = XMLUtil.getSubElementText(config,
077: "unsecure-server-url");
078: wscServerID = XMLUtil.getSubElementText(config, "server-id");
079: wscDefaultOwner = XMLUtil.getSubElementText(config,
080: "default-owner");
081:
082: if (wscServerID == null)
083: wscServerID = "0";
084: if (wscDefaultOwner == null)
085: wscDefaultOwner = "admin";
086: }
087:
088: protected void setUserManager(UserManager uMan) {
089: wscUserManager = uMan;
090: }
091:
092: public void serialize(String indent, String delta, Writer w)
093: throws IOException {
094: XMLUtil.writeElementStart(indent,
095: WebServiceServerConstants.SERVER_CONTEXT_TAG, w);
096: String indent1 = indent + delta;
097: XMLUtil.writeElement(indent1, "secure-server-url",
098: wscSecureURL, w);
099: XMLUtil.writeElement(indent1, "unsecure-server-url",
100: wscUnsecureURL, w);
101: XMLUtil.writeElement(indent1, "server-id", wscServerID, w);
102: XMLUtil.writeElement(indent1, "default-owner", wscDefaultOwner,
103: w);
104: XMLUtil.writeElementEnd(indent,
105: WebServiceServerConstants.SERVER_CONTEXT_TAG, w);
106: }
107:
108: /**
109: * Returns ID of the server this service is running on. Useful in a
110: * clustered environment.
111: */
112: public String getServerID() {
113: return wscServerID;
114: }
115:
116: /**
117: * Returns secure (SSL) URL for the Web server. Useful in generating URLs
118: * that point to services or portlets on this server.
119: */
120: public String getSecureURL() {
121: return wscSecureURL;
122: }
123:
124: /**
125: * Returns unsecure URL for the Web server. Useful in generating URLs
126: * that point to services or portlets on this server.
127: */
128: public String getUnsecureURL() {
129: return wscUnsecureURL;
130: }
131:
132: /**
133: * Delegates to the same method on servlet context.
134: */
135: public String getRealPath(String path) {
136: if (wscRootDir == null)
137: return wscServletContext.getRealPath(path);
138: String realPath = null;
139: if (path.startsWith("/")) {
140: realPath = wscRootDir + path;
141: } else {
142: realPath = wscRootDir + File.separator + path;
143: }
144: realPath = StringUtil.replacePathSeparator(realPath);
145: return realPath;
146: }
147:
148: /**
149: * Delegates to the same method on servlet context.
150: */
151: public URL getResource(String key) throws MalformedURLException {
152: return wscServletContext.getResource(key);
153: }
154:
155: /**
156: * Delegates to the same method on servlet context.
157: */
158: public InputStream getResourceAsStream(String path) {
159: return wscServletContext.getResourceAsStream(path);
160: }
161:
162: public UserManager getUserManager() {
163: return wscUserManager;
164: }
165:
166: public ServletConfig getServletConfig() {
167: return wscServletConfig;
168: }
169:
170: /* (non-Javadoc)
171: * @see com.nabhinc.ws.server.WebServiceContext#getAttribute(java.lang.String)
172: */
173: public Object getAttribute(String name) {
174: return wscServletContext.getAttribute(name);
175: }
176:
177: /* (non-Javadoc)
178: * @see com.nabhinc.ws.server.WebServiceContext#getAttributeNames()
179: */
180: public Enumeration getAttributeNames() {
181: return wscServletContext.getAttributeNames();
182: }
183:
184: /* (non-Javadoc)
185: * @see com.nabhinc.ws.server.WebServiceContext#setAttribute(java.lang.String, java.lang.Object)
186: */
187: public void setAttribute(String name, Object value) {
188: wscServletContext.setAttribute(name, value);
189: }
190:
191: /* (non-Javadoc)
192: * @see com.nabhinc.ws.server.WebServiceContext#removeAttribute(java.lang.String)
193: */
194: public void removeAttribute(String name) {
195: wscServletContext.removeAttribute(name);
196: }
197: }
|