Project

General

Profile

1 5211 jones
/**
2
 *  '$RCSfile$'
3 5805 berkley
 *  Copyright: 2011 Regents of the University of California and the
4 5211 jones
 *              National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author: Serhan AKIN $'
7
 *     '$Date: 2009-06-13 15:28:13 +0300  $'
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
 */
23
package edu.ucsb.nceas.metacat.restservice;
24
25 6130 leinfelder
import java.io.File;
26 6269 leinfelder
import java.io.FileInputStream;
27
import java.io.FileNotFoundException;
28 6130 leinfelder
import java.io.IOException;
29
import java.io.InputStream;
30
import java.io.OutputStream;
31
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
import java.util.Enumeration;
37
import java.util.Hashtable;
38 6269 leinfelder
import java.util.Iterator;
39
import java.util.List;
40
import java.util.Map;
41 6130 leinfelder
import java.util.TimeZone;
42
import java.util.Timer;
43 5211 jones
44
import javax.servlet.ServletContext;
45
import javax.servlet.http.HttpServletRequest;
46
import javax.servlet.http.HttpServletResponse;
47 6067 rnahf
48 6269 leinfelder
import org.apache.commons.fileupload.FileUploadException;
49 5299 jones
import org.apache.commons.io.IOUtils;
50 5211 jones
import org.apache.log4j.Logger;
51 6244 leinfelder
import org.dataone.client.auth.CertificateManager;
52 6269 leinfelder
import org.dataone.mimemultipart.MultipartRequest;
53
import org.dataone.mimemultipart.MultipartRequestResolver;
54 5299 jones
import org.dataone.service.exceptions.BaseException;
55 6269 leinfelder
import org.dataone.service.exceptions.InvalidRequest;
56
import org.dataone.service.exceptions.ServiceFailure;
57 6130 leinfelder
import org.dataone.service.types.Session;
58 6269 leinfelder
import org.dataone.service.types.SystemMetadata;
59 5851 berkley
import org.dataone.service.types.util.ServiceTypeUtil;
60 5320 jones
import org.jibx.runtime.JiBXException;
61 5211 jones
62
import edu.ucsb.nceas.metacat.MetacatHandler;
63 5637 berkley
import edu.ucsb.nceas.metacat.properties.PropertyService;
64
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
65 5211 jones
/**
66
 *
67 6268 leinfelder
 * Base class for handling D1 REST calls in Metacat
68 5211 jones
 *
69 6268 leinfelder
 * @author leinfelder
70 5211 jones
 */
