Example #1
0
 /**
  * Get the content length, including both soap and any attachments.
  *
  * @return the total length of this message in bytes
  * @throws org.apache.axis.AxisFault if there was a problem that prevented the length being
  *     calculated
  */
 public long getContentLength() throws org.apache.axis.AxisFault {
   long ret = mSOAPPart.getContentLength();
   if (mAttachments != null && 0 < mAttachments.getAttachmentCount()) {
     ret = mAttachments.getContentLength();
   }
   return ret;
 }
Example #2
0
 /**
  * Writes this <CODE>SOAPMessage</CODE> object to the given output stream. The externalization
  * format is as defined by the SOAP 1.1 with Attachments specification.
  *
  * <p>If there are no attachments, just an XML stream is written out. For those messages that have
  * attachments, <CODE>writeTo</CODE> writes a MIME-encoded byte stream.
  *
  * @param os the <CODE>OutputStream</CODE> object to which this <CODE>SOAPMessage</CODE> object
  *     will be written
  * @throws SOAPException if there was a problem in externalizing this SOAP message
  * @throws IOException if an I/O error occurs
  */
 public void writeTo(java.io.OutputStream os) throws SOAPException, IOException {
   // Do it the old fashion way.
   if (getSendType() == Attachments.SEND_TYPE_NONE
       || mAttachments == null
       || 0 == mAttachments.getAttachmentCount()) {
     try {
       String charEncoding = XMLUtils.getEncoding(this, msgContext);
       ;
       mSOAPPart.setEncoding(charEncoding);
       mSOAPPart.writeTo(os);
     } catch (java.io.IOException e) {
       log.error(Messages.getMessage("javaIOException00"), e);
     }
   } else {
     try {
       mAttachments.writeContentToStream(os);
     } catch (java.lang.Exception e) {
       log.error(Messages.getMessage("exception00"), e);
     }
   }
 }
Example #3
0
 /**
  * Updates this <CODE>SOAPMessage</CODE> object with all the changes that have been made to it.
  * This method is called automatically when a message is sent or written to by the methods <CODE>
  * ProviderConnection.send</CODE>, <CODE>
  *   SOAPConnection.call</CODE>, or <CODE>
  *   SOAPMessage.writeTo</CODE>. However, if changes are made to a message that was received or to
  * one that has already been sent, the method <CODE>saveChanges</CODE> needs to be called
  * explicitly in order to save the changes. The method <CODE>saveChanges</CODE> also generates any
  * changes that can be read back (for example, a MessageId in profiles that support a message id).
  * All MIME headers in a message that is created for sending purposes are guaranteed to have valid
  * values only after <CODE>saveChanges</CODE> has been called.
  *
  * <p>In addition, this method marks the point at which the data from all constituent <CODE>
  * AttachmentPart</CODE> objects are pulled into the message.
  *
  * @throws SOAPException if there was a problem saving changes to this message.
  */
 public void saveChanges() throws SOAPException {
   headers.removeHeader("Content-Length");
   if (mAttachments != null && 0 < mAttachments.getAttachmentCount()) {
     try {
       headers.setHeader("Content-Type", mAttachments.getContentType());
     } catch (AxisFault af) {
       log.error(Messages.getMessage("exception00"), af);
     }
   }
   saveRequired = false;
   try {
     /* Fix for Bug 16418 - Start from scratch */
     mSOAPPart.saveChanges();
   } catch (AxisFault axisFault) {
     log.error(Messages.getMessage("exception00"), axisFault);
   }
 }
