001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.transport.http.server;
020:
021: import org.apache.axis2.transport.http.HTTPConstants;
022: import org.apache.axis2.transport.TransportListener;
023: import org.apache.axis2.engine.AxisConfiguration;
024: import org.apache.axis2.description.Parameter;
025: import org.apache.http.Header;
026:
027: import java.net.InetAddress;
028: import java.net.NetworkInterface;
029: import java.net.SocketException;
030: import java.util.Enumeration;
031:
032: public class HttpUtils {
033:
034: private HttpUtils() {
035: }
036:
037: public static String getSoapAction(final AxisHttpRequest request) {
038: if (request == null) {
039: return null;
040: }
041: Header header = request
042: .getFirstHeader(HTTPConstants.HEADER_SOAP_ACTION);
043: if (header != null) {
044: return header.getValue();
045: } else {
046: return null;
047: }
048: }
049:
050: /**
051: * Returns the ip address to be used for the replyto epr
052: * CAUTION:
053: * This will go through all the available network interfaces and will try to return an ip address.
054: * First this will try to get the first IP which is not loopback address (127.0.0.1). If none is found
055: * then this will return this will return 127.0.0.1.
056: * This will <b>not<b> consider IPv6 addresses.
057: * <p/>
058: * TODO:
059: * - Improve this logic to genaralize it a bit more
060: * - Obtain the ip to be used here from the Call API
061: *
062: * @return Returns String.
063: * @throws SocketException
064: */
065: public static String getIpAddress() throws SocketException {
066: Enumeration e = NetworkInterface.getNetworkInterfaces();
067: String address = "127.0.0.1";
068:
069: while (e.hasMoreElements()) {
070: NetworkInterface netface = (NetworkInterface) e
071: .nextElement();
072: Enumeration addresses = netface.getInetAddresses();
073:
074: while (addresses.hasMoreElements()) {
075: InetAddress ip = (InetAddress) addresses.nextElement();
076: if (!ip.isLoopbackAddress()
077: && isIP(ip.getHostAddress())) {
078: return ip.getHostAddress();
079: }
080: }
081: }
082:
083: return address;
084: }
085:
086: /**
087: * First check whether the hostname parameter is there in AxisConfiguration (axis2.xml) ,
088: * if it is there then this will retun that as the host name , o.w will return the IP address.
089: */
090: public static String getIpAddress(
091: AxisConfiguration axisConfiguration) throws SocketException {
092: if (axisConfiguration != null) {
093: Parameter param = axisConfiguration
094: .getParameter(TransportListener.HOST_ADDRESS);
095: if (param != null) {
096: String hostAddress = ((String) param.getValue()).trim();
097: if (hostAddress != null) {
098: return hostAddress;
099: }
100: }
101: }
102: return getIpAddress();
103: }
104:
105: private static boolean isIP(String hostAddress) {
106: return hostAddress.split("[.]").length == 4;
107: }
108: }
|