Project

General

Profile

« Previous | Next » 

Revision 6514

correctly handle incoming "accessPolicy" parameters for the setAccess() method

View differences:

src/edu/ucsb/nceas/metacat/restservice/MNResourceHandler.java
157 157
            
158 158
            // get the rest of the path info
159 159
            String extra = null;
160
            if (resource.lastIndexOf("/") != -1) {
161
                extra = resource.substring(resource.lastIndexOf("/") + 1);
162
            }
163 160
                        
164 161
            logMetacat.debug("handling verb " + httpVerb + " request with resource '" + resource + "'");
165 162
            logMetacat.debug("resource: '" + resource + "'");
......
173 170
                    status = true;
174 171
                } else if (resource.startsWith(RESOURCE_ACCESS_RULES)) {
175 172
                    if (httpVerb == POST) {
173
                    	// after the command
174
                        extra = parseTrailing(resource, RESOURCE_ACCESS_RULES);
176 175
	                	// set the access rules
177
	                    setAccess();
176
	                    setAccess(extra);
178 177
	                    status = true;
179 178
	                    logMetacat.debug("done setting access");
180 179
                    }
181 180
                } else if (resource.startsWith(RESOURCE_IS_AUTHORIZED)) {
182 181
                    if (httpVerb == GET) {
182
                    	// after the command
183
                        extra = parseTrailing(resource, RESOURCE_IS_AUTHORIZED);
183 184
	                	// check the access rules
184 185
	                    isAuthorized(extra);
185 186
	                    status = true;
......
189 190
                    logMetacat.debug("Using resource 'meta'");
190 191
                    // get
191 192
                    if (httpVerb == GET) {
193
                    	// after the command
194
                        extra = parseTrailing(resource, RESOURCE_META);
192 195
                        getSystemMetadataObject(extra);
193 196
                        status = true;
194 197
                    }
195 198
                    
196 199
                } else if (resource.startsWith(RESOURCE_OBJECTS)) {
197 200
                    logMetacat.debug("Using resource 'object'");
198
                    
201
                    // after the command
202
                    extra = parseTrailing(resource, RESOURCE_OBJECTS);
199 203
                    logMetacat.debug("objectId: " + extra);
200 204
                    logMetacat.debug("verb:" + httpVerb);
201 205

  
......
227 231
                    logMetacat.debug("Using resource 'checksum'");
228 232
                    // handle checksum requests
229 233
                    if (httpVerb == GET) {
234
                    	// after the command
235
                        extra = parseTrailing(resource, RESOURCE_CHECKSUM);
230 236
                        checksum(extra);
231 237
                        status = true;
232 238
                    }
233 239
                } else if (resource.startsWith(RESOURCE_MONITOR)) {
234 240
                    // there are various parts to monitoring
235 241
                    if (httpVerb == GET) {
242
                    	// after the command
243
                        extra = parseTrailing(resource, RESOURCE_MONITOR);
236 244
                    	// health monitoring calls
237 245
                        status = monitor(extra);
238 246
                    }
......
330 338
		MNodeService.getInstance().synchronizationFailed(session, syncFailed);
331 339
    }
332 340
    
333
    protected SynchronizationFailed collectSynchronizationFailed() throws IOException, ServiceFailure, InvalidRequest, JiBXException, InstantiationException, IllegalAccessException, ParserConfigurationException, SAXException  {
334
		
335
		// Read the incoming data from its Mime Multipart encoding
336
		logMetacat.debug("Disassembling MIME multipart form");
337
		InputStream sf = null;
338 341

  
339
		// handle MMP inputs
340
		File tmpDir = getTempDirectory();
341
		logMetacat.debug("temp dir: " + tmpDir.getAbsolutePath());
342
		MultipartRequestResolver mrr = 
343
			new MultipartRequestResolver(tmpDir.getAbsolutePath(), 1000000000, 0);
344
		MultipartRequest mr = null;
345
		try {
346
			mr = mrr.resolveMultipart(request);
347
		} catch (Exception e) {
348
			throw new ServiceFailure("2161", 
349
					"Could not resolve multipart: " + e.getMessage());
350
		}
351
		logMetacat.debug("resolved multipart request");
352
		Map<String, File> files = mr.getMultipartFiles();
353
		if (files == null || files.keySet() == null) {
354
			throw new InvalidRequest("2163",
355
					"must have multipart file with name 'message'");
356
		}
357
		logMetacat.debug("got multipart files");
358

  
359
		multipartparams = mr.getMultipartParameters();
360

  
361
		File sfFile = files.get("message");
362
		if (sfFile == null) {
363
			throw new InvalidRequest("2163",
364
					"Missing the required file-part 'message' from the multipart request.");
365
		}
366
		logMetacat.debug("sfFile: " + sfFile.getAbsolutePath());
367
		sf = new FileInputStream(sfFile);
368
	
369
		SynchronizationFailed syncFailed = (SynchronizationFailed) ExceptionHandler.deserializeXml(sf, "Error deserializing exception");
370
		return syncFailed;
371
	}
372

  
373 342
    /**
374 343
     * Handles the monitoring resources
375 344
     * @return
......
973 942
     * @throws IllegalAccessException 
974 943
     * @throws InstantiationException 
975 944
     * @throws IOException 
945
     * @throws SAXException 
946
     * @throws ParserConfigurationException 
976 947
     */
977
    protected void setAccess() throws JiBXException, InvalidToken, ServiceFailure, NotFound, NotAuthorized, NotImplemented, InvalidRequest, IOException, InstantiationException, IllegalAccessException
948
    protected void setAccess(String pid) throws JiBXException, InvalidToken, ServiceFailure, NotFound, NotAuthorized, NotImplemented, InvalidRequest, IOException, InstantiationException, IllegalAccessException, ParserConfigurationException, SAXException
978 949
    {
979 950
    
980
        String pid = params.get("pid")[0];
951
        //String pid = params.get("pid")[0];
981 952
        Identifier id = new Identifier();
982 953
        id.setValue(pid);
983
        String accesspolicy = params.get("accesspolicy")[0];
984
        AccessPolicy accessPolicy = TypeMarshaller.unmarshalTypeFromStream(AccessPolicy.class, new ByteArrayInputStream(accesspolicy.getBytes("UTF-8")));
954
        
955
        AccessPolicy accessPolicy = collectAccessPolicy();
985 956
        MNodeService.getInstance().setAccessPolicy(session, id, accessPolicy);
986 957
        
987 958
        
988 959
    }
989 960

  
961
	protected SynchronizationFailed collectSynchronizationFailed() throws IOException, ServiceFailure, InvalidRequest, JiBXException, InstantiationException, IllegalAccessException, ParserConfigurationException, SAXException  {
962
		
963
		// Read the incoming data from its Mime Multipart encoding
964
		logMetacat.debug("Disassembling MIME multipart form");
965
		InputStream sf = null;
966
	
967
		// handle MMP inputs
968
		File tmpDir = getTempDirectory();
969
		logMetacat.debug("temp dir: " + tmpDir.getAbsolutePath());
970
		MultipartRequestResolver mrr = 
971
			new MultipartRequestResolver(tmpDir.getAbsolutePath(), 1000000000, 0);
972
		MultipartRequest mr = null;
973
		try {
974
			mr = mrr.resolveMultipart(request);
975
		} catch (Exception e) {
976
			throw new ServiceFailure("2161", 
977
					"Could not resolve multipart: " + e.getMessage());
978
		}
979
		logMetacat.debug("resolved multipart request");
980
		Map<String, File> files = mr.getMultipartFiles();
981
		if (files == null || files.keySet() == null) {
982
			throw new InvalidRequest("2163",
983
					"must have multipart file with name 'message'");
984
		}
985
		logMetacat.debug("got multipart files");
986
	
987
		multipartparams = mr.getMultipartParameters();
988
	
989
		File sfFile = files.get("message");
990
		if (sfFile == null) {
991
			throw new InvalidRequest("2163",
992
					"Missing the required file-part 'message' from the multipart request.");
993
		}
994
		logMetacat.debug("sfFile: " + sfFile.getAbsolutePath());
995
		sf = new FileInputStream(sfFile);
996
	
997
		SynchronizationFailed syncFailed = (SynchronizationFailed) ExceptionHandler.deserializeXml(sf, "Error deserializing exception");
998
		return syncFailed;
999
	}
1000

  
990 1001
}
src/edu/ucsb/nceas/metacat/restservice/CNResourceHandler.java
34 34
import javax.servlet.ServletContext;
35 35
import javax.servlet.http.HttpServletRequest;
36 36
import javax.servlet.http.HttpServletResponse;
37
import javax.xml.parsers.ParserConfigurationException;
37 38

  
38 39
import org.apache.commons.fileupload.FileUploadException;
39 40
import org.apache.commons.io.IOUtils;
......
65 66
import org.dataone.service.util.DateTimeMarshaller;
66 67
import org.dataone.service.util.TypeMarshaller;
67 68
import org.jibx.runtime.JiBXException;
69
import org.xml.sax.SAXException;
68 70

  
69 71
import edu.ucsb.nceas.metacat.dataone.CNodeService;
70 72

  
......
150 152

  
151 153
                if (resource.startsWith(RESOURCE_ACCESS_RULES) && httpVerb == PUT) {
152 154
                    logMetacat.debug("Setting access policy");
153
                    setAccess();
155
                    // after the command
156
                    extra = parseTrailing(resource, RESOURCE_ACCESS_RULES);
157
                    setAccess(extra);
154 158
                    status = true;
155 159
                    logMetacat.debug("done setting access");
156 160
                    
......
308 312
        }
