001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
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: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.util;
021:
022: import javax.servlet.http.HttpServletRequest;
023:
024: import com.salmonllc.properties.Props;
025:
026: /**
027: * This class has methods which can be used to determin the URL of the server that the application is running on
028: */
029:
030: public class URLGenerator {
031: /**
032: * This method will return the url of the the object store from the system.properties file
033: */
034: public static String generateObjectstoreURL(HttpServletRequest req) {
035: return generateObjectstoreURL(req, null);
036: }
037:
038: /**
039: * This method will return the url of the the object store for a particular application
040: */
041: public static String generateObjectstoreURL(HttpServletRequest req,
042: String appName) {
043: Props p = null;
044: if (appName == null)
045: p = Props.getSystemProps();
046: else
047: p = Props.getProps(appName, null);
048: String url = p.getProperty(Props.SYS_OBJECTSTORE_URL);
049: if (url != null) {
050: if (url.startsWith("http"))
051: return url;
052: else {
053: StringBuffer buf = new StringBuffer(
054: generateServerURL(req));
055: buf.append(url);
056: return buf.toString();
057: }
058: } else {
059: StringBuffer buf = new StringBuffer();
060: buf.append(generateServerURL(req));
061: buf.append("/");
062: buf.append(generateServletBaseURL(req));
063: buf.append("/Objectstore");
064: return buf.toString();
065: }
066: }
067:
068: /**
069: * This method will return the url of the server running the servlet
070: */
071: public static String generateServerURL(HttpServletRequest req) {
072: Props p = Props.getSystemProps();
073: StringBuffer buf = new StringBuffer();
074: buf.append(p.getProperty(Props.SYS_UNSECURE_SCHEME, req
075: .getScheme()));
076: buf.append("://");
077: buf.append(req.getServerName());
078: int port = p.getIntProperty(Props.SYS_UNSECURE_PORT);
079: if (port == -1)
080: port = req.getServerPort();
081: if (port > 0 && port != 80 && port != 443) {
082: buf.append(":");
083: buf.append(new Integer(port).toString());
084: }
085: return buf.toString();
086: }
087:
088: /**
089: * This method will return the url of the server running the servlet as a local url
090: */
091: public static String generateLocalHostServerURL(
092: HttpServletRequest req) {
093: Props p = Props.getSystemProps();
094: StringBuffer buf = new StringBuffer();
095: buf.append(p.getProperty(Props.SYS_UNSECURE_SCHEME, req
096: .getScheme()));
097: buf.append("://");
098: buf.append(p.getProperty(Props.SYS_LOCALHOST, "127.0.0.1"));
099: int port = p.getIntProperty(Props.SYS_UNSECURE_PORT);
100: if (port == -1)
101: port = req.getServerPort();
102: if (port > 0 && port != 80 && port != 443) {
103: buf.append(":");
104: buf.append(new Integer(port).toString());
105: }
106: return buf.toString();
107: }
108:
109: /**
110: * This method will return the url of the server running the servlet as a local url
111: */
112: public static String generateLocalHostServerURL() {
113: Props p = Props.getSystemProps();
114: StringBuffer buf = new StringBuffer();
115: buf.append(p.getProperty(Props.SYS_UNSECURE_SCHEME, "http"));
116: buf.append("://");
117: buf.append(p.getProperty(Props.SYS_LOCALHOST, "127.0.0.1"));
118: int port = p.getIntProperty(Props.SYS_UNSECURE_PORT);
119: if (port == -1)
120: port = 80;
121: if (port > 0 && port != 80 && port != 443) {
122: buf.append(":");
123: buf.append(new Integer(port).toString());
124: }
125: return buf.toString();
126: }
127:
128: /**
129: * This method will return base url for every servlet in the web application ex:servlet in http://host/servlet/AppServer
130: */
131: public static String generateServletBaseURL(HttpServletRequest req) {
132: String servletPath = req.getRequestURI();
133: String servletName = req.getServletPath();
134: if (servletName != null) {
135: int pos = servletName.lastIndexOf("/");
136: if (pos != -1)
137: servletName = servletName.substring(pos);
138: } else
139: servletName = "";
140:
141: int pos = -1;
142: if (servletPath != null)
143: pos = servletPath.indexOf(servletName);
144: if (pos < 1)
145: servletPath = "";
146: else
147: servletPath = servletPath.substring(1, pos);
148: return servletPath;
149:
150: }
151:
152: /**
153: * This method will return base url for every servlet in the web application ex:servlet in http://host/servlet/AppServer
154: */
155: public static String generateServletURL(HttpServletRequest req) {
156: String servletPath = req.getRequestURI();
157: String servletName = req.getServletPath();
158: int pos = servletName.lastIndexOf("/");
159: if (pos != -1)
160: servletName = servletName.substring(pos);
161:
162: pos = servletPath.indexOf(servletName);
163: if (pos <= 0)
164: servletPath = "";
165: else
166: servletPath = servletPath.substring(1, pos);
167:
168: return servletPath + servletName;
169:
170: }
171:
172: /**
173: * This method will return a secure url of the server running the servlet
174: */
175: public static String generateSecureServerURL(HttpServletRequest req) {
176: Props p = Props.getSystemProps();
177: StringBuffer buf = new StringBuffer();
178: buf.append(p.getProperty(Props.SYS_SECURE_SCHEME, "https"));
179: buf.append("://");
180: buf.append(req.getServerName());
181: int port = p.getIntProperty(Props.SYS_SECURE_PORT);
182: if (port > 0 && port != 80 && port != 443) {
183: buf.append(":");
184: buf.append(new Integer(port).toString());
185: }
186: return buf.toString();
187: }
188: }
|