Ejemplo n.º 1
0
  public boolean handleMessage(SOAPMessageContext context) {
    boolean isSuccess = true;
    SOAPMessage message = null;
    boolean isRequest = false;
    String msgType = Constant.MESSAGE_TYPE_REQUEST;
    String requestID = null;
    String requestTimeStamp = null;
    String responseTimeStamp = null;
    String nameSpace = null;
    String webServiceName = null;
    try {
      if (context != null) {
        // get soap message from context
        message = context.getMessage();
        if (message != null) {
          isRequest =
              ((Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)).booleanValue();
          if (isRequest) {
            // assign a unique request id
            requestID = UUID.randomUUID().toString();
            context.put(Constant.WS_REQUEST_ID, requestID);
            // get the request time stamp
            requestTimeStamp = Utility.getTimeStamp(Constant.WS_TIMESTAMP_FORMAT);
            // find the name space and service name
            QName wsdlService = null;
            if (context.get(SOAPMessageContext.WSDL_SERVICE) != null) {
              wsdlService = (QName) context.get(SOAPMessageContext.WSDL_SERVICE);
              nameSpace = wsdlService.getNamespaceURI();
              webServiceName = wsdlService.getLocalPart();
              context.put(Constant.WS_NAME, webServiceName);
            }
            // dump the request
            dump(message, msgType, requestTimeStamp, webServiceName, requestID);
            // add the authentication header
            QName userNameElement = new QName(nameSpace, Constant.SOAP_USERNAME);
            QName passwordElement = new QName(nameSpace, Constant.SOAP_PASSWORD);
            SOAPHeader header = message.getSOAPHeader();
            SOAPPart soapPart = message.getSOAPPart();
            SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
            header = soapEnvelope.addHeader();
            SOAPElement userNameSOAPElement = header.addChildElement(userNameElement);
            userNameSOAPElement.addTextNode(Utility.getWSProperty(Constant.USERNAME_KEY));
            SOAPElement passwordSOAPElement = header.addChildElement(passwordElement);
            passwordSOAPElement.addTextNode(
                Utility.convertHexToString(Utility.getWSProperty(Constant.PASSWORD_KEY)));
          } else {
            msgType = Constant.MESSAGE_TYPE_RESPONSE;
            requestID = (String) context.get(Constant.WS_REQUEST_ID);
            webServiceName = (String) context.get(Constant.WS_NAME);
            responseTimeStamp = Utility.getTimeStamp(Constant.WS_TIMESTAMP_FORMAT);
            if (message.getSOAPHeader() != null) {
              message.getSOAPHeader().detachNode();
            }
            dump(message, msgType, responseTimeStamp, webServiceName, requestID);
          }
        }
      }
    } catch (Exception e) {

    }
    return isSuccess;
  }