Example #4
0
  public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException {

    String retval = "<html> <H4>";

    try {
      // Create a message factory.
      MessageFactory mf = MessageFactory.newInstance();

      // Create a message from the message factory.
      SOAPMessage msg = mf.createMessage();

      // Message creation takes care of creating the SOAPPart - a
      // required part of the message as per the SOAP 1.1
      // specification.
      SOAPPart sp = msg.getSOAPPart();

      // Retrieve the envelope from the soap part to start building
      // the soap message.
      SOAPEnvelope envelope = sp.getEnvelope();

      // Create a soap header from the envelope.
      SOAPHeader hdr = envelope.getHeader();

      // Create a soap body from the envelope.
      SOAPBody bdy = envelope.getBody();

      // Add a soap body element to the soap body
      SOAPBodyElement gltp =
          bdy.addBodyElement(
              envelope.createName("GetLastTradePrice", "ztrade", "http://wombat.ztrade.com"));

      gltp.addChildElement(envelope.createName("symbol", "ztrade", "http://wombat.ztrade.com"))
          .addTextNode("SUNW");

      StringBuffer urlSB = new StringBuffer();
      urlSB.append(req.getScheme()).append("://").append(req.getServerName());
      urlSB.append(":").append(req.getServerPort()).append(req.getContextPath());
      String reqBase = urlSB.toString();

      if (data == null) {
        data = reqBase + "/index.html";
      }

      // Want to set an attachment from the following url.
      // Get context
      URL url = new URL(data);

      AttachmentPart ap = msg.createAttachmentPart(new DataHandler(url));

      ap.setContentType("text/html");

      // Add the attachment part to the message.
      msg.addAttachmentPart(ap);

      // Create an endpoint for the recipient of the message.
      if (to == null) {
        to = reqBase + "/receiver";
      }

      URL urlEndpoint = new URL(to);

      System.err.println("Sending message to URL: " + urlEndpoint);
      System.err.println("Sent message is logged in \"sent.msg\"");

      retval += " Sent message (check \"sent.msg\") and ";

      FileOutputStream sentFile = new FileOutputStream("sent.msg");
      msg.writeTo(sentFile);
      sentFile.close();

      // Send the message to the provider using the connection.
      SOAPMessage reply = con.call(msg, urlEndpoint);

      if (reply != null) {
        FileOutputStream replyFile = new FileOutputStream("reply.msg");
        reply.writeTo(replyFile);
        replyFile.close();
        System.err.println("Reply logged in \"reply.msg\"");
        retval += " received reply (check \"reply.msg\").</H4> </html>";

      } else {
        System.err.println("No reply");
        retval += " no reply was received. </H4> </html>";
      }

    } catch (Throwable e) {
      e.printStackTrace();
      logger.severe("Error in constructing or sending message " + e.getMessage());
      retval += " There was an error " + "in constructing or sending message. </H4> </html>";
    }

    try {
      OutputStream os = resp.getOutputStream();
      os.write(retval.getBytes());
      os.flush();
      os.close();
    } catch (IOException e) {
      e.printStackTrace();
      logger.severe("Error in outputting servlet response " + e.getMessage());
    }
  }
Example #5
0
 public SOAPHeader getSOAPHeader() throws SOAPException {
   return mSOAPPart.getEnvelope().getHeader();
 }
Example #6
0
 public SOAPBody getSOAPBody() throws SOAPException {
   return mSOAPPart.getEnvelope().getBody();
 }
Example #7
0
 /**
  * Get this message's SOAPPart as a SOAPEnvelope.
  *
  * @return a SOAPEnvelope containing this message's SOAPPart
  * @throws AxisFault if this failed
  */
 public SOAPEnvelope getSOAPEnvelope() throws AxisFault {
   return mSOAPPart.getAsSOAPEnvelope();
 }
Example #8
0
 /**
  * Get a byte array representation of this message's SOAPPart.
  *
  * @return the soap part of this message as a <code>byte[]</code>
  * @throws org.apache.axis.AxisFault if creating the byte[] failed
  */
 public byte[] getSOAPPartAsBytes() throws org.apache.axis.AxisFault {
   return mSOAPPart.getAsBytes();
 }
Example #9
0
 /**
  * Get a string representation of this message's SOAPPart.
  *
  * @return the soap part of this message as a <code>String</code>
  * @throws org.apache.axis.AxisFault if the stringification failed
  */
 public String getSOAPPartAsString() throws org.apache.axis.AxisFault {
   return mSOAPPart.getAsString();
 }