309 313
    }
310 314
    
311
    private String parseTrailing(String resource, String token) {
312
    	// get the rest
313
        String extra = null;
314
        if (resource.indexOf(token) != -1) {
315
        	// what comes after the token?
316
            extra = resource.substring(resource.indexOf(token) + token.length());
317
            // remove the slash
318
            if (extra.startsWith("/")) {
319
            	extra = extra.substring(1);
320
            }
321
            // is there anything left?
322
            if (extra.length() == 0) {
323
            	extra = null;
324
            }
325
        }
326
        return extra;
327
    }
328 315
    
329 316
    /**
330 317
     * Get the checksum for the given guid
......
787 774
     * @throws IllegalAccessException 
788 775
     * @throws InstantiationException 
789 776
     * @throws IOException 
777
     * @throws SAXException 
778
     * @throws ParserConfigurationException 
790 779
     */
791
    protected void setAccess() throws JiBXException, InvalidToken, ServiceFailure, NotFound, NotAuthorized, NotImplemented, InvalidRequest, IOException, InstantiationException, IllegalAccessException {
780
    protected void setAccess(String pid) throws JiBXException, InvalidToken, ServiceFailure, NotFound, NotAuthorized, NotImplemented, InvalidRequest, IOException, InstantiationException, IllegalAccessException, ParserConfigurationException, SAXException {
792 781

  
793
        String guid = params.get("guid")[0];
794 782
        Identifier id = new Identifier();
795
        id.setValue(guid);
796
        String accesspolicy = params.get("accesspolicy")[0];
797
        AccessPolicy accessPolicy = TypeMarshaller.unmarshalTypeFromStream(AccessPolicy.class, new ByteArrayInputStream(accesspolicy.getBytes("UTF-8")));
783
        id.setValue(pid);
784
        
785
        AccessPolicy accessPolicy = collectAccessPolicy();
798 786
        CNodeService.getInstance().setAccessPolicy(session, id, accessPolicy);
799 787

  
800 788
    }
src/edu/ucsb/nceas/metacat/restservice/D1ResourceHandler.java
24 24

  
25 25
import java.io.File;
26 26
import java.io.FileInputStream;
27
import java.io.FileNotFoundException;
28 27
import java.io.IOException;
29 28
import java.io.InputStream;
30 29
import java.io.OutputStream;
31 30
import java.io.PrintWriter;
32
import java.text.DateFormat;
33
import java.text.ParseException;
34
import java.text.SimpleDateFormat;
35
import java.util.Date;
36 31
import java.util.Enumeration;
37 32
import java.util.Hashtable;
38 33
import java.util.Iterator;
39 34
import java.util.List;
40 35
import java.util.Map;
41
import java.util.TimeZone;
42 36
import java.util.Timer;
43 37

  
44 38
import javax.servlet.ServletContext;
45 39
import javax.servlet.http.HttpServletRequest;
46 40
import javax.servlet.http.HttpServletResponse;
41
import javax.xml.parsers.ParserConfigurationException;
47 42

  
48 43
import org.apache.commons.fileupload.FileUploadException;
49 44
import org.apache.commons.io.IOUtils;
......
54 49
import org.dataone.service.exceptions.BaseException;
55 50
import org.dataone.service.exceptions.InvalidRequest;
56 51
import org.dataone.service.exceptions.ServiceFailure;
52
import org.dataone.service.types.v1.AccessPolicy;
57 53
import org.dataone.service.types.v1.Session;
58 54
import org.dataone.service.types.v1.SystemMetadata;
59 55
import org.dataone.service.util.TypeMarshaller;
60 56
import org.jibx.runtime.JiBXException;
57
import org.xml.sax.SAXException;
61 58

  
62 59
import edu.ucsb.nceas.metacat.MetacatHandler;
63 60
import edu.ucsb.nceas.metacat.properties.PropertyService;
......
148 145
        }
