|
From: <bob...@us...> - 2003-12-15 09:51:38
|
Update of /cvsroot/ebxmlms/ebxmlms/src/hk/hku/cecid/phoenix/message/handler
In directory sc8-pr-cvs1:/tmp/cvs-serv20508/src/hk/hku/cecid/phoenix/message/handler
Modified Files:
HttpSender.java MessageServer.java
Log Message:
Take out soap envelope parsing logic from
Message.getMessageFromDataSource() to form
another method parseSoapEnvelopeOnly().
Change Http.send() to return a EbxmlMessage rather than a soap message
Store the byte array of soap envelope when received my Http and Mail,
and also change the signature verification logic to read those byte array.
It will fix the bug on signature verification of a non "soap-env" namespace
message received from Sync Reply channel.
Index: HttpSender.java
===================================================================
RCS file: /cvsroot/ebxmlms/ebxmlms/src/hk/hku/cecid/phoenix/message/handler/HttpSender.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -d -r1.16 -r1.17
*** HttpSender.java 11 Dec 2003 06:41:29 -0000 1.16
--- HttpSender.java 15 Dec 2003 09:51:34 -0000 1.17
***************
*** 135,144 ****
//final SOAPMessage soapMessage = HttpServlet.
// send(ebxmlMessage.getSOAPMessage(), toMshUrl.toString());
! final SOAPMessage soapMessage = Http.
! send(ebxmlMessage, toMshUrl.toString());
successful = true;
if (soapMessage != null) {
responseMessage = new EbxmlMessage(soapMessage);
! }
}
catch (TransportException te) {
--- 135,144 ----
//final SOAPMessage soapMessage = HttpServlet.
// send(ebxmlMessage.getSOAPMessage(), toMshUrl.toString());
! responseMessage = Http.send(ebxmlMessage, toMshUrl.toString());
successful = true;
+ /*
if (soapMessage != null) {
responseMessage = new EbxmlMessage(soapMessage);
! }*/
}
catch (TransportException te) {
***************
*** 146,149 ****
--- 146,150 ----
exceptionMessage = te.getMessage();
}
+ /*
catch (SOAPException e) {
exceptionMessage = ErrorMessages.getMessage
***************
*** 152,155 ****
--- 153,157 ----
logger.error(exceptionMessage);
}
+ */
catch (Exception e) {
exceptionMessage = ErrorMessages.getMessage
Index: MessageServer.java
===================================================================
RCS file: /cvsroot/ebxmlms/ebxmlms/src/hk/hku/cecid/phoenix/message/handler/MessageServer.java,v
retrieving revision 1.143
retrieving revision 1.144
diff -C2 -d -r1.143 -r1.144
*** MessageServer.java 11 Dec 2003 06:41:29 -0000 1.143
--- MessageServer.java 15 Dec 2003 09:51:34 -0000 1.144
***************
*** 3542,3546 ****
* @param file the file that contain the message.
* @return the ebxmlmessage deserialized from the file.
! * @throws MessageServerException Description of the Exception
*/
static EbxmlMessage getMessageFromFile(File file)
--- 3542,3546 ----
* @param file the file that contain the message.
* @return the ebxmlmessage deserialized from the file.
! * @throws MessageServerException throw if there is error on parsing
*/
static EbxmlMessage getMessageFromFile(File file)
***************
*** 3548,3551 ****
--- 3548,3600 ----
return (EbxmlMessage) getMessageFromFile(file, true);
}
+
+ /**
+ * parse the message from InputStream and get the byte array of SOAP
+ * Envelope
+ * @param stream the InputStream contains the message
+ * @return the byte array of the Soap Envelope
+ * @throws MessageServerException throw if there is error on parsing
+ */
+ public static byte[] getSoapEnvelopeBytesFromStream(InputStream stream)
+ throws MessageServerException {
+ InputStream pStream = null;
+ try {
+ MessageSemiParsedOutput parsedOutput
+ = parseSoapEnvelopeOnly(stream);
+ pStream = parsedOutput.getInputStream();
+ return parsedOutput.getSoapMessageBytes();
+ } catch (IOException e) {
+ e.printStackTrace();
+ String err = ErrorMessages.getMessage(
+ ErrorMessages.ERR_HERMES_UNKNOWN_ERROR, e);
+ logger.error(err);
+ throw new MessageServerException(err);
+ } catch (MessageServerException e) {
+ throw e;
+ } finally {
+ if (pStream != null) {
+ try {
+ pStream.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ String err = ErrorMessages.getMessage(
+ ErrorMessages.ERR_HERMES_UNKNOWN_ERROR, e);
+ logger.error(err);
+ throw new MessageServerException(err);
+ }
+ }
+ if (stream != null) {
+ try {
+ stream.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ String err = ErrorMessages.getMessage(
+ ErrorMessages.ERR_HERMES_UNKNOWN_ERROR, e);
+ logger.error(err);
+ throw new MessageServerException(err);
+ }
+ }
+ }
+ }
/**
***************
*** 3597,3684 ****
try {
fis = dataSource.getInputStream();
! fileStream = new PushbackInputStream(fis);
! String boundary = null;
! int soapMessageFileOffset = 0;
! int soapMessageLength = 0;
! ByteArrayOutputStream out = new ByteArrayOutputStream();
! byte[] line = readLine(fileStream);
! long offset = line.length;
! String s = new String(line);
! while (line.length > 0 &&
! !(s.startsWith(Constants.MIME_BOUNDARY_PREFIX))) {
! out.write(line);
! line = readLine(fileStream);
! offset += line.length;
! s = new String(line);
! }
! byte[] soapMessageBytes = out.toByteArray();
! soapMessageLength = soapMessageBytes.length;
! int lastIndex = soapMessageBytes.length - 1;
!
! if (line.length > 0) {
! lastIndex = line.length - 1;
! for (; lastIndex >= 0; lastIndex--) {
! if (line[lastIndex] != 0xA && line[lastIndex] != 0xD) {
! break;
! }
! }
! boundary = new String(line,
! Constants.MIME_BOUNDARY_PREFIX.length(),
! lastIndex - Constants.MIME_BOUNDARY_PREFIX.length() + 1);
!
! /*
! * Find the empty line delimiter separating the MIME header and
! * the SOAPPart content
! */
! line = readLine(fileStream);
! offset += line.length;
! while (line.length > 0 && line[0] != 0xA && line[0] != 0xD) {
! line = readLine(fileStream);
! offset += line.length;
! }
! if (line.length == 0) {
! String err = ErrorMessages.getMessage(
! ErrorMessages.ERR_HERMES_FILE_IO_ERROR,
! "missing empty line delimiter of MIME header");
! logger.warn(err);
! throw new MessageServerException(err);
! }
!
! /*
! * Find the location and the length of the SOAPPart content
! * with offset being the beginning position
! */
! soapMessageFileOffset = (int) offset;
! out = new ByteArrayOutputStream();
! line = readLine(fileStream);
! offset += line.length;
! s = new String(line);
! while (line.length > 0 && !(s.startsWith
! (Constants.MIME_BOUNDARY_PREFIX + boundary))) {
! out.write(line);
! line = readLine(fileStream);
! offset += line.length;
! s = new String(line);
! }
!
! if (line.length == 0) {
! String err = ErrorMessages.getMessage(
! ErrorMessages.ERR_HERMES_FILE_IO_ERROR,
! "missing ending MIME boundary");
! logger.warn(err);
! throw new MessageServerException(err);
! }
!
! soapMessageBytes = out.toByteArray();
! soapMessageLength = soapMessageBytes.length;
! lastIndex = soapMessageBytes.length - 1;
! for (; lastIndex >= 0; lastIndex--) {
! if (soapMessageBytes[lastIndex] != 0xA &&
! soapMessageBytes[lastIndex] != 0xD) {
! break;
! }
! }
! }
!
final MimeHeaders headers = new MimeHeaders();
headers.addHeader(Constants.CONTENT_TYPE, Constants.TEXT_XML_TYPE);
--- 3646,3656 ----
try {
fis = dataSource.getInputStream();
! MessageSemiParsedOutput parsedOutput = parseSoapEnvelopeOnly(fis);
! fileStream = parsedOutput.getInputStream();
! int soapMessageFileOffset = parsedOutput.getSoapMessageOffset();
! byte[] soapMessageBytes = parsedOutput.getSoapMessageBytes();
! int soapMessageLength = soapMessageBytes.length;
! int lastIndex = parsedOutput.getLastIndex();
! String boundary = parsedOutput.getBoundary();
final MimeHeaders headers = new MimeHeaders();
headers.addHeader(Constants.CONTENT_TYPE, Constants.TEXT_XML_TYPE);
***************
*** 3697,3700 ****
--- 3669,3674 ----
ArrayList payloads = new ArrayList();
if (boundary != null && withAttachments) {
+ byte[] line = parsedOutput.getLastLine();
+ long offset = parsedOutput.getOffset();
lastIndex = line.length - 1;
for (; lastIndex >= 0; lastIndex--) {
***************
*** 3703,3707 ****
}
}
! s = new String(line, 0, lastIndex + 1);
while (!s.endsWith(boundary + Constants.MIME_BOUNDARY_PREFIX)) {
--- 3677,3681 ----
}
}
! String s = new String(line, 0, lastIndex + 1);
while (!s.endsWith(boundary + Constants.MIME_BOUNDARY_PREFIX)) {
***************
*** 4122,4125 ****
--- 4096,4263 ----
public static boolean isDelivered(int sequenceNumber) {
return sequenceNumber <= FIRST_MESSAGE_DELIVERED;
+ }
+
+ /**
+ * The function which parse the soap envelope only from the inputstream.
+ */
+ private static MessageSemiParsedOutput parseSoapEnvelopeOnly(
+ InputStream fis) throws IOException, MessageServerException {
+ PushbackInputStream fileStream = new PushbackInputStream(fis);
+ String boundary = null;
+ int soapMessageFileOffset = 0;
+ int soapMessageLength = 0;
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ byte[] line = readLine(fileStream);
+ long offset = line.length;
+ String s = new String(line);
+ while (line.length > 0 &&
+ !(s.startsWith(Constants.MIME_BOUNDARY_PREFIX))) {
+ out.write(line);
+ line = readLine(fileStream);
+ offset += line.length;
+ s = new String(line);
+ }
+ byte[] soapMessageBytes = out.toByteArray();
+ soapMessageLength = soapMessageBytes.length;
+ int lastIndex = soapMessageBytes.length - 1;
+
+ if (line.length > 0) {
+ lastIndex = line.length - 1;
+ for (; lastIndex >= 0; lastIndex--) {
+ if (line[lastIndex] != 0xA && line[lastIndex] != 0xD) {
+ break;
+ }
+ }
+ boundary = new String(line,
+ Constants.MIME_BOUNDARY_PREFIX.length(),
+ lastIndex - Constants.MIME_BOUNDARY_PREFIX.length() + 1);
+
+ /*
+ * Find the empty line delimiter separating the MIME header and
+ * the SOAPPart content
+ */
+ line = readLine(fileStream);
+ offset += line.length;
+ while (line.length > 0 && line[0] != 0xA && line[0] != 0xD) {
+ line = readLine(fileStream);
+ offset += line.length;
+ }
+ if (line.length == 0) {
+ String err = ErrorMessages.getMessage(
+ ErrorMessages.ERR_HERMES_FILE_IO_ERROR,
+ "missing empty line delimiter of MIME header");
+ logger.warn(err);
+ throw new MessageServerException(err);
+ }
+
+ /*
+ * Find the location and the length of the SOAPPart content
+ * with offset being the beginning position
+ */
+ soapMessageFileOffset = (int) offset;
+ out = new ByteArrayOutputStream();
+ line = readLine(fileStream);
+ offset += line.length;
+ s = new String(line);
+ while (line.length > 0 && !(s.startsWith
+ (Constants.MIME_BOUNDARY_PREFIX + boundary))) {
+ out.write(line);
+ line = readLine(fileStream);
+ offset += line.length;
+ s = new String(line);
+ }
+
+ if (line.length == 0) {
+ String err = ErrorMessages.getMessage(
+ ErrorMessages.ERR_HERMES_FILE_IO_ERROR,
+ "missing ending MIME boundary");
+ logger.warn(err);
+ throw new MessageServerException(err);
+ }
+ soapMessageBytes = out.toByteArray();
+ soapMessageLength = soapMessageBytes.length;
+ lastIndex = soapMessageBytes.length - 1;
+ for (; lastIndex >= 0; lastIndex--) {
+ if (soapMessageBytes[lastIndex] != 0xA &&
+ soapMessageBytes[lastIndex] != 0xD) {
+ break;
+ }
+ }
+ }
+ return new MessageSemiParsedOutput(fileStream, lastIndex,
+ soapMessageBytes, soapMessageFileOffset, boundary, line,
+ offset);
+ }
+
+ /**
+ * class represent the data returned from the parseSoapEnvelopeOnly
+ */
+ private static class MessageSemiParsedOutput {
+ private PushbackInputStream istream;
+ private byte[] soapMessage;
+ private int soapMessageOffset;
+ private String boundary;
+ private int lastIndex;
+ private byte[] endLine;
+ private long offset;
+ public MessageSemiParsedOutput(PushbackInputStream istream,
+ int lastIndex, byte[] soapMessage, int soapMessageOffset,
+ String boundary, byte[] endLine, long offset) {
+ this.istream = istream;
+ this.soapMessage = soapMessage;
+ this.soapMessageOffset = soapMessageOffset;
+ this.boundary = boundary;
+ this.lastIndex = lastIndex;
+ this.endLine = endLine;
+ this.offset = offset;
+ }
+
+ /**
+ * get the offset up on parsing
+ */
+ public long getOffset() {
+ return offset;
+ }
+
+ /**
+ * get the last line up on parsing
+ */
+ public byte[] getLastLine() {
+ return endLine;
+ }
+
+ /**
+ * get the last index to read the soap message
+ */
+ public int getLastIndex() {
+ return lastIndex;
+ }
+ /**
+ * get the remain stream which is not parsed
+ */
+ public PushbackInputStream getInputStream() {
+ return istream;
+ }
+
+ /**
+ * get the byte array that contains the soap message
+ */
+ public byte[] getSoapMessageBytes() {
+ return soapMessage;
+ }
+
+ /**
+ * get the soap message offset from the started stream
+ */
+ public int getSoapMessageOffset() {
+ return soapMessageOffset;
+ }
+
+ /**
+ * get the mime boundary
+ */
+ public String getBoundary() {
+ return boundary;
+ }
}
}
|