Example #10
0
  /**
   * Do the work of construction.
   *
   * @param initialContents may be String, byte[], InputStream, SOAPEnvelope, or AxisFault
   * @param bodyInStream is true if initialContents is an InputStream containing just the SOAP body
   *     (no SOAP-ENV)
   * @param contentType this if the contentType has been already determined (as in the case of
   *     servlets)
   * @param contentLocation the location of the content
   * @param mimeHeaders mime headers for attachments
   */
  private void setup(
      Object initialContents,
      boolean bodyInStream,
      String contentType,
      String contentLocation,
      javax.xml.soap.MimeHeaders mimeHeaders) {

    if (contentType == null && mimeHeaders != null) {
      String contentTypes[] = mimeHeaders.getHeader("Content-Type");
      contentType = (contentTypes != null) ? contentTypes[0] : null;
    }
    if (contentLocation == null && mimeHeaders != null) {
      String contentLocations[] = mimeHeaders.getHeader("Content-Location");
      contentLocation = (contentLocations != null) ? contentLocations[0] : null;
    }
    if (contentType != null) {
      int delimiterIndex = contentType.lastIndexOf("charset");
      if (delimiterIndex > 0) {
        String charsetPart = contentType.substring(delimiterIndex);
        int delimiterIndex2 = charsetPart.indexOf(';');
        if (delimiterIndex2 != -1) {
          charsetPart = charsetPart.substring(0, delimiterIndex2);
        }
        int charsetIndex = charsetPart.indexOf('=');
        String charset = charsetPart.substring(charsetIndex + 1).trim();
        if ((charset.startsWith("\"") || charset.startsWith("\'"))) {
          charset = charset.substring(1, charset.length());
        }
        if ((charset.endsWith("\"") || charset.endsWith("\'"))) {
          charset = charset.substring(0, charset.length() - 1);
        }
        try {
          setProperty(SOAPMessage.CHARACTER_SET_ENCODING, charset);
        } catch (SOAPException e) {
        }
      }
    }
    // Try to construct an AttachmentsImpl object for attachment
    // functionality.
    // If there is no org.apache.axis.attachments.AttachmentsImpl class,
    // it must mean activation.jar is not present and attachments are not
    // supported.
    if (isAttachmentSupportEnabled(getMessageContext())) {
      // Construct one, and cast to Attachments.
      // There must be exactly one constructor of AttachmentsImpl, which
      // must take an org.apache.axis.Message!
      Constructor attachImplConstr = attachImpl.getConstructors()[0];
      try {
        mAttachments =
            (Attachments)
                attachImplConstr.newInstance(
                    new Object[] {initialContents, contentType, contentLocation});

        // If it can't support it, it wont have a root part.
        mSOAPPart = (SOAPPart) mAttachments.getRootPart();
      } catch (InvocationTargetException ex) {
        log.fatal(Messages.getMessage("invocationTargetException00"), ex);
        throw new RuntimeException(ex.getMessage());
      } catch (InstantiationException ex) {
        log.fatal(Messages.getMessage("instantiationException00"), ex);
        throw new RuntimeException(ex.getMessage());
      } catch (IllegalAccessException ex) {
        log.fatal(Messages.getMessage("illegalAccessException00"), ex);
        throw new RuntimeException(ex.getMessage());
      }
    } else if (contentType != null && contentType.startsWith("multipart")) {
      throw new RuntimeException(Messages.getMessage("noAttachments"));
    }

    // text/xml
    if (null == mSOAPPart) {
      mSOAPPart = new SOAPPart(this, initialContents, bodyInStream);
    } else mSOAPPart.setMessage(this);

    // The stream was not determined by a more complex type so default to
    if (mAttachments != null) mAttachments.setRootPart(mSOAPPart);

    headers = (mimeHeaders == null) ? new MimeHeaders() : new MimeHeaders(mimeHeaders);
  }
  public boolean handleMessage(SOAPMessageContext smc) {
    Boolean outbound = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    if (outbound) {
      // outbound message

      // *** #8 ***
      // get token from response context
      String propertyValue = (String) smc.get(RESPONSE_PROPERTY);
      System.out.printf("%s received '%s'%n", CLASS_NAME, propertyValue);

      // put token in response SOAP header
      try {
        // get SOAP envelope
        SOAPMessage msg = smc.getMessage();
        SOAPPart sp = msg.getSOAPPart();
        SOAPEnvelope se = sp.getEnvelope();

        // add header
        SOAPHeader sh = se.getHeader();
        if (sh == null) sh = se.addHeader();

        // add header element (name, namespace prefix, namespace)
        Name name = se.createName(RESPONSE_HEADER, "e", RESPONSE_NS);
        SOAPHeaderElement element = sh.addHeaderElement(name);

        // *** #9 ***
        // add header element value
        String newValue = propertyValue + "," + TOKEN;
        element.addTextNode(newValue);

        System.out.printf("%s put token '%s' on response message header%n", CLASS_NAME, TOKEN);

      } catch (SOAPException e) {
        System.out.printf("Failed to add SOAP header because of %s%n", e);
      }

    } else {
      // inbound message

      // get token from request SOAP header
      try {
        // get SOAP envelope header
        SOAPMessage msg = smc.getMessage();
        SOAPPart sp = msg.getSOAPPart();
        SOAPEnvelope se = sp.getEnvelope();
        SOAPHeader sh = se.getHeader();

        // check header
        if (sh == null) {
          System.out.println("Header not found.");
          return true;
        }

        // get Ticket header element
        Name nameTicket = se.createName(REQUEST_TICKET_HEADER, "e", REQUEST_NS);
        Iterator it = sh.getChildElements(nameTicket);
        // check header element
        if (!it.hasNext()) {
          System.out.printf("Header element %s not found.%n", REQUEST_TICKET_HEADER);
          return true;
        }
        SOAPElement elementTicket = (SOAPElement) it.next();

        // *** #4 ***
        // get header element value
        String headerTicketValue = elementTicket.getValue();
        System.out.printf("%s got '%s'%n", CLASS_NAME, headerTicketValue);

        // *** #5 ***
        // put Ticket token in request context
        System.out.printf("%s put token '%s' on request context%n", CLASS_NAME, headerTicketValue);
        smc.put(REQUEST_TICKET_PROPERTY, headerTicketValue);
        // set property scope to application so that server class can access property
        smc.setScope(REQUEST_TICKET_PROPERTY, Scope.APPLICATION);

        // get Author header element
        Name nameAuthor = se.createName(REQUEST_AUTHOR_HEADER, "e", REQUEST_NS);
        Iterator itAuthor = sh.getChildElements(nameAuthor);
        // check header element
        if (!itAuthor.hasNext()) {
          System.out.printf("Header element %s not found.%n", REQUEST_AUTHOR_HEADER);
          return true;
        }
        SOAPElement elementAuthor = (SOAPElement) itAuthor.next();

        // *** #4 ***
        // get Author header element value
        String headerAuthorValue = elementAuthor.getValue();
        System.out.printf("%s got '%s'%n", CLASS_NAME, headerAuthorValue);

        // *** #5 ***
        // put Author token in request context
        System.out.printf("%s put token '%s' on request context%n", CLASS_NAME, headerAuthorValue);
        smc.put(REQUEST_AUTHOR_PROPERTY, headerAuthorValue);
        // set property scope to application so that server class can access property
        smc.setScope(REQUEST_AUTHOR_PROPERTY, Scope.APPLICATION);

        // MAC
        // get MAC header element
        Name nameMac = se.createName(REQUEST_MAC_HEADER, "e", REQUEST_NS);
        Iterator itMac = sh.getChildElements(nameAuthor);
        // check header element
        if (!itMac.hasNext()) {
          System.out.printf("Header element %s not found.%n", REQUEST_MAC_HEADER);
          return true;
        }
        SOAPElement elementMac = (SOAPElement) itMac.next();

        // *** #4 ***
        // get MAC header element value
        String headerMacValue = elementMac.getTextContent();

        System.out.printf("%s got '%s'%n", CLASS_NAME, headerMacValue);

        // *** #5 ***
        // put MAC token in request context
        System.out.printf("%s put token '%s' on request context%n", CLASS_NAME, headerMacValue);
        smc.put(REQUEST_MAC_PROPERTY, headerMacValue);
        // set property scope to application so that server class can access property
        smc.setScope(REQUEST_MAC_PROPERTY, Scope.APPLICATION);

        String bodyValue = se.getBody().getTextContent();
        // *** #5 ***
        // put Body token in request context
        System.out.printf("%s put token '%s' on request context%n", CLASS_NAME, bodyValue);
        smc.put(REQUEST_BODY_PROPERTY, bodyValue);
        // set property scope to application so that server class can access property
        smc.setScope(REQUEST_BODY_PROPERTY, Scope.APPLICATION);

        msg.writeTo(System.out);

      } catch (SOAPException e) {
        System.out.printf("Failed to get SOAP header because of %s%n", e);
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    return true;
  }