Project

General

Profile

« Previous | Next » 

Revision 6120

include CNCore implementation - only registerSystemMetadata is implemented at the moment. also - updated d1 jar (0.6.2) should be used since that is where the method is defined.
would like to consider making ResourceHandler more modular - seems like it does A LOT of different things

View differences:

src/edu/ucsb/nceas/metacat/restservice/ResourceHandler.java
74 74
import edu.ucsb.nceas.metacat.MetaCatServlet;
75 75
import edu.ucsb.nceas.metacat.MetacatHandler;
76 76
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
77
import edu.ucsb.nceas.metacat.dataone.CNCoreImpl;
77 78
import edu.ucsb.nceas.metacat.dataone.CrudService;
78 79
import edu.ucsb.nceas.metacat.dataone.HealthService;
79 80
import edu.ucsb.nceas.metacat.properties.PropertyService;
......
324 325
                    }
325 326
                } else if (resource.equals(RESOURCE_META)) {
326 327
                    System.out.println("Using resource 'meta'");
327
                    if(params != null && params.get(FUNCTION_KEYWORD) != null &&
328
                    if (params != null && params.get(FUNCTION_KEYWORD) != null &&
328 329
                            params.get(FUNCTION_KEYWORD)[0].equals(FUNCTION_NAME_GENERATE_MISSING_SYSTEM_METADATA))
329 330
                    { 
330 331
                        status = true;
......
336 337
                        {
337 338
                            objectId = request.getPathInfo().substring(1);
338 339
                        }
339
                        getSystemMetadataObject(objectId);
340
                        status = true;
340
                        
341
                        // get
342
                        if (httpVerb == GET) {
343
	                        getSystemMetadataObject(objectId);
344
	                        status = true;
345
                        }
346
                        
347
                        // post to register system metadata
348
                        if (httpVerb == POST) {
349
                        	registerSystemMetadata(objectId);
350
                        	status = true;
351
                        }
352
                        
341 353
                    }
342 354

  
343 355
                } else if (resource.equals(RESOURCE_OBJECTS)) {
......
1985 1997
        return sm;
1986 1998
    }
1987 1999

  
2000
    /**
2001
     * Register System Metadata without data or metadata object
2002
     * @param pid identifier for System Metadata entry
2003
     */
2004
	private void registerSystemMetadata(String pid) {
2005
		logMetacat.debug("Entering registerSystemMetadata: " + pid);
2006
		OutputStream out = null;
2007
		try {
2008
			out = response.getOutputStream();
2009
			response.setStatus(200);
2010
			response.setContentType("text/xml");
2011
		} catch (IOException e1) {
2012
			logMetacat.error("Could not get the output stream for writing in putObject");
2013
		}
2014
		try {
2015

  
2016
			// Read the incoming data from its Mime Multipart encoding
2017
			logMetacat.debug("Disassembling MIME multipart form");
2018
			InputStream sysmeta = null;
2019
			Map<String, List<String>> multipartparams;
2020

  
2021
			try {
2022

  
2023
				// handle MMP inputs
2024
				File tmpDir = getTempDirectory();
2025

  
2026
				logMetacat.debug("temp dir: " + tmpDir.getAbsolutePath());
2027
				MultipartRequestResolver mrr = new MultipartRequestResolver(
2028
						tmpDir.getAbsolutePath(), 1000000000, 0);
2029
				MultipartRequest mr = mrr.resolveMultipart(request);
2030
				logMetacat.debug("resolved multipart request");
2031
				Map<String, File> files = mr.getMultipartFiles();
2032
				if (files == null) {
2033
					throw new ServiceFailure("1202",
2034
							"register meta must have multipart file with name 'sysmeta'");
2035
				}
2036
				logMetacat.debug("got multipart files");
2037

  
2038
				if (files.keySet() == null) {
2039
					logMetacat.error("No file keys in MMP request.");
2040
					throw new ServiceFailure(
2041
							"1202",
2042
							"No file keys found in MMP.  "
2043
									+ "register meta must have multipart file with name 'sysmeta'");
2044
				}
2045

  
2046
				// for logging purposes, dump out the key-value pairs that
2047
				// constitute the request
2048
				// 3 types exist: request params, multipart params, and
2049
				// multipart files
2050
				Iterator it = files.keySet().iterator();
2051
				logMetacat.debug("iterating through request parts: " + it);
2052
				while (it.hasNext()) {
2053
					String key = (String) it.next();
2054
					logMetacat.debug("files key: " + key);
2055
					logMetacat.debug("files value: " + files.get(key));
2056
				}
2057

  
2058
				multipartparams = mr.getMultipartParameters();
2059
				it = multipartparams.keySet().iterator();
2060
				while (it.hasNext()) {
2061
					String key = (String) it.next();
2062
					logMetacat.debug("multipartparams key: " + key);
2063
					logMetacat.debug("multipartparams value: "
2064
							+ multipartparams.get(key));
2065
				}
2066

  
2067
				it = params.keySet().iterator();
2068
				while (it.hasNext()) {
2069
					String key = (String) it.next();
2070
					logMetacat.debug("param key: " + key);
2071
					logMetacat.debug("param value: " + params.get(key));
2072
				}
2073
				logMetacat.debug("done iterating the request...");
2074

  
2075
				File smFile = files.get("sysmeta");
2076
				if (smFile == null) {
2077
					throw new InvalidRequest("1102",
2078
							"Missing the required file-part 'sysmeta' from the multipart request.");
2079
				}
2080
				logMetacat.debug("smFile: " + smFile.getAbsolutePath());
2081
				sysmeta = new FileInputStream(smFile);
2082

  
2083
			} catch (org.apache.commons.fileupload.FileUploadException fue) {
2084
				throw new ServiceFailure("1202", "Could not upload MMP files: "
2085
						+ fue.getMessage());
2086
			} catch (IOException ioe) {
2087
				throw new ServiceFailure("1202",
2088
						"IOException when processing Mime Multipart: "
2089
								+ ioe.getMessage());
2090
			} catch (Exception e) {
2091
				throw new ServiceFailure("1202", "Error handling MMP upload: "
2092
						+ e.getClass() + ": " + e.getMessage());
2093
			}
2094

  
2095
			// Check if the objectId exists
2096
			IdentifierManager im = IdentifierManager.getInstance();
2097
			if (im.identifierExists(pid)) {
2098
				throw new IdentifierNotUnique("1000",
2099
						"Identifier is already in use: " + pid);
2100
			}
2101

  
2102
			logMetacat.debug("Commence creation...");
2103
			IBindingFactory bfact = BindingDirectory
2104
					.getFactory(SystemMetadata.class);
2105
			IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
2106
			SystemMetadata systemMetadata = (SystemMetadata) uctx
2107
					.unmarshalDocument(sysmeta, null);
2108

  
2109
			// TODO: get session
2110
			Session session = null;
2111
			Identifier guid = new Identifier();
2112
			guid.setValue(pid);
2113
			logMetacat.debug("registering system metadata with pid "
2114
					+ guid.getValue());
2115
			boolean result = CNCoreImpl.getInstance().registerSystemMetaData(
2116
					session, guid, systemMetadata);
2117
			serializeServiceType(Boolean.class, result, out);
2118

  
2119
		} catch (NotAuthorized e) {
2120
			response.setStatus(500);
2121
			serializeException(e, out);
2122
		} catch (ServiceFailure e) {
2123
			response.setStatus(500);
2124
			serializeException(e, out);
2125
		} catch (IdentifierNotUnique e) {
2126
			response.setStatus(500);
2127
			serializeException(e, out);
2128
		} catch (NotImplemented e) {
2129
			response.setStatus(500);
2130
			serializeException(e, out);
2131
		} catch (InvalidRequest e) {
2132
			response.setStatus(500);
2133
			serializeException(e, out);
2134
		} catch (JiBXException e) {
2135
			response.setStatus(500);
2136
			e.printStackTrace(System.out);
2137
			InvalidSystemMetadata ism = new InvalidSystemMetadata("1080", e
2138
					.getMessage());
2139
			serializeException(ism, out);
2140
		}
2141
	}
2142

  
1988 2143
}
src/edu/ucsb/nceas/metacat/dataone/CNCoreImpl.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2000 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author$'
7
 *     '$Date$'
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.dataone;
24

  
25
import java.util.Date;
26
import java.util.Hashtable;
27
import java.util.Timer;
28

  
29
import org.apache.log4j.Logger;
30
import org.dataone.service.cn.tier1.CNCore;
31
import org.dataone.service.exceptions.IdentifierNotUnique;
32
import org.dataone.service.exceptions.InsufficientResources;
33
import org.dataone.service.exceptions.InvalidRequest;
34
import org.dataone.service.exceptions.InvalidSystemMetadata;
35
import org.dataone.service.exceptions.InvalidToken;
36
import org.dataone.service.exceptions.NotAuthorized;
37
import org.dataone.service.exceptions.NotFound;
38
import org.dataone.service.exceptions.NotImplemented;
39
import org.dataone.service.exceptions.ServiceFailure;
40
import org.dataone.service.exceptions.UnsupportedType;
41
import org.dataone.service.types.Event;
42
import org.dataone.service.types.Identifier;
43
import org.dataone.service.types.Log;
44
import org.dataone.service.types.NodeList;
45
import org.dataone.service.types.ObjectFormat;
46
import org.dataone.service.types.Session;
47
import org.dataone.service.types.SystemMetadata;
48
import org.joda.time.DateTime;
49

  
50
import edu.ucsb.nceas.metacat.EventLog;
51
import edu.ucsb.nceas.metacat.IdentifierManager;
52

  
53
/**
54
 * 
55
 * Implements DataONE MemberNode CRUD API for Metacat. 
56
 * 
57
 * @author Matthew Jones
58
 */
