Revision 5639
Added by berkley over 13 years ago
ResourceHandler.java | ||
---|---|---|
1062 | 1062 |
} |
1063 | 1063 |
*/ |
1064 | 1064 |
|
1065 |
/** |
|
1066 |
* write the parts of an MMP to files. The first file in the vector is |
|
1067 |
* a system metadata file, the 2nd is the object. |
|
1068 |
*/ |
|
1069 |
private static Hashtable<String, File> writeMMPPartsToFiles(InputStream is) |
|
1065 |
protected static String[] findBoundaryString(InputStream is) |
|
1070 | 1066 |
throws IOException |
1071 | 1067 |
{ |
1072 |
System.out.println("producing data"); |
|
1073 |
Logger logMetacat = Logger.getLogger(ResourceHandler.class); |
|
1074 |
|
|
1075 |
String[] searchStrings = {"boundary=", |
|
1076 |
"Content-Disposition: attachment; filename=systemmetadata", |
|
1077 |
"Content-Disposition: attachment; filename=object"}; |
|
1078 |
|
|
1068 |
String[] endResult = new String[2]; |
|
1079 | 1069 |
String boundary = ""; |
1080 |
String searchSegment = null; |
|
1070 |
String searchString = "boundary="; |
|
1071 |
boolean doneWithCurrentArray = false; |
|
1081 | 1072 |
byte[] b = new byte[1024]; |
1082 |
int numread = is.read(b, 0, 1024); |
|
1083 |
String s = new String(b, 0, numread); |
|
1084 |
int ssi = 0; |
|
1085 |
boolean doneWithCurrentArray = false; |
|
1086 |
boolean searchForBoundary = false; |
|
1087 |
boolean writeMarker = true; |
|
1088 |
int currentArrayIndex = 0; |
|
1089 |
|
|
1090 |
File tmpDir; |
|
1091 |
try |
|
1073 |
int numbytes = is.read(b, 0, 1024); |
|
1074 |
while(numbytes != -1) |
|
1092 | 1075 |
{ |
1093 |
tmpDir = new File(PropertyService.getProperty("application.tempDir")); |
|
1076 |
String s = new String(b, 0, numbytes); |
|
1077 |
|
|
1078 |
int[] result = StreamUtil.lookForMatch(searchString, s); |
|
1079 |
int searchStringIndex = s.indexOf(searchString); |
|
1080 |
if(s.indexOf("\"", searchStringIndex + searchString.length() + 1) == -1) |
|
1081 |
{ //the end of the boundary is in the next byte array |
|
1082 |
boundary = s.substring(searchStringIndex + searchString.length() + 1, s.length()); |
|
1083 |
} |
|
1084 |
else if(!boundary.startsWith("--")) |
|
1085 |
{ //we can read the whole boundary from this byte array |
|
1086 |
boundary = s.substring(searchStringIndex + searchString.length() + 1, |
|
1087 |
s.indexOf("\"", searchStringIndex + searchString.length() + 1)); |
|
1088 |
boundary = "--" + boundary; |
|
1089 |
endResult[0] = boundary; |
|
1090 |
endResult[1] = s.substring(s.indexOf("\"", searchStringIndex + searchString.length() + 1) + 1, |
|
1091 |
s.length()); |
|
1092 |
break; |
|
1093 |
} |
|
1094 |
else |
|
1095 |
{ //we're now reading the 2nd byte array to get the rest of the boundary |
|
1096 |
searchString = "\""; |
|
1097 |
searchStringIndex = s.indexOf(searchString); |
|
1098 |
boundary += s.substring(0, searchStringIndex); |
|
1099 |
boundary = "--" + boundary; |
|
1100 |
endResult[0] = boundary; |
|
1101 |
endResult[1] = s.substring(s.indexOf("\"", searchStringIndex + searchString.length() + 1) + 1, |
|
1102 |
s.length()); |
|
1103 |
break; |
|
1104 |
} |
|
1094 | 1105 |
} |
1095 |
catch(PropertyNotFoundException pnfe) |
|
1096 |
{ |
|
1097 |
logMetacat.error("ResourceHandler.writeMMPPartstoFiles: " + |
|
1098 |
"application.tmpDir not found. Using /tmp instead."); |
|
1099 |
tmpDir = new File("/tmp"); |
|
1106 |
System.out.println("boundary is: " + boundary); |
|
1107 |
return endResult; |
|
1108 |
} |
|
1109 |
|
|
1110 |
protected String writeMMPPartToFile(String beginSearch, |
|
1111 |
InputStream is, String boundary, String searchString, File f) |
|
1112 |
throws IOException |
|
1113 |
{ |
|
1114 |
String s = beginSearch; |
|
1115 |
FileOutputStream fos = new FileOutputStream(f); |
|
1116 |
int numread = 0; |
|
1117 |
byte[] b = new byte[1024]; |
|
1118 |
String writeString = ""; |
|
1119 |
|
|
1120 |
if(s == null) |
|
1121 |
{ //starting with the first part of the stream |
|
1122 |
numread = is.read(b, 0, 1024); |
|
1123 |
s = new String(b, 0, numread); |
|
1100 | 1124 |
} |
1101 |
long datetimetag = new Date().getTime(); |
|
1102 |
File smFile = new File(tmpDir, "sm." + datetimetag + ".tmp"); |
|
1103 |
File objFile = new File(tmpDir, "obj." + datetimetag + ".tmp"); |
|
1104 |
FileOutputStream smFos = new FileOutputStream(smFile); |
|
1105 |
FileOutputStream objFos = new FileOutputStream(objFile); |
|
1106 | 1125 |
|
1107 |
boolean writeSM = true; |
|
1126 |
boolean useCurrentS = true; |
|
1127 |
boolean searchForBoundary = false; |
|
1128 |
String seekString = searchString; |
|
1108 | 1129 |
|
1109 | 1130 |
while(numread != -1) |
1110 | 1131 |
{ |
1111 |
if(ssi == 2 && writeSM) |
|
1132 |
//System.out.println("////////////////////////iterating"); |
|
1133 |
//System.out.println("searchForBoundary: " + searchForBoundary); |
|
1134 |
if(searchForBoundary) |
|
1112 | 1135 |
{ |
1113 |
writeSM = false;
|
|
1136 |
seekString = boundary;
|
|
1114 | 1137 |
} |
1115 |
|
|
1116 |
String searchString; |
|
1117 |
if(!searchForBoundary) |
|
1118 |
{ |
|
1119 |
if(ssi >= searchStrings.length) |
|
1120 |
{ |
|
1121 |
break; |
|
1122 |
} |
|
1123 |
searchString = searchStrings[ssi]; |
|
1124 |
} |
|
1125 | 1138 |
else |
1126 | 1139 |
{ |
1127 |
searchString = boundary;
|
|
1140 |
seekString = searchString;
|
|
1128 | 1141 |
} |
1129 | 1142 |
|
1130 |
System.out.println("searchString: " + searchString); |
|
1131 |
System.out.println("done with array: " + doneWithCurrentArray); |
|
1132 |
System.out.println("searchForBoundary: " + searchForBoundary); |
|
1133 |
if(doneWithCurrentArray) |
|
1143 |
int[] result = StreamUtil.lookForMatch(seekString, s); |
|
1144 |
if(!useCurrentS) |
|
1134 | 1145 |
{ |
1135 |
System.out.println("Getting new chunk"); |
|
1136 | 1146 |
numread = is.read(b, 0, 1024); |
1137 |
s = new String(b, 0, numread); |
|
1147 |
if(numread != -1) |
|
1148 |
{ |
|
1149 |
s = new String(b, 0, numread); |
|
1150 |
} |
|
1151 |
else |
|
1152 |
{ |
|
1153 |
break; |
|
1154 |
} |
|
1138 | 1155 |
} |
1139 | 1156 |
|
1140 |
System.out.println("/////////////////current chunk (s): \n" + s + "\n////////////end current chunk"); |
|
1157 |
//System.out.println("searchString: " + searchString); |
|
1158 |
//System.out.println("seekString: " + seekString); |
|
1159 |
//System.out.println("in string: " + s); |
|
1141 | 1160 |
|
1142 |
int[] result = StreamUtil.lookForMatch(searchString, s); |
|
1143 |
System.out.println("got result from StreamUtil: " + result[0] + ", " + result[1]); |
|
1144 |
if(result[0] == -1 && result[1] == -1) |
|
1145 |
{ //same as else statement |
|
1146 |
if(searchForBoundary) |
|
1147 |
{ |
|
1148 |
//we're in between the start and end of a section |
|
1149 |
//so just write this to the stream |
|
1150 |
System.out.println("XXXX 4 writing string: " + s + "\nXXXX"); |
|
1151 |
//dataSink.write(s.getBytes()); |
|
1152 |
if(writeSM) |
|
1161 |
if(result[0] >= 0 && result[1] == seekString.length()) |
|
1162 |
{ |
|
1163 |
//searchString is full in s |
|
1164 |
if(!searchForBoundary) |
|
1165 |
{ //we're looking for searchString and we found it |
|
1166 |
//chop off the searchString itself and start writing |
|
1167 |
//until we find boundary |
|
1168 |
s = s.substring(result[0] + result[1], s.length()); |
|
1169 |
if(s.length() > 0) |
|
1153 | 1170 |
{ |
1154 |
smFos.write(s.getBytes());
|
|
1171 |
useCurrentS = true;
|
|
1155 | 1172 |
} |
1156 | 1173 |
else |
1157 | 1174 |
{ |
1158 |
objFos.write(s.getBytes());
|
|
1175 |
useCurrentS = false;
|
|
1159 | 1176 |
} |
1177 |
searchForBoundary = true; |
|
1160 | 1178 |
} |
1161 |
} |
|
1162 |
else if(result[0] != -1 && result[1] == searchString.length()) |
|
1163 |
{ //we have the whole search string in this group of bytes |
|
1164 |
System.out.println("1"); |
|
1165 |
if(searchForBoundary) |
|
1166 |
{ |
|
1167 |
System.out.println("2"); |
|
1168 |
//find the boundary, chop it off the end and inc ssi |
|
1169 |
String strToWrite = s.substring(0, s.indexOf(boundary, currentArrayIndex)); |
|
1170 |
System.out.println("XXXX 1 writing string: " + strToWrite + "\nXXXX"); |
|
1179 |
else |
|
1180 |
{ //we're writing, but we found the boundary in this chunk |
|
1171 | 1181 |
|
1172 |
//dataSink.write(strToWrite.getBytes()); |
|
1173 |
if(writeSM) |
|
1174 |
{ |
|
1175 |
smFos.write(strToWrite.getBytes()); |
|
1176 |
} |
|
1177 |
else |
|
1178 |
{ |
|
1179 |
objFos.write(strToWrite.getBytes()); |
|
1180 |
} |
|
1181 |
doneWithCurrentArray = false; |
|
1182 |
currentArrayIndex = s.indexOf(boundary, currentArrayIndex) + 1; |
|
1183 |
searchForBoundary = false; |
|
1184 |
ssi++; |
|
1182 |
writeString = s.substring(0, result[0]); |
|
1183 |
fos.write(writeString.getBytes()); |
|
1184 |
//we're done. break and return; |
|
1185 |
return s.substring(result[0] + result[1], s.length()); |
|
1185 | 1186 |
} |
1186 |
else if(ssi == 0) |
|
1187 |
{ //we're looking for the boundary marker |
|
1188 |
int searchStringIndex = s.indexOf(searchString); |
|
1189 |
if(s.indexOf("\"", searchStringIndex + searchString.length() + 1) == -1) |
|
1190 |
{ //the end of the boundary is in the next byte array |
|
1191 |
boundary = s.substring(searchStringIndex + searchString.length() + 1, s.length()); |
|
1192 |
doneWithCurrentArray = true; |
|
1193 |
} |
|
1194 |
else if(!boundary.startsWith("--")) |
|
1195 |
{ //we can read the whole boundary from this byte array |
|
1196 |
boundary = s.substring(searchStringIndex + searchString.length() + 1, |
|
1197 |
s.indexOf("\"", searchStringIndex + searchString.length() + 1)); |
|
1198 |
boundary = "--" + boundary; |
|
1199 |
ssi++; |
|
1200 |
doneWithCurrentArray = false; |
|
1201 |
} |
|
1202 |
else |
|
1203 |
{ //we're now reading the 2nd byte array to get the rest of the boundary |
|
1204 |
searchString = "\""; |
|
1205 |
searchStringIndex = s.indexOf(searchString); |
|
1206 |
boundary += s.substring(0, searchStringIndex); |
|
1207 |
boundary = "--" + boundary; |
|
1208 |
ssi++; |
|
1209 |
doneWithCurrentArray = false; |
|
1210 |
} |
|
1211 |
System.out.println("boundary: " + boundary); |
|
1212 |
} |
|
1213 |
else if(ssi == 1 || ssi == 2) |
|
1214 |
{ |
|
1215 |
int searchStringIndex = s.indexOf(searchString); |
|
1216 |
//find the beginning of the system metadata or data object and |
|
1217 |
//start writing it to the dataSink |
|
1218 |
int smBeginIndex = s.indexOf(searchString) + searchString.length() + 2; |
|
1219 |
searchForBoundary = true; |
|
1220 |
int boundaryIndex = s.indexOf(boundary, smBeginIndex); |
|
1221 |
int end = s.length(); |
|
1222 |
if(boundaryIndex != -1) |
|
1223 |
{ |
|
1224 |
end = boundaryIndex; |
|
1225 |
ssi++; |
|
1226 |
searchForBoundary = false; |
|
1227 |
} |
|
1228 |
currentArrayIndex = end + 1; |
|
1229 |
String strToWrite = s.substring(smBeginIndex, end); |
|
1230 |
System.out.println("XXXX 2 writing string: " + strToWrite + "\nXXXX"); |
|
1231 |
|
|
1232 |
//dataSink.write(strToWrite.getBytes()); |
|
1233 |
if(writeSM) |
|
1234 |
{ |
|
1235 |
smFos.write(strToWrite.getBytes()); |
|
1236 |
} |
|
1237 |
else |
|
1238 |
{ |
|
1239 |
objFos.write(strToWrite.getBytes()); |
|
1240 |
} |
|
1241 |
|
|
1242 |
if(end == s.length()) |
|
1243 |
{ |
|
1244 |
System.out.println("DONE WITH ARRAY"); |
|
1245 |
doneWithCurrentArray = true; |
|
1246 |
currentArrayIndex = 0; |
|
1247 |
} |
|
1248 |
else |
|
1249 |
{ |
|
1250 |
System.out.println("NOT DONE WITH ARRAY"); |
|
1251 |
doneWithCurrentArray = false; |
|
1252 |
} |
|
1253 |
} |
|
1254 | 1187 |
} |
1255 |
else if(result[1] != searchString.length() && result[0] != 0) |
|
1256 |
{ //part of the search string is on the end of this byte group |
|
1257 |
doneWithCurrentArray = true; |
|
1258 |
searchForBoundary = false; |
|
1259 |
searchSegment = s.substring(result[0], s.length()); |
|
1260 |
System.out.println("searchSegment1: " + searchSegment); |
|
1188 |
else if(result[0] > 0 && result[1] != seekString.length()) |
|
1189 |
{ |
|
1190 |
//seekString is partially in s |
|
1191 |
//more specifically, the beginning of seekString is at the end |
|
1192 |
//of s |
|
1193 |
|
|
1194 |
//get the next chunk right now, see if the beginning matches |
|
1195 |
numread = is.read(b, 0, 1024); |
|
1196 |
String s2 = new String(b, 0, numread); |
|
1197 |
s += s2; |
|
1198 |
useCurrentS = false; |
|
1261 | 1199 |
} |
1262 |
else if(result[1] != searchString.length() && result[0] == 0) |
|
1263 |
{ //we have the end of the searchString at the beginning of this byte group |
|
1264 |
if(searchSegment != null) |
|
1265 |
{ |
|
1266 |
searchSegment += s.substring(0, result[1]); |
|
1267 |
System.out.println("searchSegment2: " + searchSegment); |
|
1268 |
searchForBoundary = true; |
|
1269 |
doneWithCurrentArray = true; |
|
1270 |
ssi++; |
|
1271 |
String strToWrite = s.substring(result[1] + 1, s.length()); |
|
1272 |
System.out.println("XXXX 3 writing string: " + strToWrite + "\nXXXX"); |
|
1273 |
|
|
1274 |
//dataSink.write(strToWrite.getBytes()); |
|
1275 |
if(writeSM) |
|
1276 |
{ |
|
1277 |
smFos.write(strToWrite.getBytes()); |
|
1278 |
} |
|
1279 |
else |
|
1280 |
{ |
|
1281 |
objFos.write(strToWrite.getBytes()); |
|
1282 |
} |
|
1283 |
} |
|
1284 |
} |
|
1285 | 1200 |
else |
1286 | 1201 |
{ |
1202 |
//searchString is not in s |
|
1287 | 1203 |
if(searchForBoundary) |
1288 | 1204 |
{ |
1289 |
//we're in between the start and end of a section |
|
1290 |
//so just write this to the stream |
|
1291 |
System.out.println("XXXX 4 writing string: " + s + "\nXXXX"); |
|
1292 |
//dataSink.write(s.getBytes()); |
|
1293 |
if(writeSM) |
|
1294 |
{ |
|
1295 |
smFos.write(s.getBytes()); |
|
1296 |
} |
|
1297 |
else |
|
1298 |
{ |
|
1299 |
objFos.write(s.getBytes()); |
|
1300 |
} |
|
1205 |
fos.write(s.getBytes()); |
|
1301 | 1206 |
} |
1207 |
numread = is.read(b, 0, 1024); |
|
1208 |
if(numread != -1) |
|
1209 |
{ |
|
1210 |
s = new String(b, 0, numread); |
|
1211 |
} |
|
1212 |
else |
|
1213 |
{ |
|
1214 |
break; |
|
1215 |
} |
|
1302 | 1216 |
} |
1303 | 1217 |
} |
1218 |
return ""; |
|
1219 |
} |
|
1220 |
|
|
1221 |
protected Hashtable<String, File> writeMMPPartsToFiles(InputStream is) |
|
1222 |
throws IOException |
|
1223 |
{ |
|
1224 |
String[] boundaryResults = findBoundaryString(is); |
|
1225 |
String boundary = boundaryResults[0]; |
|
1226 |
String s = boundaryResults[1]; |
|
1227 |
String[] searchStrings = { |
|
1228 |
"Content-Disposition: attachment; filename=systemmetadata\n\n", |
|
1229 |
"Content-Disposition: attachment; filename=object\n\n"}; |
|
1304 | 1230 |
|
1231 |
File[] fileArr = getMMPTempFiles(); |
|
1305 | 1232 |
Hashtable<String, File> h = new Hashtable<String, File>(); |
1306 |
objFos.flush(); |
|
1307 |
smFos.flush(); |
|
1308 |
h.put("sysmeta", smFile); |
|
1309 |
h.put("object", objFile); |
|
1233 |
System.out.println("==========================Looking for SM"); |
|
1234 |
s = writeMMPPartToFile(s, is, boundary, searchStrings[0], fileArr[0]); |
|
1235 |
System.out.println("==========================Looking for Object"); |
|
1236 |
writeMMPPartToFile(s, is, boundary, searchStrings[1], fileArr[1]); |
|
1237 |
h.put("sysmeta", fileArr[0]); |
|
1238 |
h.put("object", fileArr[1]); |
|
1310 | 1239 |
return h; |
1311 | 1240 |
} |
1312 | 1241 |
|
1242 |
private static File[] getMMPTempFiles() |
|
1243 |
throws IOException |
|
1244 |
{ |
|
1245 |
Logger logMetacat = Logger.getLogger(ResourceHandler.class); |
|
1246 |
File tmpDir; |
|
1247 |
try |
|
1248 |
{ |
|
1249 |
tmpDir = new File(PropertyService.getProperty("application.tempDir")); |
|
1250 |
} |
|
1251 |
catch(PropertyNotFoundException pnfe) |
|
1252 |
{ |
|
1253 |
logMetacat.error("ResourceHandler.writeMMPPartstoFiles: " + |
|
1254 |
"application.tmpDir not found. Using /tmp instead."); |
|
1255 |
tmpDir = new File("/tmp"); |
|
1256 |
} |
|
1257 |
long datetimetag = new Date().getTime(); |
|
1258 |
File smFile = new File(tmpDir, "sm." + datetimetag + ".tmp"); |
|
1259 |
File objFile = new File(tmpDir, "obj." + datetimetag + ".tmp"); |
|
1260 |
File[] fileArr = {smFile, objFile}; |
|
1261 |
return fileArr; |
|
1262 |
} |
|
1263 |
|
|
1313 | 1264 |
/** |
1314 | 1265 |
* return a vector where the first element is a string that represents the system |
1315 | 1266 |
* metadata and the 2nd element is an InputStream that is the object |
Also available in: Unified diff
still working on mmp stream parsing