Project

General

Profile

« Previous | Next » 

Revision 9033

Added by Jing Tao about 10 years ago

Refactor the code to check a pid exists in the v1 api.

View differences:

src/edu/ucsb/nceas/metacat/dataone/v1/MNodeService.java
273 273
	@Override
274 274
	public InputStream get(Identifier pid) throws InvalidToken, NotAuthorized,
275 275
			NotImplemented, ServiceFailure, NotFound, InsufficientResources {
276
	    boolean exists = false;
277
	    try {
278
	        exists = IdentifierManager.getInstance().systemMetadataPIDExists(pid);
279
	    } catch (SQLException e) {
280
	        throw new ServiceFailure("1030", "The object specified by "+ pid.getValue()+
281
                    " couldn't be identified if it exists at this node since "+e.getMessage());
282
	    }
283
	    if(!exists) {
284
	        //the v1 method only handles a pid.
285
	        throw new NotFound("1020", "The object specified by "+pid.getValue()+" does not exist at this node");
286
	    }
276
	    String serviceFailure = "1030";
277
        String notFound = "1020";
278
        impl.checkV1SystemMetaPidExist(pid, serviceFailure, notFound);
287 279
		return impl.get(null, pid);
288 280
	}
289 281

  
......
292 284
	public InputStream get(Session session, Identifier pid) throws InvalidToken,
293 285
			NotAuthorized, NotImplemented, ServiceFailure, NotFound,
294 286
			InsufficientResources {
295
	    boolean exists = false;
296
        try {
297
            exists = IdentifierManager.getInstance().systemMetadataPIDExists(pid);
298
        } catch (SQLException e) {
299
            throw new ServiceFailure("1030", "The object specified by "+ pid.getValue()+
300
                    " couldn't be identified if it exists at this node since "+e.getMessage());
301
        }
302
        if(!exists) {
303
            //the v1 method only handles a pid.
304
            throw new NotFound("1020", "The object specified by "+pid.getValue()+" does not exist at this node");
305
        }
287
	    String serviceFailure = "1030";
288
        String notFound = "1020";
289
        impl.checkV1SystemMetaPidExist(pid, serviceFailure, notFound);
306 290
		return impl.get(session, pid);
307 291
	}
308 292

  
src/edu/ucsb/nceas/metacat/dataone/v1/CNodeService.java
208 208
	@Override
209 209
	public InputStream get(Identifier pid) throws InvalidToken,
210 210
			ServiceFailure, NotAuthorized, NotFound, NotImplemented {
211
	    boolean exists = false;
212
        try {
213
            exists = IdentifierManager.getInstance().systemMetadataPIDExists(pid);
214
        } catch (SQLException e) {
215
            throw new ServiceFailure("1030", "The object specified by "+ pid.getValue()+
216
                    " couldn't be identified if it exists at this node since "+e.getMessage());
217
        }
218
        if(!exists) {
219
          //the v1 method only handles a pid.
220
            throw new NotFound("1020", "The object specified by "+pid.getValue()+" does not exist at this node");
221
        }
211
        String serviceFailure = "1030";
212
        String notFound = "1020";
213
        impl.checkV1SystemMetaPidExist(pid, serviceFailure, notFound);
222 214
		return impl.get(null, pid);
223 215
	}
224 216

  
......
226 218
	@Deprecated
227 219
	public InputStream get(Session session, Identifier pid) throws InvalidToken,
228 220
			ServiceFailure, NotAuthorized, NotFound, NotImplemented {
229
	    boolean exists = false;
230
        try {
231
            exists = IdentifierManager.getInstance().systemMetadataPIDExists(pid);
232
        } catch (SQLException e) {
233
            throw new ServiceFailure("1030", "The object specified by "+ pid.getValue()+
234
                    " couldn't be identified if it exists at this node since "+e.getMessage());
235
        }
236
        if(!exists) {
237
          //the v1 method only handles a pid.
238
            throw new NotFound("1020", "The object specified by "+pid.getValue()+" does not exist at this node");
239
        }
221
	    String serviceFailure = "1030";
222
        String notFound = "1020";
223
        impl.checkV1SystemMetaPidExist(pid, serviceFailure, notFound);
240 224
		return impl.get(session, pid);
241 225
	}