Ejemplo n.º 2
0
  /**
   * dump soap message
   *
   * @param msg
   * @param msgType
   * @param timeStamp
   * @param wsName
   * @param requestID
   * @throws Exception
   */
  private void dump(
      SOAPMessage msg, String msgType, String timeStamp, String wsName, String requestID)
      throws Exception {
    ByteArrayOutputStream out = null;
    BufferedWriter bw = null;
    try {
      if (timeStamp != null && wsName != null && requestID != null) {
        Properties properties = Utility.loadEARProperties();
        String wasRoot = properties.getProperty(Constant.WAS_ROOT_DIR);
        String appDir = properties.getProperty(Constant.APP_DIR);
        String dumpDir = properties.getProperty(Constant.DUMP_DIR);
        String consumerDir = properties.getProperty(Constant.DUMP_COMSUMER_DIR);
        String dumpDirPath =
            wasRoot
                + File.separator
                + appDir
                + File.separator
                + dumpDir
                + File.separator
                + consumerDir;
        if (Constant.MESSAGE_TYPE_REQUEST.equals(msgType)) {
          dumpDirPath =
              dumpDirPath
                  + File.separator
                  + properties.getProperty(Constant.DUMP_COMSUMER_REQUEST_DIR);
        } else {
          dumpDirPath =
              dumpDirPath
                  + File.separator
                  + properties.getProperty(Constant.DUMP_COMSUMER_RESPONSE_DIR);
        }
        if (dumpDirPath != null) {
          File dir = new File(dumpDirPath);
          if (dir != null) {
            ArrayList<File> list = null;
            File[] files = dir.listFiles();
            if (files != null && files.length > 0) {
              list = new ArrayList<File>();
              for (int i = 0; i < files.length; ++i) {
                list.add(files[i]);
              }
              Collections.sort(
                  list,
                  new Comparator<File>() {
                    public int compare(File f1, File f2) {
                      long index1 =
                          Long.parseLong(
                              f1.getName()
                                  .substring(
                                      Constant.WS_DUMP_FILE_NAME.length(),
                                      f1.getName().indexOf(Constant.WS_DUMP_FILE_EXT)));
                      long index2 =
                          Long.parseLong(
                              f2.getName()
                                  .substring(
                                      Constant.WS_DUMP_FILE_NAME.length(),
                                      f2.getName().indexOf(Constant.WS_DUMP_FILE_EXT)));
                      return ((index2 > index1) ? 1 : -1);
                    }
                  });
            }
            File dumpFile = null;
            File lastFile = null;
            long maxFileSize = 1L;
            boolean createNewFile = false;
            if (list == null) {
              createNewFile = true;
            } else {
              maxFileSize = Long.parseLong(Utility.getWSProperty(Constant.WS_DUMP_FILE_SIZE));
              lastFile = list.get(0);
            }
            if (lastFile != null) {
              dumpFile = lastFile;
              long fileSize = dumpFile.length() / 1024L / 1024L;
              if (fileSize >= maxFileSize) {
                createNewFile = true;
              }
            }
            long fileIndex = 0L;
            if (dumpFile != null && createNewFile) {
              String lastFileName = dumpFile.getName();
              fileIndex =
                  Long.parseLong(
                      lastFileName.substring(
                          Constant.WS_DUMP_FILE_NAME.length(),
                          lastFileName.indexOf(Constant.WS_DUMP_FILE_EXT)));
            }
            if (createNewFile) {
              File newFile =
                  new File(
                      dumpDirPath
                          + File.separator
                          + Constant.WS_DUMP_FILE_NAME
                          + (fileIndex + 1)
                          + Constant.WS_DUMP_FILE_EXT);
              if (newFile.createNewFile()) {
                dumpFile = newFile;
              }
            }
            String startTag =
                Constant.LESS_THAN
                    + Constant.WS_MSG_TAG
                    + Constant.SPACE
                    + Constant.WS_MSG_ATTRIBUTE_REQUEST_ID
                    + Constant.EQUAL
                    + Constant.DOUBLE_QUOTE
                    + requestID
                    + Constant.DOUBLE_QUOTE
                    + Constant.WS_MSG_ATTRIBUTE_MSG_TYPE
                    + Constant.EQUAL
                    + Constant.DOUBLE_QUOTE
                    + msgType
                    + Constant.DOUBLE_QUOTE
                    + Constant.WS_MSG_ATTRIBUTE_SERVICE_NAME
                    + Constant.EQUAL
                    + Constant.DOUBLE_QUOTE
                    + wsName
                    + Constant.DOUBLE_QUOTE
                    + Constant.WS_MSG_ATTRIBUTE_TIMESTAMP
                    + Constant.EQUAL
                    + Constant.DOUBLE_QUOTE
                    + timeStamp
                    + Constant.DOUBLE_QUOTE
                    + Constant.GREATER_THAN;

            out = new ByteArrayOutputStream();
            msg.writeTo(out);
            String strMsg = new String(out.toByteArray(), Constant.UTF8);
            out.close();
            out = null;

            String endTag =
                Constant.LESS_THAN
                    + Constant.FORWARD_SLASH
                    + Constant.WS_MSG_TAG
                    + Constant.GREATER_THAN;
            bw =
                new BufferedWriter(
                    new OutputStreamWriter(new FileOutputStream(dumpFile, true), Constant.UTF8));
            bw.write(startTag + strMsg + endTag);
            bw.close();
            bw = null;
          }
        }
      }
    } catch (Exception e) {
      throw e;
    } finally {
      if (out != null) {
        out.close();
        out = null;
      }
      if (bw != null) {
        bw.close();
        bw = null;
      }
    }
  }