59
public class CNCoreImpl implements CNCore
60
{
61
    private static CNCoreImpl instance = null;
62

  
63
    private Logger logMetacat = null;
64
        
65
    /**
66
     * singleton accessor
67
     */
68
    public static CNCoreImpl getInstance() 
69
    {
70
      if (instance == null) {
71
        instance = new CNCoreImpl();
72
      }
73
      
74
      return instance;
75
    }
76
    
77
    /**
78
     * Constructor, private for singleton access
79
     */
80
    private CNCoreImpl() {
81
        logMetacat = Logger.getLogger(CNCoreImpl.class);
82
        
83
    }
84
    
85
    /**
86
     * register System Metadata
87
     */
88
    public boolean registerSystemMetaData(Session session, Identifier guid,
89
            SystemMetadata sysmeta) throws NotImplemented, NotAuthorized,
90
            ServiceFailure, InvalidRequest {
91
        logMetacat.debug("Starting CrudService.create()...");
92
        
93
        // TODO: control who can call this?
94
        if (session == null) {
95
            //TODO: many of the thrown exceptions do not use the correct error codes
96
            //check these against the docs and correct them
97
            throw new NotAuthorized("4861", "No Session - could not authorize to registration." +
98
                    "  If you are not logged in, please do so and retry the request.");
99
        }
100
        
101
        // verify that guid == SystemMetadata.getIdentifier()
102
        logMetacat.debug("Comparing guid|sysmeta_guid: " + guid.getValue() + "|" + sysmeta.getIdentifier().getValue());
103
        if (!guid.getValue().equals(sysmeta.getIdentifier().getValue())) {
104
            throw new InvalidRequest("4863", 
105
                "GUID in method call (" + guid.getValue() + ") does not match GUID in system metadata (" +
106
                sysmeta.getIdentifier().getValue() + ").");
107
        }
108

  
109
        logMetacat.debug("Checking if identifier exists...");
110
        // Check that the identifier does not already exist
111
        IdentifierManager im = IdentifierManager.getInstance();
112
        if (im.identifierExists(guid.getValue())) {
113
            throw new InvalidRequest("4863", 
114
                "GUID is already in use by an existing object.");
115
        }
116

  
117
        // insert the system metadata into the object store
118
        logMetacat.debug("Starting to insert SystemMetadata...");
119
        sysmeta.setDateSysMetadataModified(new Date());
120
        try {
121
			IdentifierManager.getInstance().createSystemMetadata(sysmeta);
122
			IdentifierManager.getInstance().updateSystemMetadata(sysmeta);
123
		} catch (Exception e) {
124
            throw new ServiceFailure("4862", "Error inserting system metadata: " + e.getClass() + ": " + e.getMessage());
125
		}
126
        
127
        logMetacat.debug("Returning from registerSystemMetadata");
128
        EventLog.getInstance().log(null, session.getSubject().getValue(), guid.getValue(), "registerSystemMetadata");
129
        return true;
130
    }
131

  
132
	@Override
133
	public ObjectFormat getFormat(Identifier arg0) throws InvalidRequest,
134
			ServiceFailure, NotFound, InsufficientResources, NotImplemented {
135
		throw new NotImplemented("4845", "Not implemented");
136
	}
137

  
138
	@Override
139
	public Log getLogRecords(DateTime arg0, DateTime arg1, Event arg2)
140
			throws InvalidToken, InvalidRequest, ServiceFailure, NotAuthorized,
141
			NotImplemented {
142
		throw new NotImplemented("1461", "Not implemented");
143

  
144
	}
145

  
146
	@Override
147
	public NodeList listNodes() throws NotImplemented, ServiceFailure {
148
		throw new NotImplemented("4800", "Not implemented");
149

  
150
	}
151
}
152

  
0 153

  

Also available in: Unified diff