VolD 0.1

URIKey.java

Go to the documentation of this file.
00001 package de.zib.vold.common;
00002 
00003 import java.net.URLEncoder;
00004 import java.net.URLDecoder;
00005 
00006 import java.io.UnsupportedEncodingException;
00007 
00013 public class URIKey
00014 {
00015         private final String source;
00016         private final Key key;
00017 
00018         private final boolean refresh;
00019         private final boolean delete;
00020         private final String enc;
00021 
00033         public URIKey( String source, String scope, String type, String keyname, boolean refresh, boolean delete, String enc )
00034         {
00035                 this.source = source;
00036                 this.key = new Key( scope, type, keyname );
00037                 this.refresh = refresh;
00038                 this.delete = delete;
00039 
00040                 this.enc = enc;
00041 
00042                 if( true == refresh && true == delete )
00043                 {
00044                         throw new IllegalArgumentException( "Cannot mark a URIKey with refresh and delete operation." );
00045                 }
00046         }
00047 
00053         public String getSource( )
00054         {
00055                 return source;
00056         }
00057 
00065         public Key getKey( )
00066         {
00067                 return key;
00068         }
00069 
00073         public boolean isRefresh( )
00074         {
00075                 return refresh;
00076         }
00077 
00081         public boolean isDelete( )
00082         {
00083                 return delete;
00084         }
00085 
00091         public String toURIString( )
00092         {
00093                 StringBuilder sb = new StringBuilder( );
00094                 
00095                 try
00096                 {
00097                         if( null != source )
00098                         {
00099                                 sb.append( URLEncoder.encode( source, enc ) );
00100                         }
00101 
00102                         sb.append( "/" );
00103                         sb.append( URLEncoder.encode( key.get_scope(), enc ) );
00104                         sb.append( "/" );
00105                         sb.append( URLEncoder.encode( key.get_type(), enc ) );
00106                         sb.append( ":" );
00107                         sb.append( URLEncoder.encode( key.get_keyname(), enc ) );
00108 
00109                         if( refresh )
00110                         {
00111                                 sb.append( "<" );
00112                         }
00113                         else if( delete )
00114                         {
00115                                 sb.append( ">" );
00116                         }
00117                 }
00118                 catch( UnsupportedEncodingException e )
00119                 {
00120                         throw new IllegalArgumentException( "Cannot convert key.", e );
00121                 }
00122 
00123                 return sb.toString();
00124         }
00125 
00133         public static URIKey fromURIString( String uri, String enc )
00134         {
00135                 String source;
00136                 String scope;
00137                 String type;
00138                 String keyname;
00139 
00140                 boolean refresh = false;
00141                 boolean delete = false;
00142 
00143                 // get source
00144                 {
00145                         int slashindex = uri.indexOf( "/" );
00146 
00147                         if( slashindex > 0 )
00148                         {
00149                                 source = uri.substring( 0, slashindex );
00150                                 uri = uri.substring( slashindex );
00151                         }
00152                         else
00153                         {
00154                                 source = null;
00155                         }
00156                 }
00157 
00158                 // get scope
00159                 {
00160                         int slashindex = uri.lastIndexOf( "/" );
00161 
00162                         if( -1 == slashindex )
00163                         {
00164                                 scope = "/";
00165                         }
00166                         else
00167                         {
00168                                 scope = uri.substring( 1, slashindex );
00169                                 uri = uri.substring( slashindex+1 );
00170                         }
00171                 }
00172 
00173                 // get type
00174                 {
00175                         String[] splited = uri.split( ":", 2 );
00176 
00177                         if( 2 == splited.length )
00178                         {
00179                                 type = splited[0];
00180                                 uri = splited[1];
00181                         }
00182                         else if( 1 == splited.length )
00183                         {
00184                                 type = new String();
00185                         }
00186                         else
00187                         {
00188                                 throw new IllegalArgumentException( "Invalid format of URIString." );
00189                         }
00190                 }
00191 
00192                 // get operation
00193                 {
00194                         if( uri.endsWith( "<" ) )
00195                         {
00196                                 refresh = true;
00197                                 uri = uri.substring( 0, uri.length()-1 );
00198                         }
00199                         else if( uri.endsWith( ">" ) )
00200                         {
00201                                 delete = true;
00202                                 uri = uri.substring( 0, uri.length()-1 );
00203                         }
00204                 }
00205 
00206                 // get keyname
00207                 {
00208                         keyname = new String( uri );
00209                 }
00210 
00211                 try
00212                 {
00213                         if( null != source )
00214                                 source = URLDecoder.decode( source, enc );
00215                         scope = URLDecoder.decode( scope, enc );
00216                         type = URLDecoder.decode( type, enc );
00217                         keyname = URLDecoder.decode( keyname, enc );
00218                 }
00219                 catch( UnsupportedEncodingException e )
00220                 {
00221                         throw new IllegalArgumentException( "Cannot convert key.", e );
00222                 }
00223 
00224 
00225                 return new URIKey( source, scope, type, keyname, refresh, delete, enc );
00226         }
00227 }