149 146
    }
150 147
    
148
    protected String parseTrailing(String resource, String token) {
149
    	// get the rest
150
        String extra = null;
151
        if (resource.indexOf(token) != -1) {
152
        	// what comes after the token?
153
            extra = resource.substring(resource.indexOf(token) + token.length());
154
            // remove the slash
155
            if (extra.startsWith("/")) {
156
            	extra = extra.substring(1);
157
            }
158
            // is there anything left?
159
            if (extra.length() == 0) {
160
            	extra = null;
161
            }
162
        }
163
        return extra;
164
    }
165
    
166
    protected AccessPolicy collectAccessPolicy() throws IOException, ServiceFailure, InvalidRequest, JiBXException, InstantiationException, IllegalAccessException, ParserConfigurationException, SAXException  {
167
		
168
		// Read the incoming data from its Mime Multipart encoding
169
		logMetacat.debug("Disassembling MIME multipart form");
170
		InputStream ap = null;
171

  
172
		// handle MMP inputs
173
		File tmpDir = getTempDirectory();
174
		logMetacat.debug("temp dir: " + tmpDir.getAbsolutePath());
175
		MultipartRequestResolver mrr = 
176
			new MultipartRequestResolver(tmpDir.getAbsolutePath(), 1000000000, 0);
177
		MultipartRequest mr = null;
178
		try {
179
			mr = mrr.resolveMultipart(request);
180
		} catch (Exception e) {
181
			throw new ServiceFailure("2161", 
182
					"Could not resolve multipart: " + e.getMessage());
183
		}
184
		logMetacat.debug("resolved multipart request");
185
		Map<String, File> files = mr.getMultipartFiles();
186
		if (files == null || files.keySet() == null) {
187
			throw new InvalidRequest("2163",
188
					"must have multipart file with name 'accessPolicy'");
189
		}
190
		logMetacat.debug("got multipart files");
191

  
192
		multipartparams = mr.getMultipartParameters();
193

  
194
		File apFile = files.get("accessPolicy");
195
		if (apFile == null) {
196
			throw new InvalidRequest("2163",
197
					"Missing the required file-part 'accessPolicy' from the multipart request.");
198
		}
199
		logMetacat.debug("apFile: " + apFile.getAbsolutePath());
200
		ap = new FileInputStream(apFile);
201
	
202
		AccessPolicy accessPolicy = TypeMarshaller.unmarshalTypeFromStream(AccessPolicy.class, ap);
203
		return accessPolicy;
204
	}
205
    
151 206
    protected SystemMetadata collectSystemMetadata() throws IOException, FileUploadException, ServiceFailure, InvalidRequest, JiBXException, InstantiationException, IllegalAccessException  {
152 207
		
153 208
		// Read the incoming data from its Mime Multipart encoding

Also available in: Unified diff