001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2007 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: HttpServerConfig.java 8843 2007-08-30 15:09:27Z mpreston $
023: */
024: package com.bostechcorp.cbesb.runtime.component.http.server;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028:
029: import com.bostechcorp.cbesb.runtime.component.http.HttpEndpoint;
030:
031: public class HttpServerConfig {
032:
033: protected final transient Log logger = LogFactory
034: .getLog(getClass());
035:
036: private boolean ssl;
037: private String host;
038: private int port;
039:
040: private String sslProtocol;
041: private String keyStoreFile;
042: private String keyStorePassword;
043: private String trustStoreFile;
044: private String trustStorePassword;
045: private boolean authenticateClient;
046:
047: public HttpServerConfig() {
048:
049: }
050:
051: public HttpServerConfig(String url) {
052: initFromURL(url);
053: }
054:
055: public HttpServerConfig(HttpEndpoint endpoint) {
056: initFromURL(endpoint.getLocationURI());
057: if (isSsl()) {
058: if (endpoint.getSslProtocol() != null) {
059: sslProtocol = endpoint.getSslProtocol();
060: } else {
061: sslProtocol = "TLS";
062: }
063: keyStoreFile = endpoint.getKeyStoreFile();
064: keyStorePassword = endpoint.getKeyStorePassword();
065: if (!endpoint.isUseDefaultTrustStore()) {
066: trustStoreFile = endpoint.getTrustStoreFile();
067: trustStorePassword = endpoint.getTrustStorePassword();
068: }
069: authenticateClient = endpoint.isAuthenticateClient();
070: }
071: }
072:
073: /**
074: * @return the host
075: */
076: public String getHost() {
077: return host;
078: }
079:
080: /**
081: * @param host the host to set
082: */
083: public void setHost(String host) {
084: this .host = host;
085: }
086:
087: /**
088: * @return the port
089: */
090: public int getPort() {
091: return port;
092: }
093:
094: /**
095: * @param port the port to set
096: */
097: public void setPort(int port) {
098: this .port = port;
099: }
100:
101: /**
102: * @return the ssl
103: */
104: public boolean isSsl() {
105: return ssl;
106: }
107:
108: /**
109: * @param ssl the ssl to set
110: */
111: public void setSsl(boolean ssl) {
112: this .ssl = ssl;
113: }
114:
115: /**
116: * @return the authenticateClient
117: */
118: public boolean isAuthenticateClient() {
119: return authenticateClient;
120: }
121:
122: /**
123: * @param authenticateClient the authenticateClient to set
124: */
125: public void setAuthenticateClient(boolean authenticateClient) {
126: this .authenticateClient = authenticateClient;
127: }
128:
129: /**
130: * @return the keyStoreFile
131: */
132: public String getKeyStoreFile() {
133: return keyStoreFile;
134: }
135:
136: /**
137: * @param keyStoreFile the keyStoreFile to set
138: */
139: public void setKeyStoreFile(String keyStoreFile) {
140: this .keyStoreFile = keyStoreFile;
141: }
142:
143: /**
144: * @return the keyStorePassword
145: */
146: public String getKeyStorePassword() {
147: return keyStorePassword;
148: }
149:
150: /**
151: * @param keyStorePassword the keyStorePassword to set
152: */
153: public void setKeyStorePassword(String keyStorePassword) {
154: this .keyStorePassword = keyStorePassword;
155: }
156:
157: /**
158: * @return the sslProtocol
159: */
160: public String getSslProtocol() {
161: return sslProtocol;
162: }
163:
164: /**
165: * @param sslProtocol the sslProtocol to set
166: */
167: public void setSslProtocol(String sslProtocol) {
168: this .sslProtocol = sslProtocol;
169: }
170:
171: /**
172: * @return the trustStoreFile
173: */
174: public String getTrustStoreFile() {
175: return trustStoreFile;
176: }
177:
178: /**
179: * @param trustStoreFile the trustStoreFile to set
180: */
181: public void setTrustStoreFile(String trustStoreFile) {
182: this .trustStoreFile = trustStoreFile;
183: }
184:
185: /**
186: * @return the trustStorePassword
187: */
188: public String getTrustStorePassword() {
189: return trustStorePassword;
190: }
191:
192: /**
193: * @param trustStorePassword the trustStorePassword to set
194: */
195: public void setTrustStorePassword(String trustStorePassword) {
196: this .trustStorePassword = trustStorePassword;
197: }
198:
199: public String getURL() {
200: StringBuffer buf = new StringBuffer();
201: if (isSsl()) {
202: buf.append("https://");
203: } else {
204: buf.append("http://");
205: }
206: buf.append(getHost());
207: if (getPort() > 0 && getPort() != 80 && getPort() != 443) {
208: buf.append(":" + Integer.toString(getPort()));
209: }
210: return buf.toString();
211: }
212:
213: private void initFromURL(String url) {
214: int index = 0;
215: if (url.startsWith("http://")) {
216: setSsl(false);
217: index = "http://".length();
218: } else if (url.startsWith("https://")) {
219: setSsl(true);
220: index = "https://".length();
221: } else {
222: logger
223: .error("URL must begin with 'http://' or 'https://' - "
224: + url);
225: return;
226: }
227: int index2 = url.indexOf('/', index);
228: String host;
229: if (index2 == -1) {
230: host = url.substring(index);
231:
232: } else {
233: host = url.substring(index, index2);
234: }
235:
236: String[] tmpArray = host.split(":");
237: if (tmpArray.length > 0) {
238: setHost(tmpArray[0]);
239: if (tmpArray.length > 1) {
240: setPort(Integer.parseInt(tmpArray[1]));
241: } else {
242: //no port specified, use defaults for protocol
243: if (isSsl()) {
244: port = 443;
245: } else {
246: port = 80;
247: }
248: }
249: }
250: }
251:
252: public String toString() {
253: return host + ":" + port + " SSL=" + ssl;
254: }
255:
256: public boolean equals(HttpServerConfig config) {
257: return (this.host.equals(config.getHost())
258: && this.port == config.getPort() && this.ssl == config
259: .isSsl());
260: }
261: }
|