All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.spardat.xma.boot.transport.XMA_URI Maven / Gradle / Ivy

/*******************************************************************************
 * 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.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;

import at.spardat.xma.boot.Statics;
import at.spardat.xma.boot.logger.LogLevel;
import at.spardat.xma.boot.logger.Logger;

/**
 * custom URI class for xma usage. This abstraction of URI brings the capability to
 * use the same URI even for different implementations of {@link 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 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 = Statics.PROTO_HTTP; private String protocol_ = DEFAULT_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 setProperties( Properties pnew ) { String enc = pnew.getProperty("boot.transport.urlencoding"); if(enc!=null) { encoding = 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
    * * * @param url the URL to parse the information from. * @throws 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 url) throws MalformedURLException { if( url == null) throw new IllegalArgumentException(); try { protocol_ = url.getProtocol(); if(!"http".equalsIgnoreCase(protocol_)&&!"https".equalsIgnoreCase(protocol_)) { throw new MalformedURLException(url.toString()+" protocol '"+protocol_+"' not supported"); } server_ = URLDecoder.decode(url.getHost(),encoding); if(server_==null||server_.length()==0) { throw new MalformedURLException(url.toString()+" server missing"); } port_ = url.getPort(); String path = url.getPath(); StringTokenizer tok = new StringTokenizer(path,"/"); if(tok.hasMoreTokens()) { application_ = URLDecoder.decode(tok.nextToken(),encoding); } if(tok.hasMoreTokens()) { component_ = URLDecoder.decode(tok.nextToken(),encoding); } if(tok.hasMoreTokens()) { List list = new ArrayList(); while(tok.hasMoreTokens()) { String res = URLDecoder.decode(tok.nextToken(),encoding); list.add(res); } resource_ = new String[list.size()]; for(int i=0;i0&&ihttp://host/application
    * * @return XMA_URI base URI to application */ public XMA_URI getApplicationURI() { XMA_URI newuri = new XMA_URI(); copyURI(newuri, this); newuri.component_ = null; newuri.resource_ = null; newuri.parameter_.clear(); return newuri; } /** * copy all atrributes of the input uri from * to the destination uri to. * * @param from copy from * @param to copy to */ private void copyURI(XMA_URI to, XMA_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. * @param key the name of the parameter * @param value the value of the parameter */ public void addParameter(String key,String value) { parameter_.put(key,value); } /** * Converts this XMA_URI to a java.net.URL using the http-protocol. * @return URL a URL required by http transport layer */ public URL getHTTP_URI() { return toURL( toString()); } /** * Get a String representation of this XMA_URI. This String represents a valid HTTP-URL. * Characters considered unsafe for HTTP-URLs are escaped automatically. * * @return String uri-string */ public String toString() { try { StringBuffer url = new StringBuffer(32); url = getHostApp(url); if(component_!=null) url.append("/"+URLEncoder.encode(component_,encoding)); //$NON-NLS-1$ if(resource_!=null) { for(int i=0;ijava.net.URL. The String * must contain a valid url-string. * @param url containing a valid url-string. * @return URL the converted URL */ private URL toURL( String url ) { try { return new URL(url); } catch (MalformedURLException e) { Logger.getLogger( "boot.XMA_URI" ).log(LogLevel.WARNING, "exception:", e); throw new RuntimeException(e); } } /** * Appends protocol, server and if not empty environment and installation * and port to the given StringBuffer. * @param url to append * @return the given url with the mentioned data added. */ private StringBuffer getHostPort( StringBuffer url ){ try { url.append( protocol_ ); url.append( "://" + URLEncoder.encode(server_ ,encoding)); //$NON-NLS-1$ if( environment_!=null ) url.append(environment_); if( installation_!=null ) url.append(installation_); if( port_ > 0 ) url.append( ':' + Integer.toString(port_ ) ); return url; } catch (UnsupportedEncodingException uee) { throw new RuntimeException(uee); } } /** * Appends the application part of the URI to the given StringBuffer * @param url to append * @return the given url with the mentioned data added. */ private StringBuffer getHostApp( StringBuffer url ){ try { url = getHostPort(url); if (application_ != null) { url.append("/"); //$NON-NLS-1$ url.append(URLEncoder.encode(application_,encoding)); } return url; } catch (UnsupportedEncodingException uee) { throw new RuntimeException(uee); } } /** * Appends the query parameters of the URI to the given StringBuffer * @param url to append * @return the given url with the mentioned data added. */ private StringBuffer appendQuery( StringBuffer url ){ try { if(parameter_.size()>0) { url.append("?"); //$NON-NLS-1$ for(Iterator it=parameter_.keySet().iterator();it.hasNext();){ String key = (String)it.next(); String value = (String)parameter_.get(key); if(key!=null) key = URLEncoder.encode(key,encoding); if(value!=null) value = URLEncoder.encode(value,encoding); 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. * * @return URL a URL required by http transport layer */ public URL getHTTP_AppUri( String 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(),encoding); 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. * @return the default protocol */ public static String getDEFAULT_PROTOCOL() { return DEFAULT_PROTOCOL; } /** * Get the application part of the URI. * @return the application part */ public String getApplication() { return application_; } /** * Get the component part of the URI * @return the component part */ public String getComponent() { return component_; } /** * Get the environment part of the URI * @return the environment part */ public String getEnvironment() { return environment_; } /** * Get the installation part of the URI * @return the installation part */ public String getInstallation() { return installation_; } /** * Get the port specified in the URI * @return the port number */ public int getPort_() { return port_; } /** * Set the port for the URI * @param port the port to use */ public void setPort(int port) { port_ = port; } /** * Get the protocol of the URI * @return the protocol */ public String getProtocol_() { return protocol_; } /** * Sets the protocl of the URI. * Only http and https are supported. * @param protocol the protocol to use in this URI */ public void setProtocol(String protocol) { if("http".equalsIgnoreCase(protocol_) || "https".equalsIgnoreCase(protocol_)) { protocol_=protocol; } else { throw new RuntimeException("protocol '"+protocol+"' not supported"); } } /** * Get the resource part of the URI * @return the resource part */ public String getResource() { if(resource_==null) return null; else if(resource_.length==1) return resource_[0]; else { StringBuffer result = new StringBuffer(); for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy