Project

General

Profile

Revision 6592

Added by Chris Jones over 9 years ago

Handle calls to CNReplication REST services. Modify handle() to field calls to /replicaPolicies, /replicaMetadata, /replicaAuthorizations, and /replicaNotifications. Add the isNodeAuthorized(), setReplicationPolicy(), setReplicationStatus(), and updateReplicationMetadata() methods to parse and pass multipart form data and params on to the CNodeService implementing class.
The CNreplication additions also include support for the serialVersion parameter that was added to some of the API calls. Likewise, setAccess() and owner() were modified to handle the serialVersion param too.

View differences:

src/edu/ucsb/nceas/metacat/restservice/CNResourceHandler.java
56 56
import org.dataone.service.types.v1.Event;
57 57
import org.dataone.service.types.v1.Identifier;
58 58
import org.dataone.service.types.v1.Log;
59
import org.dataone.service.types.v1.NodeReference;
59 60
import org.dataone.service.types.v1.ObjectFormat;
60 61
import org.dataone.service.types.v1.ObjectFormatIdentifier;
61 62
import org.dataone.service.types.v1.ObjectFormatList;
62 63
import org.dataone.service.types.v1.ObjectLocationList;
63 64
import org.dataone.service.types.v1.Permission;
65
import org.dataone.service.types.v1.Replica;
66
import org.dataone.service.types.v1.ReplicationPolicy;
67
import org.dataone.service.types.v1.ReplicationStatus;
68
import org.dataone.service.types.v1.Session;
64 69
import org.dataone.service.types.v1.Subject;
65 70
import org.dataone.service.types.v1.SystemMetadata;
66 71
import org.dataone.service.util.DateTimeMarshaller;
......
99 104
	CNIdentity - not implemented at all on Metacat
100 105
	
101 106
	CNReplication
102
		setReplicationStatus() - Not exposed
103
		updateReplicationMetadata() - New method?
104
		setReplicationPolicy() - Not exposed
107
		setReplicationStatus() - PUT /replicationNotifications/PID
108
		updateReplicationMetadata() - PUT /replicationMetadata/PID
109
		setReplicationPolicy() - PUT /replicationPolicies/PID
110
		isNodeAuthorized() - GET /replicationAuthorizations/PID
105 111
	
106 112
	CNRegister -- not implemented at all in Metacat
107 113
 * ******************
......
116 122
    protected static final String RESOURCE_RESOLVE = "resolve";
117 123
    protected static final String RESOURCE_ASSERT_RELATION = "assertRelation";
118 124
    protected static final String RESOURCE_OWNER = "owner";
119
    protected static final String RESOURCE_NOTIFY = "notify";
120
    protected static final String RESOURCE_META_REPLICATION = "meta/replication";
121
    protected static final String RESOURCE_META_POLICY = "meta/policy";
125
    protected static final String RESOURCE_REPLICATION_POLICY = "replicaPolicies";
126
    protected static final String RESOURCE_REPLICATION_META = "replicaMetadata";
127
    protected static final String RESOURCE_REPLICATION_AUTHORIZED = "replicaAuthorizations";
128
    protected static final String RESOURCE_REPLICATION_NOTIFY = "replicaNotifications";
122 129
	
123 130
    public CNResourceHandler(ServletContext servletContext,
124 131
			HttpServletRequest request, HttpServletResponse response) {
......
280 287
                    
281 288
                        checksum(extra);
282 289
                        status = true;
290
                        
283 291
                    }
292
                
293
                } else if ( resource.startsWith(RESOURCE_REPLICATION_POLICY) && 
294
                        httpVerb == PUT) {
295
                    
296
                    logMetacat.debug("Using resource: " + RESOURCE_REPLICATION_POLICY);
297
                    // get the trailing pid
298
                    extra = parseTrailing(resource, RESOURCE_REPLICATION_POLICY);
299
                    setReplicationPolicy(extra);
300
                    status = true;
301

  
302
                } else if ( resource.startsWith(RESOURCE_REPLICATION_META) && 
303
                        httpVerb == PUT) {
304
                    
305
                    logMetacat.debug("Using resource: " + RESOURCE_REPLICATION_META);
306
                    // get the trailing pid
307
                    extra = parseTrailing(resource, RESOURCE_REPLICATION_META);
308
                    updateReplicationMetadata(extra);
309
                    status = true;
310

  
311
                } else if ( resource.startsWith(RESOURCE_REPLICATION_NOTIFY) && 
312
                        httpVerb == PUT ) {
313
                    
314
                    logMetacat.debug("Using resource: " + RESOURCE_REPLICATION_NOTIFY);
315
                    // get the trailing pid
316
                    extra = parseTrailing(resource, RESOURCE_REPLICATION_NOTIFY);
317
                    setReplicationStatus(extra);
318
                    status = true;
319
                    
320
                } else if ( resource.startsWith(RESOURCE_REPLICATION_AUTHORIZED) 
321
                        && httpVerb == GET) {
322
                    
323
                    logMetacat.debug("Using resource: " + RESOURCE_REPLICATION_AUTHORIZED);
324
                    // get the trailing pid
325
                    extra = parseTrailing(resource, RESOURCE_REPLICATION_AUTHORIZED);
326
                    isNodeAuthorized(extra);
327
                    status = true;
328
                    
284 329
                } 