242 226

  
src/edu/ucsb/nceas/metacat/dataone/D1NodeService.java
561 561
    throws InvalidToken, ServiceFailure, NotAuthorized, NotFound, 
562 562
    NotImplemented {
563 563
    
564
    try {
565
        //determine if the given pid is a sid or not.
566
        if(IdentifierManager.getInstance().systemMetadataSIDExists(pid)) {
567
            try {
568
                //set the header pid for the sid if the identifier is a sid.
569
                pid = IdentifierManager.getInstance().getHeadPID(pid);
570
            } catch (SQLException sqle) {
571
                throw new ServiceFailure("1030", "The current pid associated with the sid "+ pid.getValue()+
572
                        " couldn't be identified at this node since "+sqle.getMessage());
573
            }
574
            
575
        }
576
    } catch (SQLException e) {
577
        throw new ServiceFailure("1030", "The object specified by "+ pid.getValue()+
578
                " couldn't be identified at this node since "+e.getMessage());
564
    String serviceFailureCode = "1030";
565
    Identifier sid = getPIDForSID(pid, serviceFailureCode);
566
    if(sid != null) {
567
        pid = sid;
579 568
    }
580 569
    
581 570
    InputStream inputStream = null; // bytes to be returned
......
1675 1664

  
1676 1665
      return pid;
1677 1666
  }
1667
  
1668
  
1669
  /**
1670
   * A utility method for v1 api to check the specified identifier exists as a pid
1671
   * @param identifier  the specified identifier
1672
   * @param serviceFailureCode  the detail error code for the service failure exception
1673
   * @param noFoundCode  the detail error code for the not found exception
1674
   * @throws ServiceFailure
1675
   * @throws NotFound
1676
   */
1677
  public void checkV1SystemMetaPidExist(Identifier identifier, String serviceFailureCode, String noFoundCode) throws ServiceFailure, NotFound {
1678
      boolean exists = false;
1679
      try {
1680
          exists = IdentifierManager.getInstance().systemMetadataPIDExists(identifier);
1681
      } catch (SQLException e) {
1682
          throw new ServiceFailure(serviceFailureCode, "The object specified by "+ identifier.getValue()+
1683
                  " couldn't be identified if it exists at this node since "+e.getMessage());
1684
      }
1685
      if(!exists) {
1686
        //the v1 method only handles a pid.
1687
          throw new NotFound(noFoundCode, "The object specified by "+identifier.getValue()+" does not exist at this node");
1688
      }
1689
  }
1690
  
1691
  /**
1692
   * Utility method to get the PID for an SID. If the specified identifier is not an SID
1693
   * , null will be returned.
1694
   * @param sid  the specified sid
1695
   * @param serviceFailureCode  the detail error code for the service failure exception
1696
   * @return the pid for the sid. If the specified identifier is not an SID, null will be returned.
1697
   * @throws ServiceFailure
1698
   */
1699
  protected Identifier getPIDForSID(Identifier sid, String serviceFailureCode) throws ServiceFailure {
1700
      Identifier id = null;
1701
      try {
1702
          //determine if the given pid is a sid or not.
1703
          if(IdentifierManager.getInstance().systemMetadataSIDExists(sid)) {
1704
              try {
1705
                  //set the header pid for the sid if the identifier is a sid.
1706
                  id = IdentifierManager.getInstance().getHeadPID(sid);
1707
              } catch (SQLException sqle) {
1708
                  throw new ServiceFailure(serviceFailureCode, "The current pid associated with the sid "+ sid.getValue()+
1709
                          " couldn't be identified at this node since "+sqle.getMessage());
1710
              }
1711
              
1712
          }
1713
      } catch (SQLException e) {
1714
          throw new ServiceFailure(serviceFailureCode, "The object specified by "+ sid.getValue()+
1715
                  " couldn't be identified at this node since "+e.getMessage());
1716
      }
1717
      return id;
1718
  }
1678 1719

  
1679 1720

  
1680 1721
}

Also available in: Unified diff