Project

General

Profile

« Previous | Next » 

Revision 9146

Added by Jing Tao over 9 years ago

Add the code to support CNView interface in CNodeService. Both CNodeService and MNodeService share the same code base.

View differences:

src/edu/ucsb/nceas/metacat/dataone/MNodeService.java
2141 2141
		return bagInputStream;
2142 2142
	}
2143 2143

  
2144
	@Override
2145
	public OptionList listViews(Session arg0) throws InvalidToken,
2146
			ServiceFailure, NotAuthorized, InvalidRequest, NotImplemented {
2147
		OptionList views = new OptionList();
2148
		Vector<String> skinNames = null;
2149
		try {
2150
			skinNames = SkinUtil.getSkinNames();
2151
		} catch (PropertyNotFoundException e) {
2152
			throw new ServiceFailure("2841", e.getMessage());
2153
		}
2154
		for (String skinName: skinNames) {
2155
			views.addOption(skinName);
2156
		}
2157
		return views;
2158
	}
2159

  
2160
	@Override
2161
	public InputStream view(Session session, String format, Identifier pid)
2162
			throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest,
2163
			NotImplemented, NotFound {
2164
		InputStream resultInputStream = null;
2165
		
2166
		String serviceFailureCode = "2831";
2167
	    Identifier sid = getPIDForSID(pid, serviceFailureCode);
2168
	    if(sid != null) {
2169
	        pid = sid;
2170
	    }
2171
	    
2172
		SystemMetadata sysMeta = this.getSystemMetadata(session, pid);
2173
		InputStream object = this.get(session, pid);
2174

  
2175
		try {
2176
			// can only transform metadata, really
2177
			ObjectFormat objectFormat = ObjectFormatCache.getInstance().getFormat(sysMeta.getFormatId());
2178
			if (objectFormat.getFormatType().equals("METADATA")) {
2179
				// transform
2180
				DBTransform transformer = new DBTransform();
2181
	            String documentContent = IOUtils.toString(object, "UTF-8");
2182
	            String sourceType = objectFormat.getFormatId().getValue();
2183
	            String targetType = "-//W3C//HTML//EN";
2184
	            ByteArrayOutputStream baos = new ByteArrayOutputStream();
2185
	            Writer writer = new OutputStreamWriter(baos , "UTF-8");
2186
	            // TODO: include more params?
2187
	            Hashtable<String, String[]> params = new Hashtable<String, String[]>();
2188
	            String localId = null;
2189
				try {
2190
					localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
2191
				} catch (McdbDocNotFoundException e) {
2192
					throw new NotFound("1020", e.getMessage());
2193
				}
2194
	            params.put("qformat", new String[] {format});	            
2195
	            params.put("docid", new String[] {localId});
2196
	            params.put("pid", new String[] {pid.getValue()});
2197
	            transformer.transformXMLDocument(
2198
	                    documentContent , 
2199
	                    sourceType, 
2200
	                    targetType , 
2201
	                    format, 
2202
	                    writer, 
2203
	                    params, 
2204
	                    null //sessionid
2205
	                    );
2206
	            
2207
	            // finally, get the HTML back
2208
	            resultInputStream = new ContentTypeByteArrayInputStream(baos.toByteArray());
2209
	            ((ContentTypeByteArrayInputStream) resultInputStream).setContentType("text/html");
2210 2144
	
2211
			} else {
2212
				// just return the raw bytes
2213
				resultInputStream = object;
2214
			}
2215
		} catch (IOException e) {
2216
			// report as service failure
2217
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2218
			sf.initCause(e);
2219
			throw sf;
2220
		} catch (PropertyNotFoundException e) {
2221
			// report as service failure
2222
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2223
			sf.initCause(e);
2224
			throw sf;
2225
		} catch (SQLException e) {
2226
			// report as service failure
2227
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2228
			sf.initCause(e);
2229
			throw sf;
2230
		} catch (ClassNotFoundException e) {
2231
			// report as service failure
2232
			ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2233
			sf.initCause(e);
2234
			throw sf;
2235
		}
2236
		
2237
		return resultInputStream;
2238
	}	