285 330
                    
286 331
                if (!status) {
......
698 743
     * @throws IllegalAccessException 
699 744
     * @throws InstantiationException 
700 745
     */
701
    private void owner(String id) throws JiBXException, InvalidToken, ServiceFailure, NotFound, NotAuthorized, NotImplemented, InvalidRequest, IOException, InstantiationException, IllegalAccessException {
702
		Identifier pid = new Identifier();
703
		pid.setValue(id);
704
		String subjectStr = params.get("subject")[0];
705
		Subject subject = TypeMarshaller.unmarshalTypeFromStream(Subject.class, new ByteArrayInputStream(subjectStr.getBytes("UTF-8")));
706
		Identifier retPid = CNodeService.getInstance(request).setOwner(session, pid, subject);
707
		OutputStream out = response.getOutputStream();
708
		response.setStatus(200);
709
		response.setContentType("text/xml");
710
		TypeMarshaller.marshalTypeToOutputStream(retPid, out);		
746
    private void owner(String id) 
747
        throws JiBXException, InvalidToken, ServiceFailure, 
748
        NotFound, NotAuthorized, NotImplemented, InvalidRequest, IOException, 
749
        InstantiationException, IllegalAccessException {
750
		
751
        Identifier pid = new Identifier();
752
		    pid.setValue(id);
753

  
754
        long serialVersion = 0L;
755
        String serialVersionStr = null;
756
        
757
        // get the serialVersion
758
        try {
759
            serialVersionStr = params.get("serialVersion")[0];
760
            serialVersion = new Long(serialVersionStr).longValue();
761
            
762
        } catch (NullPointerException e) {
763
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
764
            logMetacat.error(msg);
765
            throw new InvalidRequest("4442", msg);
766
            
767
        }		    
768
		    
769
		    // get the subject
770
		    String subjectStr = params.get("subject")[0];
771
		    Subject subject = TypeMarshaller.unmarshalTypeFromStream(Subject.class, new ByteArrayInputStream(subjectStr.getBytes("UTF-8")));
772
		    
773
		    Identifier retPid = CNodeService.getInstance(request).setOwner(session, pid, subject, serialVersion);
774
		    OutputStream out = response.getOutputStream();
775
		    response.setStatus(200);
776
		    response.setContentType("text/xml");
777
		    TypeMarshaller.marshalTypeToOutputStream(retPid, out);		
711 778
    }
712 779
    
713 780
    /**
......
777 844
     * @throws SAXException 
778 845
     * @throws ParserConfigurationException 
779 846
     */
780
    protected void setAccess(String pid) throws JiBXException, InvalidToken, ServiceFailure, NotFound, NotAuthorized, NotImplemented, InvalidRequest, IOException, InstantiationException, IllegalAccessException, ParserConfigurationException, SAXException {
847
    protected void setAccess(String pid) 
848
        throws JiBXException, InvalidToken, ServiceFailure, NotFound, 
849
        NotAuthorized, NotImplemented, InvalidRequest, IOException, 
850
        InstantiationException, IllegalAccessException, ParserConfigurationException, 
851
        SAXException {
781 852

  
853
        long serialVersion = 0L;
854
        String serialVersionStr = null;
855
        
856
        // get the serialVersion
857
        try {
858
            serialVersionStr = params.get("serialVersion")[0];
859
            serialVersion = new Long(serialVersionStr).longValue();
860
            
861
        } catch (NullPointerException e) {
862
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
863
            logMetacat.error(msg);
864
            throw new InvalidRequest("4402", msg);
865
            
866
        }
867
        
782 868
        Identifier id = new Identifier();
783 869
        id.setValue(pid);
784 870
        
785 871
        AccessPolicy accessPolicy = collectAccessPolicy();
786
        CNodeService.getInstance(request).setAccessPolicy(session, id, accessPolicy);
872
        CNodeService.getInstance(request).setAccessPolicy(session, id, accessPolicy, serialVersion);
787 873

  
788 874
    }
789 875
    
......
797 883
     * @throws InvalidToken 
798 884
     * @throws Exception
799 885
     */
800
    private void query() throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest, NotImplemented {
886
    private void query() 
887
        throws InvalidToken, ServiceFailure, NotAuthorized, InvalidRequest, NotImplemented {
801 888
        
802
    	String query = null;
803
		String queryType = null;
804
		try {
805
			query = params.get("query")[0];
806
		} catch (Exception e) {
807
			logMetacat.warn("query not specified");
808
		}
809
		try {
810
			String qt = params.get("queryType")[0];
811
			queryType = qt;
812
		} catch (Exception e) {
813
			logMetacat.warn("queryType not specified");
814
		}
815
		
816
    	// expecting to throw NotImplemented
817
		CNodeService.getInstance(request).search(session, queryType, query);
889
    	  String query = null;
890
		    String queryType = null;
891
		    try {
892
		    	query = params.get("query")[0];
893
		    } catch (Exception e) {
894
		    	logMetacat.warn("query not specified");
895
		    }
896
		    try {
897
		    	String qt = params.get("queryType")[0];
898
		    	queryType = qt;
899
		    } catch (Exception e) {
900
		    	logMetacat.warn("queryType not specified");
901
		    }
902
		    
903
        	// expecting to throw NotImplemented
904
		    CNodeService.getInstance(request).search(session, queryType, query);
818 905
    }
906
    
907
    /**
908
     * Pass the request to get node replication authorization to CNodeService
909
     * 
910
     * @param pid  the identifier of the object to get authorization to replicate
911
     * 
912
     * @throws NotImplemented
913
     * @throws NotAuthorized
914
     * @throws InvalidToken
915
     * @throws ServiceFailure
916
     * @throws NotFound
917
     * @throws InvalidRequest
918
     */
919
    public boolean isNodeAuthorized(String pid) 
920
        throws NotImplemented, NotAuthorized, InvalidToken, ServiceFailure, 
921
        NotFound, InvalidRequest {
922
        
923
        boolean result = false;
924
        Subject targetNodeSubject = new Subject();
925
        String nodeSubject = null;
926
        Permission permission = null;
927
        String replPermission = null;
928
        
929
        // get the pid
930
        Identifier identifier = new Identifier();
931
        identifier.setValue(pid);
932
        
933
        // get the target node subject
934
        try {
935
            nodeSubject = params.get("targetNodeSubject")[0];
936
            targetNodeSubject.setValue(nodeSubject);
937
            
938
        } catch (NullPointerException e) {
939
            String msg = "The 'targetNodeSubject' must be provided as a parameter and was not.";
940
            logMetacat.error(msg);
941
            throw new InvalidRequest("4873", msg);
942
            
943
        }
944
        
945
        // get the permission
946
        try {
947
            replPermission = params.get("replicatePermission")[0];
948
            permission = Permission.convert(replPermission);
819 949

  
950
        } catch (NullPointerException e) {
951
            String msg = "The 'replicatePermission' must be provided as a parameter and was not.";
952
            logMetacat.error(msg);
953
            throw new InvalidRequest("4873", msg);
954

  
955
        }
956
        
957
        result =
958
            CNodeService.getInstance(request).isNodeAuthorized(session, targetNodeSubject, identifier, permission);
959
        
960
        response.setStatus(200);
961
        response.setContentType("text/xml");
962
        return result;
963
        
964
    }
965
    
966
    /**
967
     * Pass the request to set the replication policy to CNodeService
968
     * 
969
     * @param pid  the identifier of the object to set the replication policy on
970
     * 
971
     * @throws NotImplemented
972
     * @throws NotFound
973
     * @throws NotAuthorized
974
     * @throws ServiceFailure
975
     * @throws InvalidRequest
976
     * @throws InvalidToken
977
     * @throws IOException
978
     * @throws InstantiationException
979
     * @throws IllegalAccessException
980
     * @throws JiBXException
981
     */
982
    public boolean setReplicationPolicy(String pid) 
983
        throws NotImplemented, NotFound, NotAuthorized, ServiceFailure, 
984
        InvalidRequest, InvalidToken, IOException, InstantiationException, 
985
        IllegalAccessException, JiBXException {
986
        
987
        boolean result = false;
988
        ReplicationPolicy policy = null;
989
        long serialVersion = 0L;
990
        String serialVersionStr = null;
991
       
992
        Identifier identifier = new Identifier();
993
        identifier.setValue(pid);
994
        
995
        // get the serialVersion
996
        try {
997
            serialVersionStr = params.get("serialVersion")[0];
998
            serialVersion = new Long(serialVersionStr).longValue();
999
            
1000
        } catch (NullPointerException e) {
1001
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
1002
            logMetacat.error(msg);
1003
            throw new InvalidRequest("4883", msg);
1004
            
1005
        }
1006
        policy = collectReplicationPolicy();
1007
        result = 
1008
            CNodeService.getInstance(request).setReplicationPolicy(session, identifier, policy, serialVersion);
1009
        response.setStatus(200);
1010
        response.setContentType("text/xml");
1011
        return result;
1012
        
1013
    }
1014
    
1015
    /**
1016
     * Pass the request to set the replication status to CNodeService
1017
     * 
1018
     * @param pid  the identifier of the object to set the replication status on
1019
     * 
1020
     * @throws ServiceFailure
1021
     * @throws NotImplemented
1022
     * @throws InvalidToken
1023
     * @throws NotAuthorized
1024
     * @throws InvalidRequest
1025
     * @throws NotFound
1026
     */
1027
    public boolean setReplicationStatus(String pid) 
1028
        throws ServiceFailure, NotImplemented, InvalidToken, NotAuthorized, 
1029
        InvalidRequest, NotFound {
1030

  
1031
        boolean result = false;
1032
        Identifier identifier = new Identifier();
1033
        identifier.setValue(pid);
1034
        long serialVersion = 0L;
1035
        String serialVersionStr = null;
1036
        ReplicationStatus status = null;
1037
        String replicationStatus = null;
1038
        NodeReference targetNodeRef = null;
1039
        String targetNode = null;
1040
        
1041
        // get the serialVersion
1042
        try {
1043
            serialVersionStr = params.get("serialVersion")[0];
1044
            serialVersion = new Long(serialVersionStr).longValue();
1045
            
1046
        } catch (NullPointerException e) {
1047
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
1048
            logMetacat.error(msg);
1049
            throw new InvalidRequest("4730", msg);
1050
            
1051
        }
1052
        
1053
        // get the replication status param
1054
        try {
1055
            replicationStatus = params.get("status")[0];
1056
            status = ReplicationStatus.convert(replicationStatus);
1057
        } catch (Exception e) {
1058
            // TODO Auto-generated catch block
1059
            e.printStackTrace();
1060
        }
1061
        
1062
        // get the target node reference param
1063
        targetNode = params.get("nodeRef")[0];
1064
        targetNodeRef = new NodeReference();
1065
        targetNodeRef.setValue(targetNode);
1066
        
1067
        result = 
1068
            CNodeService.getInstance(request).setReplicationStatus(session, identifier, targetNodeRef, status, serialVersion);       
1069
        response.setStatus(200);
1070
        response.setContentType("text/xml");
1071
        return result;
1072
        
1073
    }
1074
    
1075
    /**
1076
     * Pass the request to update the replication metadata to CNodeService
1077
     * 
1078
     * @param pid  the identifier of the object to update the replication metadata on
1079
     * 
1080
     * @throws ServiceFailure
1081
     * @throws NotImplemented
1082
     * @throws InvalidToken
1083
     * @throws NotAuthorized
1084
     * @throws InvalidRequest
1085
     * @throws NotFound
1086
     */
1087
    public boolean updateReplicationMetadata(String pid) 
1088
        throws ServiceFailure, NotImplemented, InvalidToken, NotAuthorized, 
1089
        InvalidRequest, NotFound {
1090

  
1091
        boolean result = false;
1092
        long serialVersion = 0L;
1093
        String serialVersionStr = null;
1094
        Replica replica = null;
1095
        Identifier identifier = new Identifier();
1096
        identifier.setValue(pid);
1097

  
1098
        // get the serialVersion
1099
        try {
1100
            serialVersionStr = params.get("serialVersion")[0];
1101
            serialVersion = new Long(serialVersionStr).longValue();
1102
            
1103
        } catch (NullPointerException e) {
1104
            String msg = "The 'serialVersion' must be provided as a parameter and was not.";
1105
            logMetacat.error(msg);
1106
            throw new InvalidRequest("4853", msg);
1107
            
1108
        }
1109

  
1110
        replica = collectReplicaMetadata();
1111
        result = 
1112
            CNodeService.getInstance(request).updateReplicationMetadata(session, identifier, replica, serialVersion);
1113
        response.setStatus(200);
1114
        response.setContentType("text/xml");
1115
        return result;
1116

  
1117
    }
1118

  
820 1119
}

Also available in: Unified diff