Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH . All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html Contributors: s IT Solutions AT Spardat GmbH - initial API and implementation /
 
 
 /*
  * Created on 16.05.2003
  */
 package at.spardat.xma.boot.transport;
 
 import java.net.URL;
 import java.util.List;
 
custom URI class for xma usage. This abstraction of URI brings the capability to use the same URI even for different implementations of Transport. At this time only a mapping to HTTP-URLs is implemented but additional protocols may be added in the future.

The URIs used by XMA contain of the following parts:
<protocol>://<server>[_<environment>][_<installation>][:<port>]/<application>[/<component>][/<resource>][&<parameters>]
  • protocol the protocol to use. currently only http is supported.
  • server the DNS-name of the server hosting the xma application to reference.
  • optional envrionment the envrionment specific part of the DNS-name of the server (not implemented yet)
  • optional installation the part of the DNS-name specific for the organisation operating the server in a given location (not implemented yet)
  • optional port the port used by the protocol running on the given server. Can be omitted if the standard port of the protocol is used.
  • application the name of the xma-application. Must not contain any "/".
  • optional component the name of the xma-component inside the xma-application. Must not contain any "/".
  • optional resource relative path of a resource inside a xma-application or xma-component. This may contain "/"s.
  • optional parameters parmeters send to the specified resource. name=value pairs seperated by "&".
  • Author(s):
    s2877, s3595
    Version:
    $Id: XMA_URI.java 2084 2007-11-27 14:53:31Z s3460 $
 
 public class XMA_URI {

    
hostname is defined as:
server_environment.installation

is the part of the hostname, that referes to the installation environment of the AS. e.g.: .server.lan.at

 
     private String installation_;

    
the xma application name
 
     private String application_;

    
first part of the hostname, currently this is also the application name
 
     private String server_;

    
defines the environment we currently run in. e.g.: devel or prod | ... it is also encoded into the hostname.
 
     private String environment_;

    
xma component name
 
     private String component_;

    
xma resource name
 
     private String[] resource_;

    
request parameters
    private HashMap parameter_=new HashMap();

    
server port
    private int port_;
    private static final String DEFAULT_PROTOCOL = .;
    private String protocol_ = ;

    
encoding used for URLs
    private static String encoding = System.getProperty("file.encoding");

    
Set configuration properties. this will only be used by the boot runtime.
    public static void setPropertiesProperties pnew ) {
        String enc = pnew.getProperty("boot.transport.urlencoding");
        if(enc!=null) {
             = enc;
        }
    }

    
Constructs a XMA_URI from a java.net.URL. It parses its information from the given URL.

The java.net.URL may contain escaped characters. This characters are unescaped automatically. Additional slashes inbetween or at the end of an url are removed.
e.g. http://server/app//comp -> http://server/app/comp
e.g. http://server/app/ -> http://server/app
Additional '&' in query strings are removed.
e.g. http://server/app?&name=sepp&&a=b& -> http://server/app?name=sepp&a=b