2239 2145
    
2240 2146
}
src/edu/ucsb/nceas/metacat/dataone/CNodeService.java
42 42
import org.dataone.service.cn.v2.CNCore;
43 43
import org.dataone.service.cn.v2.CNRead;
44 44
import org.dataone.service.cn.v2.CNReplication;
45
import org.dataone.service.cn.v2.CNView;
45 46
import org.dataone.service.exceptions.BaseException;
46 47
import org.dataone.service.exceptions.IdentifierNotUnique;
47 48
import org.dataone.service.exceptions.InsufficientResources;
......
95 96
 *
96 97
 */
97 98
public class CNodeService extends D1NodeService implements CNAuthorization,
98
    CNCore, CNRead, CNReplication {
99
    CNCore, CNRead, CNReplication, CNView {
99 100

  
100 101
  /* the logger instance */
101 102
  private Logger logMetacat = null;
src/edu/ucsb/nceas/metacat/dataone/D1NodeService.java
23 23

  
24 24
package edu.ucsb.nceas.metacat.dataone;
25 25

  
26
import java.io.ByteArrayOutputStream;
26 27
import java.io.File;
27 28
import java.io.FileNotFoundException;
28 29
import java.io.FileOutputStream;
29 30
import java.io.IOException;
30 31
import java.io.InputStream;
31 32
import java.io.OutputStream;
33
import java.io.OutputStreamWriter;
34
import java.io.Writer;
32 35
import java.sql.SQLException;
33 36
import java.util.ArrayList;
34 37
import java.util.Calendar;
......
37 40
import java.util.List;
38 41
import java.util.Set;
39 42
import java.util.Timer;
43
import java.util.Vector;
40 44
import java.util.concurrent.locks.Lock;
41 45

  
42 46
import javax.servlet.http.HttpServletRequest;
......
65 69
import org.dataone.service.types.v1.ObjectList;
66 70
import org.dataone.service.types.v2.Log;
67 71
import org.dataone.service.types.v2.Node;
72
import org.dataone.service.types.v2.OptionList;
68 73
import org.dataone.service.types.v1.Event;
69 74
import org.dataone.service.types.v1.NodeReference;
70 75
import org.dataone.service.types.v1.NodeType;
......
79 84
import org.dataone.service.util.Constants;
80 85

  
81 86
import edu.ucsb.nceas.metacat.AccessionNumberException;
87
import edu.ucsb.nceas.metacat.DBTransform;
82 88
import edu.ucsb.nceas.metacat.DocumentImpl;
83 89
import edu.ucsb.nceas.metacat.EventLog;
84 90
import edu.ucsb.nceas.metacat.IdentifierManager;
85 91
import edu.ucsb.nceas.metacat.McdbDocNotFoundException;
86 92
import edu.ucsb.nceas.metacat.MetacatHandler;
87 93
import edu.ucsb.nceas.metacat.client.InsufficientKarmaException;
94
import edu.ucsb.nceas.metacat.common.query.stream.ContentTypeByteArrayInputStream;
88 95
import edu.ucsb.nceas.metacat.database.DBConnection;
89 96
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
90 97
import edu.ucsb.nceas.metacat.dataone.hazelcast.HazelcastService;
91 98
import edu.ucsb.nceas.metacat.index.MetacatSolrIndex;
92 99
import edu.ucsb.nceas.metacat.properties.PropertyService;
93 100
import edu.ucsb.nceas.metacat.replication.ForceReplicationHandler;
101
import edu.ucsb.nceas.metacat.util.SkinUtil;
94 102
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
95 103

  
96 104
public abstract class D1NodeService {
......
1911 1919
      return pass;
1912 1920
      
1913 1921
  }
1922
  
1923
  //@Override
1924
  public OptionList listViews(Session arg0) throws InvalidToken,
1925
          ServiceFailure, NotAuthorized, InvalidRequest, NotImplemented {
1926
      OptionList views = new OptionList();
1927
      Vector<String> skinNames = null;
1928
      try {
1929
          skinNames = SkinUtil.getSkinNames();
1930
      } catch (PropertyNotFoundException e) {
1931
          throw new ServiceFailure("2841", e.getMessage());
1932
      }
1933
      for (String skinName: skinNames) {
1934
          views.addOption(skinName);
1935
      }
1936
      return views;
1937
  }
1938
  
1939
  public OptionList listViews() throws InvalidToken,
1940
  ServiceFailure, NotAuthorized, InvalidRequest, NotImplemented { 
1941
      return listViews(null);
1942
  }
1914 1943

  
1944
  //@Override
1945
  public InputStream view(Session session, String format, Identifier pid)
1946
          throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest,
1947
          NotImplemented, NotFound {
1948
      InputStream resultInputStream = null;
1949
      
1950
      String serviceFailureCode = "2831";
1951
      Identifier sid = getPIDForSID(pid, serviceFailureCode);
1952
      if(sid != null) {
1953
          pid = sid;
1954
      }
1955
      
1956
      SystemMetadata sysMeta = this.getSystemMetadata(session, pid);
1957
      InputStream object = this.get(session, pid);
1958

  
1959
      try {
1960
          // can only transform metadata, really
1961
          ObjectFormat objectFormat = ObjectFormatCache.getInstance().getFormat(sysMeta.getFormatId());
1962
          if (objectFormat.getFormatType().equals("METADATA")) {
1963
              // transform
1964
              DBTransform transformer = new DBTransform();
1965
              String documentContent = IOUtils.toString(object, "UTF-8");
1966
              String sourceType = objectFormat.getFormatId().getValue();
1967
              String targetType = "-//W3C//HTML//EN";
1968
              ByteArrayOutputStream baos = new ByteArrayOutputStream();
1969
              Writer writer = new OutputStreamWriter(baos , "UTF-8");
1970
              // TODO: include more params?
1971
              Hashtable<String, String[]> params = new Hashtable<String, String[]>();
1972
              String localId = null;
1973
              try {
1974
                  localId = IdentifierManager.getInstance().getLocalId(pid.getValue());
1975
              } catch (McdbDocNotFoundException e) {
1976
                  throw new NotFound("1020", e.getMessage());
1977
              }
1978
              params.put("qformat", new String[] {format});               
1979
              params.put("docid", new String[] {localId});
1980
              params.put("pid", new String[] {pid.getValue()});
1981
              transformer.transformXMLDocument(
1982
                      documentContent , 
1983
                      sourceType, 
1984
                      targetType , 
1985
                      format, 
1986
                      writer, 
1987
                      params, 
1988
                      null //sessionid
1989
                      );
1990
              
1991
              // finally, get the HTML back
1992
              resultInputStream = new ContentTypeByteArrayInputStream(baos.toByteArray());
1993
              ((ContentTypeByteArrayInputStream) resultInputStream).setContentType("text/html");
1994
  
1995
          } else {
1996
              // just return the raw bytes
1997
              resultInputStream = object;
1998
          }
1999
      } catch (IOException e) {
2000
          // report as service failure
2001
          ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2002
          sf.initCause(e);
2003
          throw sf;
2004
      } catch (PropertyNotFoundException e) {
2005
          // report as service failure
2006
          ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2007
          sf.initCause(e);
2008
          throw sf;
2009
      } catch (SQLException e) {
2010
          // report as service failure
2011
          ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2012
          sf.initCause(e);
2013
          throw sf;
2014
      } catch (ClassNotFoundException e) {
2015
          // report as service failure
2016
          ServiceFailure sf = new ServiceFailure("1030", e.getMessage());
2017
          sf.initCause(e);
2018
          throw sf;
2019
      }
2020
      
2021
      return resultInputStream;
2022
  }   
2023

  
1915 2024
}

Also available in: Unified diff