Revision 5679
Added by berkley over 13 years ago
CrudService.java | ||
---|---|---|
24 | 24 |
|
25 | 25 |
import java.io.ByteArrayOutputStream; |
26 | 26 |
import java.io.File; |
27 |
import java.io.FileInputStream; |
|
27 | 28 |
import java.io.FileNotFoundException; |
28 | 29 |
import java.io.FileOutputStream; |
29 | 30 |
import java.io.IOException; |
... | ... | |
968 | 969 |
|
969 | 970 |
// Look up the localId for this global identifier |
970 | 971 |
IdentifierManager im = IdentifierManager.getInstance(); |
971 |
try { |
|
972 |
InputStream objectStream; |
|
973 |
try |
|
974 |
{ |
|
972 | 975 |
final String localId = im.getLocalId(guid.getValue()); |
973 | 976 |
|
974 |
final InputStreamFromOutputStream<String> objectStream = |
|
975 |
new InputStreamFromOutputStream<String>() { |
|
977 |
try |
|
978 |
{ |
|
979 |
String username = "public"; |
|
980 |
String[] groups = new String[0]; |
|
981 |
if(sessionData != null) |
|
982 |
{ |
|
983 |
username = sessionData.getUserName(); |
|
984 |
groups = sessionData.getGroupNames(); |
|
985 |
} |
|
976 | 986 |
|
977 |
@Override |
|
978 |
public String produce(final OutputStream dataSink) throws Exception { |
|
979 |
|
|
980 |
try { |
|
981 |
String username = "public"; |
|
982 |
String[] groups = new String[0]; |
|
983 |
if(sessionData != null) |
|
984 |
{ |
|
985 |
username = sessionData.getUserName(); |
|
986 |
groups = sessionData.getGroupNames(); |
|
987 |
} |
|
988 |
/*System.out.println("metacatUrl: " + metacatUrl + |
|
989 |
" dataSink: " + dataSink + " localId: " + localId + |
|
990 |
" username: " + username + " params: " + params.toString()); |
|
991 |
*/ |
|
992 |
/* TODO: |
|
993 |
* This multithreaded approach to getting data without |
|
994 |
* being memory bound causes problems with exception |
|
995 |
* handling. The only exception the produce method |
|
996 |
* will return is an IOException, rendering all of the |
|
997 |
* catch blocks below mute. This should probably be changed |
|
998 |
* to use a memory mapped solution instead so that we |
|
999 |
* can properly pass exceptions to the client. |
|
1000 |
* see https://trac.dataone.org/ticket/706 |
|
1001 |
*/ |
|
1002 |
handler.readFromMetacat(metacatUrl, null, |
|
1003 |
dataSink, localId, "xml", |
|
1004 |
username, |
|
1005 |
groups, true, params); |
|
1006 |
} catch (PropertyNotFoundException e) { |
|
1007 |
e.printStackTrace(); |
|
1008 |
throw new ServiceFailure("1030", "Error getting property from metacat: " + e.getMessage()); |
|
1009 |
} catch (ClassNotFoundException e) { |
|
1010 |
e.printStackTrace(); |
|
1011 |
throw new ServiceFailure("1030", "Class not found error when reading from metacat: " + e.getMessage()); |
|
1012 |
} catch (IOException e) { |
|
1013 |
e.printStackTrace(); |
|
1014 |
throw new ServiceFailure("1030", "IOException while reading from metacat: " + e.getMessage()); |
|
1015 |
} catch (SQLException e) { |
|
1016 |
e.printStackTrace(); |
|
1017 |
throw new ServiceFailure("1030", "SQLException while reading from metacat: " + e.getMessage()); |
|
1018 |
} catch (McdbException e) { |
|
1019 |
e.printStackTrace(); |
|
1020 |
throw new ServiceFailure("1030", "Metacat DB exception while reading from metacat: " + e.getMessage()); |
|
1021 |
} catch (ParseLSIDException e) { |
|
1022 |
e.printStackTrace(); |
|
1023 |
throw new NotFound("1020", "LSID parsing exception while reading from metacat: " + e.getMessage()); |
|
1024 |
} catch (InsufficientKarmaException e) { |
|
1025 |
e.printStackTrace(); |
|
1026 |
throw new NotAuthorized("1000", "User not authorized for get(): " + e.getMessage()); |
|
1027 |
} |
|
1028 |
|
|
1029 |
return "Completed"; |
|
987 |
File tmpDir; |
|
988 |
try |
|
989 |
{ |
|
990 |
tmpDir = new File(PropertyService.getProperty("application.tempDir")); |
|
1030 | 991 |
} |
1031 |
}; |
|
992 |
catch(PropertyNotFoundException pnfe) |
|
993 |
{ |
|
994 |
logMetacat.error("ResourceHandler.writeMMPPartstoFiles: " + |
|
995 |
"application.tmpDir not found. Using /tmp instead."); |
|
996 |
tmpDir = new File("/tmp"); |
|
997 |
} |
|
998 |
Date d = new Date(); |
|
999 |
File outputFile = new File(tmpDir, "metacat.output." + d.getTime()); |
|
1000 |
FileOutputStream dataSink = new FileOutputStream(outputFile); |
|
1001 |
|
|
1002 |
handler.readFromMetacat(metacatUrl, null, |
|
1003 |
dataSink, localId, "xml", |
|
1004 |
username, |
|
1005 |
groups, true, params); |
|
1006 |
|
|
1007 |
objectStream = new FileInputStream(outputFile); |
|
1008 |
|
|
1009 |
} catch (PropertyNotFoundException e) { |
|
1010 |
e.printStackTrace(); |
|
1011 |
throw new ServiceFailure("1030", "Error getting property from metacat: " + e.getMessage()); |
|
1012 |
} catch (ClassNotFoundException e) { |
|
1013 |
e.printStackTrace(); |
|
1014 |
throw new ServiceFailure("1030", "Class not found error when reading from metacat: " + e.getMessage()); |
|
1015 |
} catch (IOException e) { |
|
1016 |
e.printStackTrace(); |
|
1017 |
throw new ServiceFailure("1030", "IOException while reading from metacat: " + e.getMessage()); |
|
1018 |
} catch (SQLException e) { |
|
1019 |
e.printStackTrace(); |
|
1020 |
throw new ServiceFailure("1030", "SQLException while reading from metacat: " + e.getMessage()); |
|
1021 |
} catch (McdbException e) { |
|
1022 |
e.printStackTrace(); |
|
1023 |
throw new ServiceFailure("1030", "Metacat DB exception while reading from metacat: " + e.getMessage()); |
|
1024 |
} catch (ParseLSIDException e) { |
|
1025 |
e.printStackTrace(); |
|
1026 |
throw new NotFound("1020", "LSID parsing exception while reading from metacat: " + e.getMessage()); |
|
1027 |
} catch (InsufficientKarmaException e) { |
|
1028 |
e.printStackTrace(); |
|
1029 |
throw new NotAuthorized("1000", "User not authorized for get(): " + e.getMessage()); |
|
1030 |
} |
|
1031 |
|
|
1032 |
|
|
1032 | 1033 |
String username = "public"; |
1033 | 1034 |
if(sessionData != null) |
1034 | 1035 |
{ |
... | ... | |
1040 | 1041 |
logCrud.info("get D1GUID:" + guid.getValue() + ":D1SCIMETADATA:" + localId + |
1041 | 1042 |
":"); |
1042 | 1043 |
return objectStream; |
1043 |
|
|
1044 |
} catch (McdbDocNotFoundException e) { |
|
1044 |
} |
|
1045 |
catch (McdbDocNotFoundException e) |
|
1046 |
{ |
|
1045 | 1047 |
throw new NotFound("1020", e.getMessage()); |
1046 | 1048 |
} |
1047 | 1049 |
} |
Also available in: Unified diff
removed dependency on InputStreamToOutputStream so that exceptions will be passed correctly.