Parameters:
url the URL to parse the information from.
Throws:
java.net.MalformedURLException
  • if a protocoll different from http or https is specified
  • if no server is specified
  • if a parameter in the query string is illegal
    public XMA_URI(URL urlthrows MalformedURLException {
        ifurl == nullthrow new IllegalArgumentException();
        try {
                = url.getProtocol();
            if(!"http".equalsIgnoreCase()&&!"https".equalsIgnoreCase()) {
                throw new MalformedURLException(url.toString()+" protocol '"++"' not supported");
            }
                  = URLDecoder.decode(url.getHost(),);
            if(==null||.length()==0) {
                throw new MalformedURLException(url.toString()+" server missing");
            }
                    = url.getPort();
            String path = url.getPath();
            StringTokenizer tok = new StringTokenizer(path,"/");
            if(tok.hasMoreTokens()) {
                = URLDecoder.decode(tok.nextToken(),);
            }
            if(tok.hasMoreTokens()) {
                = URLDecoder.decode(tok.nextToken(),);
            }
            if(tok.hasMoreTokens()) {
                List list = new ArrayList();
                while(tok.hasMoreTokens()) {
                    String res = URLDecoder.decode(tok.nextToken(),);
                    list.add(res);
                }
                 = new String[list.size()];
                for(int i=0;i<list.size();i++) {
                    [i]=(String)list.get(i);
                }
            }
            String query = url.getQuery();
            if(query!=null) {
                for(StringTokenizer to = new StringTokenizer(query,"&");to.hasMoreTokens();) {
                    String param = to.nextToken();
                    int i = param.indexOf('=');
                    if(i>0&&i<param.length()) {
                        String key = URLDecoder.decode(param.substring(0,i),);
                        String value = URLDecoder.decode(param.substring(i+1,param.length()),);
                        .put(key,value);
                    } else {
                        throw new MalformedURLException(url.toString()+" url-parameter '"+param+"' malformed");
                    }
                }
            }
        } catch (UnsupportedEncodingException uee) {
            throw new RuntimeException(uee);
        }
    }


    
Constructs a XMA_URI from a string. It uses XMA_URI(java.net.URL) ot parse its information from the given string.

Parameters:
uri the string to parse the information from.
Throws:
java.net.MalformedURLException pased throu from java.net.URL and XMA_URI(java.net.URL).
    public XMA_URI(String urithrows MalformedURLException {
        this(new URL(uri));
    }

    
Copy-constructor. The constructed XMA_URI will be an exact copy of the given parameter.

Parameters:
uri to copy its data from.
    public XMA_URI(XMA_URI uri) {
        copyURI(thisuri);
    }

    
internal constructor
    private XMA_URI() { }

    
Get copy of this URI that contains only an base URI to the application root. This is for example: http://host/application

Returns:
XMA_URI base URI to application
    public XMA_URI getApplicationURI() {
        XMA_URI newuri = new XMA_URI();
        copyURI(newurithis);
        newuri.component_ = null;
        newuri.resource_  = null;
        newuri.parameter_.clear();
        return newuri;
    }

    
copy all atrributes of the input uri from to the destination uri to.

Parameters:
from copy from
to copy to
    private void copyURI(XMA_URI toXMA_URI from ) {
        to.protocol_=from.protocol_;
        to.installation_=from.installation_;
        to.environment_=from.environment_;
        to.server_=from.server_;
        to.port_=from.port_;
        to.application_=from.application_;
        to.component_=from.component_;
        if(from.resource_==null) {
            to.resource_=null;
        } else {
            to.resource_=new String[from.resource_.length];
            System.arraycopy(from.resource_,0,to.resource_,0,from.resource_.length);
        }
        to.parameter_=new HashMap(from.parameter_);
    }


    
Adds a parameter to the parameters of the query string of the url.

Parameters:
key the name of the parameter
value the value of the parameter
    public void addParameter(String key,String value) {
        .put(key,value);
    }

    
Converts this XMA_URI to a java.net.URL using the http-protocol.

Returns:
URL a URL required by http transport layer
    public URL getHTTP_URI() {
        return toURLtoString());
    }

    
Get a String representation of this XMA_URI. This String represents a valid HTTP-URL. Characters considered unsafe for HTTP-URLs are escaped automatically.

Returns:
String uri-string
    public String toString() {
        try {
            StringBuffer url = new StringBuffer(32);
            url = getHostApp(url);
            if(!=nullurl.append("/"+URLEncoder.encode(,)); //$NON-NLS-1$
            if(!=null) {
                for(int i=0;i<.;i++) {
                    url.append("/"+URLEncoder.encode([i],)); //$NON-NLS-1$
                }
            }
            appendQuery(url);
            return url.toString();
        } catch (UnsupportedEncodingException uee) {
            throw new RuntimeException(uee);
        }
    }


    
Converts the given String to a java.net.URL. The String must contain a valid url-string.

Parameters:
url containing a valid url-string.
Returns:
URL the converted URL
    private URL toURLString url ) {
        try {
            return new URL(url);
        } catch (MalformedURLException e) {
            Logger.getLogger"boot.XMA_URI" ).log(."exception:"e);
            throw new RuntimeException(e);
        }
    }

    
Appends protocol, server and if not empty environment and installation and port to the given StringBuffer.

Parameters:
url to append
Returns:
the given url with the mentioned data added.
    private StringBuffer getHostPortStringBuffer url ){
        try {
            url.append );
            url.append"://" + URLEncoder.encode( ,)); //$NON-NLS-1$
            if!=null  ) url.append();
            if!=null ) url.append();
            if > 0           ) url.append':' + Integer.toString( ) );
            return url;
        } catch (UnsupportedEncodingException uee) {
            throw new RuntimeException(uee);
        }
    }

    
Appends the application part of the URI to the given StringBuffer

