Revision 5826
Added by berkley almost 14 years ago
src/edu/ucsb/nceas/metacat/restservice/ResourceHandler.java | ||
---|---|---|
42 | 42 |
import org.apache.commons.io.IOUtils; |
43 | 43 |
import org.apache.log4j.Logger; |
44 | 44 |
import org.apache.maven.artifact.ant.shaded.IOUtil; |
45 |
import org.dataone.mimemultipart.MultipartResponseHandler; |
|
45 | 46 |
import org.dataone.service.NodeListParser; |
46 | 47 |
import org.dataone.service.exceptions.BaseException; |
47 | 48 |
import org.dataone.service.exceptions.IdentifierNotUnique; |
... | ... | |
78 | 79 |
import org.dataone.service.streaming.util.StreamUtil; |
79 | 80 |
|
80 | 81 |
import com.gc.iotools.stream.is.InputStreamFromOutputStream; |
82 |
import com.oreilly.servlet.multipart.FilePart; |
|
83 |
import com.oreilly.servlet.multipart.MultipartParser; |
|
84 |
import com.oreilly.servlet.multipart.ParamPart; |
|
85 |
import com.oreilly.servlet.multipart.Part; |
|
81 | 86 |
/** |
82 | 87 |
* |
83 | 88 |
* Implements Earthgrid REST API to Metacat <br/><br/> |
... | ... | |
586 | 591 |
} else if(resource.equals(RESOURCE_REPLICATE)) { |
587 | 592 |
System.out.println("processing replicate request"); |
588 | 593 |
|
589 |
if(httpVerb == GET)
|
|
594 |
if(httpVerb == POST)
|
|
590 | 595 |
{ |
591 |
String pathInfo = request.getPathInfo();
|
|
592 |
pathInfo = pathInfo.substring(1);
|
|
593 |
System.out.println("pathInfo: " + pathInfo);
|
|
594 |
String guid = pathInfo.substring(pathInfo.indexOf("/") + 1, pathInfo.length());
|
|
596 |
InputStream is = request.getInputStream();
|
|
597 |
/*String input = IOUtils.toString(is);
|
|
598 |
System.out.println("input: " + input);
|
|
599 |
is = IOUtils.toInputStream(input);*/
|
|
595 | 600 |
|
601 |
MultipartResponseHandler mpHandler = |
|
602 |
new MultipartResponseHandler(request, new File("/tmp/sysmeta")); |
|
603 |
Hashtable parts = mpHandler.getParts(); |
|
604 |
File f = (File)parts.get("sysmeta"); |
|
605 |
String sourceNode = (String)parts.get("sourceNode"); |
|
606 |
FileInputStream fis = new FileInputStream(f); |
|
607 |
String s; |
|
608 |
s = "sysmeta: " + IOUtils.toString(fis); |
|
609 |
s += "\n\n"; |
|
610 |
|
|
611 |
|
|
612 |
|
|
596 | 613 |
//get the system metadata from the request |
597 |
Hashtable<String, File> mmpFiles = writeMMPPartsToFiles(request.getInputStream()); |
|
614 |
|
|
615 |
/*String[] searchStrings = { |
|
616 |
"Content-Disposition: attachment; name=sysmeta\n\n", |
|
617 |
"Content-Disposition: form-data; name=sourceNode\n\n"}; |
|
618 |
String[] names = { |
|
619 |
"systemmetadata", |
|
620 |
"sourceNode" |
|
621 |
}; |
|
622 |
InputStream is = request.getInputStream(); |
|
623 |
//Hashtable<String,File> mmpFiles = writeMMPPartsToFiles(is, searchStrings, names); |
|
624 |
String s = IOUtils.toString(is); |
|
625 |
System.out.println("s: " + s);*/ |
|
626 |
/*int startIndex = s.indexOf(searchStrings[0]); |
|
627 |
startIndex = s.indexOf("\n\n", startIndex); |
|
628 |
int endIndex = s.indexOf("\n--", startIndex); |
|
629 |
System.out.println("startIndex: " + startIndex); |
|
630 |
System.out.println("endIndex: " + endIndex); |
|
631 |
String smStr = s.substring(startIndex, endIndex); |
|
632 |
System.out.println("smStr: " + smStr); |
|
598 | 633 |
SystemMetadata sm = (SystemMetadata) |
599 |
deserializeServiceType(SystemMetadata.class, |
|
600 |
new FileInputStream(mmpFiles.get("systemmetadata"))); |
|
601 |
String nodeId = sm.getAuthoritativeMemberNode().getValue(); |
|
634 |
deserializeServiceType(SystemMetadata.class, new ByteArrayInputStream(smStr.getBytes())); |
|
635 |
//String nodeId = sm.getAuthoritativeMemberNode().getValue(); |
|
636 |
startIndex = s.indexOf(searchStrings[1], endIndex); |
|
637 |
endIndex = s.indexOf("\n--", startIndex); |
|
638 |
String sourceNode = s.substring(startIndex, endIndex); |
|
639 |
System.out.println("sourceNode: " + sourceNode); |
|
602 | 640 |
//lookup nodeId from the registry |
603 | 641 |
URL url = new URL("http://cn.dataone.org/cn/node"); |
604 |
InputStream is = url.openStream(); |
|
605 |
Map<String,String> m = NodeListParser.parseNodeListFile(is); |
|
606 |
String nodeUrl = m.get(nodeId); |
|
642 |
InputStream urlis = url.openStream(); |
|
643 |
Map<String,String> m = NodeListParser.parseNodeListFile(urlis); |
|
644 |
String nodeUrl = m.get(sourceNode); |
|
645 |
System.out.println("nodeUrl: " + nodeUrl); |
|
607 | 646 |
if(nodeUrl == null) |
608 | 647 |
{ |
609 | 648 |
response.setStatus(500); |
610 | 649 |
response.getOutputStream().write(("Member Node id " + |
611 |
nodeId + " not found in node registry.").getBytes());
|
|
650 |
sourceNode + " not found in node registry.").getBytes());
|
|
612 | 651 |
response.getOutputStream().close(); |
613 | 652 |
} |
614 | 653 |
|
615 | 654 |
//we at least have enough information to continue so |
616 | 655 |
//tell the cn we're ok |
617 |
//respond to cn with 200/OK |
|
656 |
//respond to cn with 200/OK*/
|
|
618 | 657 |
response.setStatus(200); |
619 |
response.getOutputStream().write("OK".getBytes()); |
|
658 |
response.getOutputStream().write(("sourceNode: " + sourceNode + "\n\n").getBytes()); |
|
659 |
response.getOutputStream().write(("s: " + s).getBytes()); |
|
620 | 660 |
response.getOutputStream().close(); |
621 | 661 |
|
622 | 662 |
//get the document |
... | ... | |
1411 | 1451 |
boolean doneWithCurrentArray = false; |
1412 | 1452 |
byte[] b = new byte[1024]; |
1413 | 1453 |
int numbytes = is.read(b, 0, 1024); |
1454 |
|
|
1414 | 1455 |
while(numbytes != -1) |
1415 | 1456 |
{ |
1416 | 1457 |
String s = new String(b, 0, numbytes); |
1458 |
int searchStringIndex = s.indexOf(searchString); |
|
1417 | 1459 |
|
1418 |
int[] result = StreamUtil.lookForMatch(searchString, s); |
|
1419 |
int searchStringIndex = s.indexOf(searchString); |
|
1420 | 1460 |
if(s.indexOf("\"", searchStringIndex + searchString.length() + 1) == -1) |
1421 | 1461 |
{ //the end of the boundary is in the next byte array |
1422 | 1462 |
boundary = s.substring(searchStringIndex + searchString.length() + 1, s.length()); |
... | ... | |
1443 | 1483 |
break; |
1444 | 1484 |
} |
1445 | 1485 |
} |
1446 |
System.out.println("boundary is: " + boundary);
|
|
1486 |
System.out.println("boundary is: '" + boundary + "'");
|
|
1447 | 1487 |
return endResult; |
1448 | 1488 |
} |
1449 | 1489 |
|
... | ... | |
1619 | 1659 |
{ |
1620 | 1660 |
Logger logMetacat = Logger.getLogger(ResourceHandler.class); |
1621 | 1661 |
logMetacat.info("Processing Mime Multipart"); |
1662 |
//String isStr = IOUtils.toString(is); |
|
1663 |
//System.out.println("isStr: " + isStr); |
|
1664 |
//is = IOUtils.toInputStream(isStr); |
|
1622 | 1665 |
String[] boundaryResults = findBoundaryString(is); |
1623 | 1666 |
String boundary = boundaryResults[0]; |
1624 | 1667 |
String s = boundaryResults[1]; |
1625 | 1668 |
|
1626 | 1669 |
|
1627 |
File[] fileArr = getMMPTempFiles(searchStrings); |
|
1670 |
File[] fileArr = getMMPTempFiles(searchStrings, names);
|
|
1628 | 1671 |
Hashtable<String, File> h = new Hashtable<String, File>(); |
1629 | 1672 |
for(int i=0; i<searchStrings.length; i++) |
1630 | 1673 |
{ |
1631 | 1674 |
System.out.println("searchStrings: " + searchStrings[i]); |
1632 | 1675 |
System.out.println("file: " + fileArr[0].getAbsolutePath()); |
1633 | 1676 |
logMetacat.info("writing mime part " + searchStrings[i] + " to " + fileArr[0].getAbsolutePath()); |
1677 |
System.out.println("s: " + s); |
|
1678 |
System.out.println("is: " + is); |
|
1679 |
|
|
1634 | 1680 |
s = writeMMPPartToFile(s.trim(), is, boundary, searchStrings[i], fileArr[i]); |
1635 | 1681 |
logMetacat.info("writeMMPPartToFile returned '" + s.trim() + "' after processing part " + searchStrings[i]); |
1636 | 1682 |
h.put(names[i], fileArr[i]); |
... | ... | |
1639 | 1685 |
return h; |
1640 | 1686 |
} |
1641 | 1687 |
|
1642 |
private static File[] getMMPTempFiles(String[] searchStrings) |
|
1688 |
private static File[] getMMPTempFiles(String[] searchStrings, String[] names)
|
|
1643 | 1689 |
throws IOException |
1644 | 1690 |
{ |
1645 | 1691 |
Logger logMetacat = Logger.getLogger(ResourceHandler.class); |
... | ... | |
1658 | 1704 |
File[] fileArr = new File[searchStrings.length]; |
1659 | 1705 |
for(int i=0; i<searchStrings.length; i++) |
1660 | 1706 |
{ |
1661 |
File f = new File(tmpDir, searchStrings[i] + "." + datetimetag + ".tmp");
|
|
1707 |
File f = new File(tmpDir, names[i] + "." + datetimetag + ".tmp");
|
|
1662 | 1708 |
fileArr[i] = f; |
1663 | 1709 |
} |
1664 | 1710 |
|
Also available in: Unified diff
new mmp code