71 6267 leinfelder
public class D1ResourceHandler {
72 5211 jones
73
    /**HTTP Verb GET*/
74
    public static final byte GET = 1;
75
    /**HTTP Verb POST*/
76
    public static final byte POST = 2;
77
    /**HTTP Verb PUT*/
78
    public static final byte PUT = 3;
79
    /**HTTP Verb DELETE*/
80
    public static final byte DELETE = 4;
81 5651 berkley
    /**HTTP Verb HEAD*/
82
    public static final byte HEAD = 5;
83 5211 jones
84
    /*
85
     * API Resources
86
     */
87 6271 leinfelder
    protected static final String RESOURCE_BASE_URL = "d1";
88
89 6247 leinfelder
    protected static final String RESOURCE_OBJECTS = "object";
90
    protected static final String RESOURCE_META = "meta";
91
    protected static final String RESOURCE_LOG = "log";
92
    protected static final String RESOURCE_CHECKSUM = "checksum";
93 6249 leinfelder
94
    protected static final String RESOURCE_IS_AUTHORIZED = "isAuthorized";
95
    protected static final String RESOURCE_ACCESS_RULES = "accessRules";
96 5211 jones
97
    /*
98
     * API Functions used as URL parameters
99
     */
100 6247 leinfelder
    protected static final String FUNCTION_NAME_INSERT = "insert";
101
    protected static final String FUNCTION_NAME_UPDATE = "update";
102 5211 jones
103 6247 leinfelder
    protected static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
104 5414 berkley
105 6247 leinfelder
    protected ServletContext servletContext;
106
    protected Logger logMetacat;
107
    protected MetacatHandler handler;
108
    protected HttpServletRequest request;
109
    protected HttpServletResponse response;
110 5211 jones
111 6247 leinfelder
    protected Hashtable<String, String[]> params;
112 6269 leinfelder
    protected Map<String, List<String>> multipartparams;
113 6244 leinfelder
114
    // D1 certificate-based authentication
115 6247 leinfelder
    protected Session session;
116 5211 jones
117
    /**Initializes new instance by setting servlet context,request and response*/
118 6267 leinfelder
    public D1ResourceHandler(ServletContext servletContext,
119 5211 jones
            HttpServletRequest request, HttpServletResponse response) {
120
        this.servletContext = servletContext;
121
        this.request = request;
122
        this.response = response;
123
    }
124
125
    /**
126 6268 leinfelder
     * This function is called from REST API servlet and handles each request
127 5211 jones
     *
128
     * @param httpVerb (GET, POST, PUT or DELETE)
129
     */
130
    public void handle(byte httpVerb) {
131 6267 leinfelder
        logMetacat = Logger.getLogger(D1ResourceHandler.class);
132 5211 jones
        try {
133 6268 leinfelder
134 6244 leinfelder
            // load session from certificate in request
135
            session = CertificateManager.getInstance().getSession(request);
136 5211 jones
137 6268 leinfelder
            // initialize the parameters
138
            params = new Hashtable<String, String[]>();
139
            initParams();
140 5211 jones
141 6268 leinfelder
            // create the handler for interacting with Metacat
142
            Timer timer = new Timer();
143
            handler = new MetacatHandler(timer);
144 5211 jones
145
        } catch (Exception e) {
146 6268 leinfelder
        	// TODO: more D1 structure when failing here
147
        	response.setStatus(400);
148
            printError("Incorrect resource!", response);
149
            logMetacat.error(e.getClass() + ": " + e.getMessage(), e);
150 5211 jones
        }
151
    }
152 5374 berkley
153 6269 leinfelder
    protected SystemMetadata collectSystemMetadata() throws IOException, FileUploadException, ServiceFailure, InvalidRequest, JiBXException  {
154
155
		// Read the incoming data from its Mime Multipart encoding
156
		logMetacat.debug("Disassembling MIME multipart form");
157
		InputStream sysmeta = null;
158
159
		// handle MMP inputs
160
		File tmpDir = getTempDirectory();
161
		logMetacat.debug("temp dir: " + tmpDir.getAbsolutePath());
162
		MultipartRequestResolver mrr =
163
			new MultipartRequestResolver(tmpDir.getAbsolutePath(), 1000000000, 0);
164
		MultipartRequest mr = null;
165
		try {
166
			mr = mrr.resolveMultipart(request);
167
		} catch (Exception e) {
168
			throw new ServiceFailure("1202",
169
					"Could not resolve multipart: " + e.getMessage());
170
		}
171
		logMetacat.debug("resolved multipart request");
172
		Map<String, File> files = mr.getMultipartFiles();
173
		if (files == null) {
174
			throw new ServiceFailure("1202",
175
					"register meta must have multipart file with name 'sysmeta'");
176
		}
177
		logMetacat.debug("got multipart files");
178
179
		if (files.keySet() == null) {
180
			logMetacat.error("No file keys in MMP request.");
181
			throw new ServiceFailure(
182
					"1202",
183
					"No file keys found in MMP.  "
184
							+ "register meta must have multipart file with name 'sysmeta'");
185
		}
186
187
		// for logging purposes, dump out the key-value pairs that
188
		// constitute the request
189
		// 3 types exist: request params, multipart params, and
190
		// multipart files
191
		Iterator it = files.keySet().iterator();
192
		logMetacat.debug("iterating through request parts: " + it);
193
		while (it.hasNext()) {
194
			String key = (String) it.next();
195
			logMetacat.debug("files key: " + key);
196
			logMetacat.debug("files value: " + files.get(key));
197
		}
198
199
		multipartparams = mr.getMultipartParameters();
200
		it = multipartparams.keySet().iterator();
201
		while (it.hasNext()) {
202
			String key = (String) it.next();
203
			logMetacat.debug("multipartparams key: " + key);
204
			logMetacat.debug("multipartparams value: " + multipartparams.get(key));
205
		}
206
207
		it = params.keySet().iterator();
208
		while (it.hasNext()) {
209
			String key = (String) it.next();
210
			logMetacat.debug("param key: " + key);
211
			logMetacat.debug("param value: " + params.get(key));
212
		}
213
		logMetacat.debug("done iterating the request...");
214
215
		File smFile = files.get("sysmeta");
216
		if (smFile == null) {
217
			throw new InvalidRequest("1102",
218
					"Missing the required file-part 'sysmeta' from the multipart request.");
219
		}
220
		logMetacat.debug("smFile: " + smFile.getAbsolutePath());
221
		sysmeta = new FileInputStream(smFile);
222
223
		logMetacat.debug("Commence creation...");
224
		SystemMetadata systemMetadata = (SystemMetadata) deserializeServiceType(SystemMetadata.class, sysmeta);
225
		return systemMetadata;
226
	}
227
228
    protected Map<String, File> collectMultipartFiles() throws ServiceFailure, InvalidRequest {
229
230
        // Read the incoming data from its Mime Multipart encoding
231
        logMetacat.debug("Disassembling MIME multipart form");
232
        InputStream object = null;
233
        InputStream sysmeta = null;
234
235
236
        // handle MMP inputs
237
        File tmpDir = getTempDirectory();
238 6272 leinfelder
        logMetacat.debug("temp dir: " + tmpDir.getAbsolutePath());
239 6269 leinfelder
        MultipartRequestResolver mrr =
240
        	new MultipartRequestResolver(tmpDir.getAbsolutePath(), 1000000000, 0);
241
        MultipartRequest mr = null;
242
		try {
243
			mr = mrr.resolveMultipart(request);
244
		} catch (Exception e) {
245
            throw new ServiceFailure("1202",
246
            		"Could not resolve multipart files: " + e.getMessage());
247
		}
248 6272 leinfelder
        logMetacat.debug("resolved multipart request");
249 6269 leinfelder
        Map<String, File> files = mr.getMultipartFiles();
250
        if (files == null) {
251
            throw new ServiceFailure("1202", "create/update must have multipart files with names 'object' and 'sysmeta'");
252
        }
253 6272 leinfelder
        logMetacat.debug("got multipart files");
254 6269 leinfelder
255
        if (files.keySet() == null) {
256 6272 leinfelder
            logMetacat.error("No file keys in MMP request.");
257 6269 leinfelder
            throw new ServiceFailure("1202", "No file keys found in MMP.  " +
258
                    "create/update must have multipart files with names 'object' and 'sysmeta'");
259
        }
260
261
		// for logging purposes, dump out the key-value pairs that constitute the request
262
		// 3 types exist: request params, multipart params, and multipart files
263
        Iterator it = files.keySet().iterator();
264 6272 leinfelder
        logMetacat.debug("iterating through files");
265 6269 leinfelder
        while (it.hasNext()) {
266
            String key = (String)it.next();
267 6272 leinfelder
            logMetacat.debug("files key: " + key);
268
            logMetacat.debug("files value: " + files.get(key));
269 6269 leinfelder
        }
270
271
        multipartparams = mr.getMultipartParameters();
272
        it = multipartparams.keySet().iterator();
273 6272 leinfelder
        logMetacat.debug("iterating through multipartparams");
274 6269 leinfelder
        while (it.hasNext()) {
275
            String key = (String)it.next();
276 6272 leinfelder
            logMetacat.debug("multipartparams key: " + key);
277
            logMetacat.debug("multipartparams value: " + multipartparams.get(key));
278 6269 leinfelder
        }
279
280
        it = params.keySet().iterator();
281 6272 leinfelder
        logMetacat.debug("iterating through params");
282 6269 leinfelder
        while (it.hasNext()) {
283
            String key = (String)it.next();
284 6272 leinfelder
            logMetacat.debug("param key: " + key);
285
            logMetacat.debug("param value: " + params.get(key));
286 6269 leinfelder
        }
287 6272 leinfelder
        logMetacat.debug("done iterating the request...");
288 6269 leinfelder
289
        File smFile = files.get("sysmeta");
290
		if (smFile == null) {
291
		    throw new InvalidRequest("1102", "Missing the required file-part 'sysmeta' from the multipart request.");
292
		}
293 6272 leinfelder
        logMetacat.debug("smFile: " + smFile.getAbsolutePath());
294 6269 leinfelder
        File objFile = files.get("object");
295
		if (objFile == null) {
296
		    throw new InvalidRequest("1102", "Missing the required file-part 'object' from the multipart request.");
297
		}
298 6272 leinfelder
        logMetacat.debug("objectfile: " + objFile.getAbsolutePath());
299 6269 leinfelder
300
        return files;
301
    }
302
303 6140 cjones
		/**
304 5374 berkley
     *  copies request parameters to a hashtable which is given as argument to native metacathandler functions
305
     */
306 6247 leinfelder
    protected void initParams() {
307 5211 jones
308 5374 berkley
        String name = null;
309
        String[] value = null;
310
        Enumeration paramlist = request.getParameterNames();
311
        while (paramlist.hasMoreElements()) {
312
            name = (String) paramlist.nextElement();
313
            value = request.getParameterValues(name);
314
            params.put(name, value);
315
        }
316
    }
317
318 5211 jones
    /**
319 5428 berkley
     * parse a date and return it in GMT if it ends with a 'Z'
320
     * @param date
321
     * @return
322
     * @throws ParseException
323
     */
324 6247 leinfelder
    protected Date parseDateAndConvertToGMT(String date) throws ParseException
325 5428 berkley
    {
326 5429 berkley
        try
327
        {   //the format we want
328
            return dateFormat.parse(date);
329
        }
330
        catch(java.text.ParseException pe)
331
        {   //try another legacy format
332
            DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss'Z'");
333
            dateFormat2.setTimeZone(TimeZone.getTimeZone("GMT-0"));
334
            return dateFormat2.parse(date);
335
        }
336 5428 berkley
    }
337 5211 jones
338
    /**
339 5390 berkley
     * serialize an object of type to out
340
     * @param type the class of the object to serialize (i.e. SystemMetadata.class)
341
     * @param object the object to serialize
342
     * @param out the stream to serialize it to
343
     * @throws JiBXException
344
     */
345 6247 leinfelder
    protected void serializeServiceType(Class type, Object object, OutputStream out)
346 6268 leinfelder
      throws JiBXException {
347 5851 berkley
        ServiceTypeUtil.serializeServiceType(type, object, out);
348 5390 berkley
    }
349
350
    /**
351
     * deserialize an object of type from is
352
     * @param type the class of the object to serialize (i.e. SystemMetadata.class)
353
     * @param is the stream to deserialize from
354
     * @throws JiBXException
355
     */
356 6252 leinfelder
    protected Object deserializeServiceType(Class type, InputStream is)
357 6268 leinfelder
      throws JiBXException {
358 5851 berkley
        return ServiceTypeUtil.deserializeServiceType(type, is);
359 5390 berkley
    }
360 6268 leinfelder
361 5390 berkley
    /**
362 5808 berkley
     * locate the boundary marker for an MMP
363 5462 berkley
     * @param is
364
     * @return
365 5808 berkley
     * @throws IOException
366 5462 berkley
     */
367 5639 berkley
    protected static String[] findBoundaryString(InputStream is)
368 6268 leinfelder
        throws IOException {
369 5639 berkley
        String[] endResult = new String[2];
370 5637 berkley
        String boundary = "";
371 5639 berkley
        String searchString = "boundary=";
372 5637 berkley
        byte[] b = new byte[1024];
373 5639 berkley
        int numbytes = is.read(b, 0, 1024);
374 5826 berkley
375 5639 berkley
        while(numbytes != -1)
376 5636 berkley
        {
377 5639 berkley
            String s = new String(b, 0, numbytes);
378 5826 berkley
            int searchStringIndex = s.indexOf(searchString);
379 5639 berkley
380
            if(s.indexOf("\"", searchStringIndex + searchString.length() + 1) == -1)
381
            { //the end of the boundary is in the next byte array
382
                boundary = s.substring(searchStringIndex + searchString.length() + 1, s.length());
383
            }
384
            else if(!boundary.startsWith("--"))
385
            { //we can read the whole boundary from this byte array
386
                boundary = s.substring(searchStringIndex + searchString.length() + 1,
387
                    s.indexOf("\"", searchStringIndex + searchString.length() + 1));
388
                boundary = "--" + boundary;
389
                endResult[0] = boundary;
390
                endResult[1] = s.substring(s.indexOf("\"", searchStringIndex + searchString.length() + 1) + 1,
391
                        s.length());
392
                break;
393
            }
394
            else
395
            { //we're now reading the 2nd byte array to get the rest of the boundary
396
                searchString = "\"";
397
                searchStringIndex = s.indexOf(searchString);
398
                boundary += s.substring(0, searchStringIndex);
399
                boundary = "--" + boundary;
400
                endResult[0] = boundary;
401
                endResult[1] = s.substring(s.indexOf("\"", searchStringIndex + searchString.length() + 1) + 1,
402
                        s.length());
403
                break;
404
            }
405 5637 berkley
        }
406 5639 berkley
        return endResult;
407
    }
408
409 5808 berkley
    /**
410 5838 berkley
     * return the directory where temp files are stored
411
     * @return
412
     */
413 6248 leinfelder
    protected static File getTempDirectory()
414 5838 berkley
    {
415
        File tmpDir = null;
416 6267 leinfelder
        Logger logMetacat = Logger.getLogger(D1ResourceHandler.class);
417 6268 leinfelder
        try {
418 5639 berkley
            tmpDir = new File(PropertyService.getProperty("application.tempDir"));
419
        }
420 6268 leinfelder
        catch(PropertyNotFoundException pnfe) {
421 6267 leinfelder
            logMetacat.error("D1ResourceHandler.writeMMPPartstoFiles: " +
422 5639 berkley
                    "application.tmpDir not found.  Using /tmp instead.");
423
            tmpDir = new File("/tmp");
424
        }
425 5838 berkley
        return tmpDir;
426 5639 berkley
    }
427
428 5637 berkley
    /**
429 5211 jones
     * Prints xml response
430
     * @param message Message to be displayed
431
     * @param response Servlet response that xml message will be printed
432
     * */
433 6247 leinfelder
    protected void printError(String message, HttpServletResponse response) {
434 5211 jones
        try {
435 6267 leinfelder
            logMetacat.error("D1ResourceHandler: Printing error to servlet response: " + message);
436 5211 jones
            PrintWriter out = response.getWriter();
437
            response.setContentType("text/xml");
438
            out.println("<?xml version=\"1.0\"?>");
439
            out.println("<error>");
440
            out.println(message);
441
            out.println("</error>");
442
            out.close();
443
        } catch (IOException e) {
444
            e.printStackTrace();
445
        }
446
    }
447 5370 berkley
448 5692 berkley
    /**
449
     * serialize a D1 exception using jibx
450
     * @param e
451
     * @param out
452
     */
453 6247 leinfelder
    protected void serializeException(BaseException e, OutputStream out) {
454 5319 jones
        // TODO: Use content negotiation to determine which return format to use
455
        response.setContentType("text/xml");
456
        response.setStatus(e.getCode());
457 5512 berkley
458 6267 leinfelder
        logMetacat.error("D1ResourceHandler: Serializing exception with code " + e.getCode() + ": " + e.getMessage());
459 5512 berkley
        e.printStackTrace();
460
461 5319 jones
        try {
462
            IOUtils.write(e.serialize(BaseException.FMT_XML), out);
463
        } catch (IOException e1) {
464
            logMetacat.error("Error writing exception to stream. "
465
                    + e1.getMessage());
466
        }
467
    }
468
469 5211 jones
}