|
VolD 0.1
|
00001 00002 package de.zib.vold.userInterface; 00003 00004 import de.zib.vold.common.VoldException; 00005 import de.zib.vold.common.Key; 00006 import de.zib.vold.common.URIKey; 00007 import de.zib.vold.frontend.Frontend; 00008 00009 import java.util.List; 00010 import java.util.Set; 00011 import java.util.HashSet; 00012 import java.util.Map; 00013 import java.util.HashMap; 00014 00015 import org.springframework.util.MultiValueMap; 00016 00017 import javax.servlet.http.HttpServletRequest; 00018 00019 import org.springframework.beans.factory.annotation.Autowired; 00020 import org.springframework.http.HttpStatus; 00021 import org.springframework.http.ResponseEntity; 00022 import org.springframework.stereotype.Controller; 00023 import org.springframework.web.bind.annotation.ModelAttribute; 00024 import org.springframework.web.bind.annotation.RequestMapping; 00025 import org.springframework.web.bind.annotation.RequestMethod; 00026 import org.springframework.web.bind.annotation.RequestParam; 00027 import org.springframework.web.bind.annotation.RequestBody; 00028 00029 import org.slf4j.Logger; 00030 import org.slf4j.LoggerFactory; 00031 00032 import java.io.File; 00033 00040 @Controller 00041 @RequestMapping( "*" ) 00042 public class RESTController 00043 { 00044 protected final Logger log = LoggerFactory.getLogger( this.getClass() ); 00045 private static String defaulttype = "list"; 00046 00047 private Frontend frontend; 00048 private String enc = "utf-8"; 00049 00050 private void checkState() 00051 { 00052 if( null == frontend ) 00053 { 00054 throw new IllegalStateException( "Tried to operate on REST controller while it had not been initialized yet. Set a frontend first!" ); 00055 } 00056 } 00057 00068 @RequestMapping( method = RequestMethod.POST ) 00069 public ResponseEntity< Map< String, String > > post( 00070 @ModelAttribute("clientIpAddress") String clientIpAddress, 00071 @RequestParam MultiValueMap< String, String > args, 00072 @RequestBody MultiValueMap< String, String > argsbody, 00073 HttpServletRequest request) 00074 { 00075 00076 // guard 00077 { 00078 if( argsbody != null ) 00079 log.debug( "POST: " + args.toString() + " AND " + argsbody.toString() ); 00080 else 00081 log.debug( "POST: " + args.toString() ); 00082 00083 checkState(); 00084 } 00085 00086 Map< String, String > invalidKeys = new HashMap< String, String >(); 00087 00088 // get actual scope 00089 String scope; 00090 { 00091 scope = request.getRequestURI(); 00092 String removepath = request.getContextPath() + request.getServletPath(); 00093 00094 scope = scope.substring( removepath.length(), scope.length() ); 00095 } 00096 00097 // merge args to argsbody 00098 { 00099 if( null == argsbody ) 00100 { 00101 if( null == args ) 00102 { 00103 log.warn( "Got a totally empty request from " + clientIpAddress + "." ); 00104 return new ResponseEntity< Map < String, String > >( invalidKeys, HttpStatus.OK ); 00105 } 00106 00107 argsbody = args; 00108 } 00109 else if( null != args ) 00110 { 00111 argsbody.putAll( args ); 00112 } 00113 } 00114 00115 // process each key 00116 { 00117 for( Map.Entry< String, List< String > > entry: argsbody.entrySet() ) 00118 { 00119 URIKey urikey; 00120 String source; 00121 Key k; 00122 00123 // build key 00124 { 00125 urikey = URIKey.fromURIString( entry.getKey(), enc ); 00126 00127 File path_correction = new File( scope + "/" + urikey.getKey().get_scope() ); 00128 00129 k = new Key( 00130 path_correction.getPath(), 00131 urikey.getKey().get_type(), 00132 urikey.getKey().get_keyname() 00133 ); 00134 00135 if( null == urikey.getSource() ) 00136 { 00137 source = clientIpAddress; 00138 } 00139 else 00140 { 00141 source = urikey.getSource(); 00142 } 00143 } 00144 00145 // handle write request for that key 00146 { 00147 try 00148 { 00149 if( urikey.isDelete() ) 00150 { 00151 frontend.delete( source, k ); 00152 } 00153 else if( urikey.isRefresh() ) 00154 { 00155 frontend.refresh( source, k ); 00156 } 00157 else 00158 { 00159 frontend.insert( source, k, new HashSet< String >( entry.getValue() ) ); 00160 } 00161 } 00162 catch( VoldException e ) 00163 { 00164 log.error( "Could not handle write request for key " + entry.getKey() + ". ", e ); 00165 invalidKeys.put( entry.getKey().toString(), "ERROR: " + e.getMessage() ); 00166 continue; 00167 } 00168 } 00169 } 00170 } 00171 00172 return new ResponseEntity< Map < String, String > >( invalidKeys, HttpStatus.OK ); 00173 } 00174 00183 @RequestMapping( method = RequestMethod.GET ) 00184 public ResponseEntity< Map< Key, Set< String > > > get( 00185 @RequestParam Map< String, String > keys, 00186 HttpServletRequest request ) 00187 { 00188 // guard 00189 { 00190 log.debug( "GET: " + keys.toString() ); 00191 00192 checkState(); 00193 } 00194 00195 Map< Key, Set< String > > merged_result = new HashMap< Key, Set< String > >(); 00196 00197 // get actual scope 00198 String scope; 00199 { 00200 scope = request.getRequestURI(); 00201 String removepath = request.getContextPath() + request.getServletPath(); 00202 00203 scope = scope.substring( removepath.length(), scope.length() ); 00204 } 00205 00206 // process each key 00207 for( Map.Entry< String, String > entry: keys.entrySet() ) 00208 { 00209 URIKey urikey; 00210 Key k; 00211 00212 // build key 00213 { 00214 urikey = URIKey.fromURIString( entry.getKey(), enc ); 00215 00216 File path_correction = new File( scope + "/" + urikey.getKey().get_scope() ); 00217 00218 k = new Key( 00219 path_correction.getPath(), 00220 urikey.getKey().get_type(), 00221 urikey.getKey().get_keyname() 00222 ); 00223 } 00224 00225 // lookup and remember result 00226 { 00227 Map< Key, Set< String > > _result; 00228 00229 try 00230 { 00231 _result = frontend.lookup( k ); 00232 } 00233 catch( VoldException e ) 00234 { 00235 log.error( "Error on lookup for key " + k + " (" + entry.getKey() + "): ", e ); 00236 continue; 00237 /* 00238 Set< String > s = new HashSet< String >(); 00239 s.add( e.getMessage() ); 00240 00241 merged_result.clear(); 00242 merged_result.put( k, s ); 00243 00244 return new ResponseEntity< Map< Key, Set< String > > >( 00245 merged_result, 00246 HttpStatus.INTERNAL_SERVER_ERROR ); 00247 */ 00248 } 00249 00250 // found something 00251 if( null != _result ) 00252 { 00253 merged_result.putAll( _result ); 00254 } 00255 } 00256 } 00257 00258 return new ResponseEntity< Map<Key, Set< String > > >( merged_result, HttpStatus.OK ); 00259 } 00260 00261 @ModelAttribute("clientIpAddress") 00262 public String populateClientIpAddress( HttpServletRequest request ) 00263 { 00264 return request.getRemoteAddr(); 00265 } 00266 00267 @Autowired 00268 public void setFrontend( Frontend frontend ) 00269 { 00270 this.frontend = frontend; 00271 } 00272 00273 public void setEnc( String enc ) 00274 { 00275 this.enc = enc; 00276 } 00277 }