Parameters:
url to append
Returns:
the given url with the mentioned data added.
    private StringBuffer getHostAppStringBuffer url ){
        try {
            url = getHostPort(url);
            if ( != null) {
                url.append("/"); //$NON-NLS-1$
                url.append(URLEncoder.encode(,));
            }
            return url;
        } catch (UnsupportedEncodingException uee) {
            throw new RuntimeException(uee);
        }
    }

    
Appends the query parameters of the URI to the given StringBuffer

Parameters:
url to append
Returns:
the given url with the mentioned data added.
    private StringBuffer appendQueryStringBuffer url ){
        try {
            if(.size()>0) {
                url.append("?"); //$NON-NLS-1$
                for(Iterator it=.keySet().iterator();it.hasNext();){
                    String key = (String)it.next();
                    String value = (String).get(key);
                    if(key!=nullkey = URLEncoder.encode(key,);
                    if(value!=nullvalue = URLEncoder.encode(value,);
                    url.append(key+"="+value); //$NON-NLS-1$
                    if(it.hasNext()) url.append("&"); //$NON-NLS-1$
                }
            }
            return url;
        } catch (UnsupportedEncodingException uee) {
            throw new RuntimeException(uee);
        }
    }

    
Gets a HTTP-URL to a resource relative to the application specified by this XMA_URI. Used to specify a custom relative context path within the application.

Returns:
URL a URL required by http transport layer
    public URL getHTTP_AppUriString context ) {
        try {
            StringBuffer url = new StringBuffer(32);
            url = getHostApp(url);
            if (context != null) {
                StringTokenizer tok = new StringTokenizer(context,"/");
                while(tok.hasMoreTokens()) {
                    String res = URLEncoder.encode(tok.nextToken(),);
                    url.append"/" + res ); //$NON-NLS-1$
                }
            }
            return toURL(url.toString());
        } catch (UnsupportedEncodingException uee) {
            throw new RuntimeException(uee);
        }
    }

    
Get the default protocol used by the transport layer. The default protocol is http.

Returns:
the default protocol
    public static String getDEFAULT_PROTOCOL() {
        return ;
    }

    
Get the application part of the URI.

Returns:
the application part
    public String getApplication() {
        return ;
    }

    
Get the component part of the URI

Returns:
the component part
    public String getComponent() {
        return ;
    }

    
Get the environment part of the URI

Returns:
the environment part
    public String getEnvironment() {
        return ;
    }

    
Get the installation part of the URI

Returns:
the installation part
    public String getInstallation() {
        return ;
    }

    
Get the port specified in the URI

Returns:
the port number
    public int getPort_() {
        return ;
    }

    
Set the port for the URI

Parameters:
port the port to use
    public void setPort(int port) {
         = port;
    }

    
Get the protocol of the URI

Returns:
the protocol
    public String getProtocol_() {
        return ;
    }

    
Sets the protocl of the URI. Only http and https are supported.

Parameters:
protocol the protocol to use in this URI
    public void setProtocol(String protocol) {
        if("http".equalsIgnoreCase() || "https".equalsIgnoreCase()) {
            =protocol;
        } else {
            throw new RuntimeException("protocol '"+protocol+"' not supported");
        }
    }

    
Get the resource part of the URI

Returns:
the resource part
    public String getResource() {
        if(==nullreturn null;
        else if(.==1) return [0];
        else {
            StringBuffer result = new StringBuffer();
            for(int i=0;i<.-1;i++) {
                result.append([i]);
                result.append('/');
            }
            result.append([.-1]);
            return result.toString();
        }
    }

    
Get the server part of the URI

Returns:
the server part
    public String getServer() {
        return ;
    }

    
Set the component part of the URI.

Parameters:
component the component name relative to this URI.
Returns:
this pointer
    public void setComponent(String component) {
         = component;
    }

    
Set the resource part of the URI

Parameters:
resource relative to this URI.
    public void setResource(String resource) {
        StringTokenizer tok = new StringTokenizer(resource,"/");
        if(tok.hasMoreTokens()) {
            List list = new ArrayList();
            String res = tok.nextToken();
            list.add(res);
            while(tok.hasMoreTokens()) {
                res = tok.nextToken();
                list.add(res);
            }
             = new String[list.size()];
            for(int i=0;i<list.size();i++) {
                [i]=(String)list.get(i);
            }
        }
    }
New to GrepCode? Check out our FAQ X