001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.transport.http; 018 019import java.net.URI; 020 021import org.apache.activemq.transport.TransportThreadSupport; 022import org.apache.activemq.transport.util.TextWireFormat; 023 024/** 025 * A useful base class for HTTP Transport implementations. 026 * 027 * 028 */ 029public abstract class HttpTransportSupport extends TransportThreadSupport { 030 private static final int DEFAULT_PROXY_PORT = 8080; 031 private static final String PROPERTY_PROXY_HOST = "proxyHost"; 032 private static final String PROPERTY_PROXY_PORT = "proxyPort"; 033 private static final String PROPERTY_PROXY_USER = "proxyUser"; 034 private static final String PROPERTY_PROXY_PASSWORD = "proxyPassword"; 035 036 private TextWireFormat textWireFormat; 037 private URI remoteUrl; 038 private String proxyHost; 039 private Integer proxyPort; 040 private String proxyUser; 041 private String proxyPassword; 042 043 public HttpTransportSupport(TextWireFormat textWireFormat, URI remoteUrl) { 044 this.textWireFormat = textWireFormat; 045 this.remoteUrl = remoteUrl; 046 } 047 048 public String toString() { 049 return "HTTP Reader " + getRemoteUrl(); 050 } 051 052 // Properties 053 // ------------------------------------------------------------------------- 054 public String getRemoteAddress() { 055 return remoteUrl.toString(); 056 } 057 058 public URI getRemoteUrl() { 059 return remoteUrl; 060 } 061 062 public TextWireFormat getTextWireFormat() { 063 return textWireFormat; 064 } 065 066 public void setTextWireFormat(TextWireFormat textWireFormat) { 067 this.textWireFormat = textWireFormat; 068 } 069 070 public String getProxyHost() { 071 return proxyHost != null ? proxyHost : getSystemProperty(PROPERTY_PROXY_HOST); 072 } 073 074 public void setProxyHost(String proxyHost) { 075 this.proxyHost = proxyHost; 076 } 077 078 public int getProxyPort() { 079 return proxyPort != null ? proxyPort 080 : (getSystemProperty(PROPERTY_PROXY_PORT) != null 081 ? Integer.parseInt(getSystemProperty(PROPERTY_PROXY_PORT)) : DEFAULT_PROXY_PORT); 082 } 083 084 public void setProxyPort(int proxyPort) { 085 this.proxyPort = proxyPort; 086 } 087 088 public String getProxyUser() { 089 return proxyUser != null ? proxyUser : getSystemProperty(PROPERTY_PROXY_USER); 090 } 091 092 public void setProxyUser(String proxyUser) { 093 this.proxyUser = proxyUser; 094 } 095 096 public String getProxyPassword() { 097 return proxyPassword != null ? proxyPassword : getSystemProperty(PROPERTY_PROXY_PASSWORD); 098 } 099 100 public void setProxyPassword(String proxyPassword) { 101 this.proxyPassword = proxyPassword; 102 } 103 104 protected abstract String getSystemPropertyPrefix(); 105 106 private String getSystemProperty(String propertyName) { 107 return System.getProperty(getSystemPropertyPrefix() + propertyName); 108 } 109 110}