예제 #1
0
  public SOAPMessage get(Object endPoint) throws SOAPException {
    if (closed) {
      log.severe("SAAJ0011.p2p.get.already.closed.conn");
      throw new SOAPExceptionImpl("Connection is closed");
    }
    Class urlEndpointClass = null;

    try {
      urlEndpointClass = Class.forName("javax.xml.messaging.URLEndpoint");
    } catch (Exception ex) {
      // Do nothing. URLEndpoint is available only when JAXM is there.
    }

    if (urlEndpointClass != null) {
      if (urlEndpointClass.isInstance(endPoint)) {
        String url = null;

        try {
          Method m = urlEndpointClass.getMethod("getURL", (Class[]) null);
          url = (String) m.invoke(endPoint, (Object[]) null);
        } catch (Exception ex) {
          log.severe("SAAJ0004.p2p.internal.err");
          throw new SOAPExceptionImpl("Internal error: " + ex.getMessage());
        }
        try {
          endPoint = new URL(url);
        } catch (MalformedURLException mex) {
          log.severe("SAAJ0005.p2p.");
          throw new SOAPExceptionImpl("Bad URL: " + mex.getMessage());
        }
      }
    }

    if (endPoint instanceof java.lang.String) {
      try {
        endPoint = new URL((String) endPoint);
      } catch (MalformedURLException mex) {
        log.severe("SAAJ0006.p2p.bad.URL");
        throw new SOAPExceptionImpl("Bad URL: " + mex.getMessage());
      }
    }

    if (endPoint instanceof URL)
      try {
        SOAPMessage response = doGet((URL) endPoint);
        return response;
      } catch (Exception ex) {
        throw new SOAPExceptionImpl(ex);
      }
    else throw new SOAPExceptionImpl("Bad endPoint type " + endPoint);
  }
예제 #2
0
  SOAPMessage doGet(URL endPoint) throws SOAPException {
    boolean isFailure = false;

    URL url = null;
    HttpURLConnection httpConnection = null;

    int responseCode = 0;
    try {
      /// Is https GET allowed??
      if (endPoint.getProtocol().equals("https")) initHttps();
      // Process the URL
      JaxmURI uri = new JaxmURI(endPoint.toString());
      String userInfo = uri.getUserinfo();

      url = endPoint;

      if (dL > 0) d("uri: " + userInfo + " " + url + " " + uri);

      // TBD
      //    Will deal with https later.
      if (!url.getProtocol().equalsIgnoreCase("http")
          && !url.getProtocol().equalsIgnoreCase("https")) {
        log.severe("SAAJ0052.p2p.protocol.mustbe.http.or.https");
        throw new IllegalArgumentException(
            "Protocol " + url.getProtocol() + " not supported in URL " + url);
      }
      httpConnection = (HttpURLConnection) createConnection(url);

      httpConnection.setRequestMethod("GET");

      httpConnection.setDoOutput(true);
      httpConnection.setDoInput(true);
      httpConnection.setUseCaches(false);
      httpConnection.setFollowRedirects(true);

      httpConnection.connect();

      try {

        responseCode = httpConnection.getResponseCode();

        // let HTTP_INTERNAL_ERROR (500) through because it is used for SOAP faults
        if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
          isFailure = true;
        } else if ((responseCode / 100) != 2) {
          log.log(
              Level.SEVERE,
              "SAAJ0008.p2p.bad.response",
              new String[] {httpConnection.getResponseMessage()});
          throw new SOAPExceptionImpl(
              "Bad response: (" + responseCode + httpConnection.getResponseMessage());
        }
      } catch (IOException e) {
        // on JDK1.3.1_01, we end up here, but then getResponseCode() succeeds!
        responseCode = httpConnection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
          isFailure = true;
        } else {
          throw e;
        }
      }

    } catch (SOAPException ex) {
      throw ex;
    } catch (Exception ex) {
      log.severe("SAAJ0012.p2p.get.failed");
      throw new SOAPExceptionImpl("Get failed", ex);
    }

    SOAPMessage response = null;
    if (responseCode == HttpURLConnection.HTTP_OK || isFailure) {
      try {
        MimeHeaders headers = new MimeHeaders();

        String key, value;

        // Header field 0 is the status line so we skip it.

        int i = 1;

        while (true) {
          key = httpConnection.getHeaderFieldKey(i);
          value = httpConnection.getHeaderField(i);

          if (key == null && value == null) break;

          if (key != null) {
            StringTokenizer values = new StringTokenizer(value, ",");
            while (values.hasMoreTokens()) headers.addHeader(key, values.nextToken().trim());
          }
          i++;
        }

        InputStream httpIn =
            (isFailure ? httpConnection.getErrorStream() : httpConnection.getInputStream());
        // If no reply message is returned,
        // content-Length header field value is expected to be zero.
        // java SE 6 documentation says :
        // available() : an estimate of the number of bytes that can be read
        // (or skipped over) from this input stream without blocking
        // or 0 when it reaches the end of the input stream.
        if ((httpIn == null)
            || (httpConnection.getContentLength() == 0)
            || (httpIn.available() == 0)) {
          response = null;
          log.warning("SAAJ0014.p2p.content.zero");
        } else {
          response = messageFactory.createMessage(headers, httpIn);
        }

        httpIn.close();
        httpConnection.disconnect();

      } catch (SOAPException ex) {
        throw ex;
      } catch (Exception ex) {
        log.log(Level.SEVERE, "SAAJ0010.p2p.cannot.read.resp", ex);
        throw new SOAPExceptionImpl("Unable to read response: " + ex.getMessage());
      }
    }
    return response;
  }
예제 #3
0
  SOAPMessage post(SOAPMessage message, URL endPoint) throws SOAPException {
    boolean isFailure = false;

    URL url = null;
    HttpURLConnection httpConnection = null;

    int responseCode = 0;
    try {
      if (endPoint.getProtocol().equals("https"))
        // if(!setHttps)
        initHttps();
      // Process the URL
      JaxmURI uri = new JaxmURI(endPoint.toString());
      String userInfo = uri.getUserinfo();

      url = endPoint;

      if (dL > 0) d("uri: " + userInfo + " " + url + " " + uri);

      // TBD
      //    Will deal with https later.
      if (!url.getProtocol().equalsIgnoreCase("http")
          && !url.getProtocol().equalsIgnoreCase("https")) {
        log.severe("SAAJ0052.p2p.protocol.mustbe.http.or.https");
        throw new IllegalArgumentException(
            "Protocol " + url.getProtocol() + " not supported in URL " + url);
      }
      httpConnection = (HttpURLConnection) createConnection(url);

      httpConnection.setRequestMethod("POST");

      httpConnection.setDoOutput(true);
      httpConnection.setDoInput(true);
      httpConnection.setUseCaches(false);
      httpConnection.setInstanceFollowRedirects(true);

      if (message.saveRequired()) message.saveChanges();

      MimeHeaders headers = message.getMimeHeaders();

      Iterator it = headers.getAllHeaders();
      boolean hasAuth = false; // true if we find explicit Auth header
      while (it.hasNext()) {
        MimeHeader header = (MimeHeader) it.next();

        String[] values = headers.getHeader(header.getName());
        if (values.length == 1)
          httpConnection.setRequestProperty(header.getName(), header.getValue());
        else {
          StringBuffer concat = new StringBuffer();
          int i = 0;
          while (i < values.length) {
            if (i != 0) concat.append(',');
            concat.append(values[i]);
            i++;
          }

          httpConnection.setRequestProperty(header.getName(), concat.toString());
        }

        if ("Authorization".equals(header.getName())) {
          hasAuth = true;
          log.fine("SAAJ0091.p2p.https.auth.in.POST.true");
        }
      }

      if (!hasAuth && userInfo != null) {
        initAuthUserInfo(httpConnection, userInfo);
      }

      OutputStream out = httpConnection.getOutputStream();
      message.writeTo(out);

      out.flush();
      out.close();

      httpConnection.connect();

      try {

        responseCode = httpConnection.getResponseCode();

        // let HTTP_INTERNAL_ERROR (500) through because it is used for SOAP faults
        if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
          isFailure = true;
        }
        // else if (responseCode != HttpURLConnection.HTTP_OK)
        // else if (!(responseCode >= HttpURLConnection.HTTP_OK && responseCode < 207))
        else if ((responseCode / 100) != 2) {
          log.log(
              Level.SEVERE,
              "SAAJ0008.p2p.bad.response",
              new String[] {httpConnection.getResponseMessage()});
          throw new SOAPExceptionImpl(
              "Bad response: (" + responseCode + httpConnection.getResponseMessage());
        }
      } catch (IOException e) {
        // on JDK1.3.1_01, we end up here, but then getResponseCode() succeeds!
        responseCode = httpConnection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) {
          isFailure = true;
        } else {
          throw e;
        }
      }

    } catch (SOAPException ex) {
      throw ex;
    } catch (Exception ex) {
      log.severe("SAAJ0009.p2p.msg.send.failed");
      throw new SOAPExceptionImpl("Message send failed", ex);
    }

    SOAPMessage response = null;
    if (responseCode == HttpURLConnection.HTTP_OK || isFailure) {
      try {
        MimeHeaders headers = new MimeHeaders();

        String key, value;

        // Header field 0 is the status line so we skip it.

        int i = 1;

        while (true) {
          key = httpConnection.getHeaderFieldKey(i);
          value = httpConnection.getHeaderField(i);

          if (key == null && value == null) break;

          if (key != null) {
            StringTokenizer values = new StringTokenizer(value, ",");
            while (values.hasMoreTokens()) headers.addHeader(key, values.nextToken().trim());
          }
          i++;
        }

        InputStream httpIn =
            (isFailure ? httpConnection.getErrorStream() : httpConnection.getInputStream());

        byte[] bytes = readFully(httpIn);

        int length =
            httpConnection.getContentLength() == -1
                ? bytes.length
                : httpConnection.getContentLength();

        // If no reply message is returned,
        // content-Length header field value is expected to be zero.
        if (length == 0) {
          response = null;
          log.warning("SAAJ0014.p2p.content.zero");
        } else {
          ByteInputStream in = new ByteInputStream(bytes, length);
          response = messageFactory.createMessage(headers, in);
        }

        httpIn.close();
        httpConnection.disconnect();

      } catch (SOAPException ex) {
        throw ex;
      } catch (Exception ex) {
        log.log(Level.SEVERE, "SAAJ0010.p2p.cannot.read.resp", ex);
        throw new SOAPExceptionImpl("Unable to read response: " + ex.getMessage());
      }
    }
    return response;
  }
예제 #4
0
  public SOAPMessage call(SOAPMessage message, Object endPoint) throws SOAPException {
    if (closed) {
      log.severe("SAAJ0003.p2p.call.already.closed.conn");
      throw new SOAPExceptionImpl("Connection is closed");
    }

    Class urlEndpointClass = null;
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    try {
      if (loader != null) {
        urlEndpointClass = loader.loadClass(JAXM_URLENDPOINT);
      } else {
        urlEndpointClass = Class.forName(JAXM_URLENDPOINT);
      }
    } catch (ClassNotFoundException ex) {
      // Do nothing. URLEndpoint is available only when JAXM is there.
      log.finest("SAAJ0090.p2p.endpoint.available.only.for.JAXM");
    }

    if (urlEndpointClass != null) {
      if (urlEndpointClass.isInstance(endPoint)) {
        String url = null;

        try {
          Method m = urlEndpointClass.getMethod("getURL", (Class[]) null);
          url = (String) m.invoke(endPoint, (Object[]) null);
        } catch (Exception ex) {
          // TBD -- exception chaining
          log.log(Level.SEVERE, "SAAJ0004.p2p.internal.err", ex);
          throw new SOAPExceptionImpl("Internal error: " + ex.getMessage());
        }
        try {
          endPoint = new URL(url);
        } catch (MalformedURLException mex) {
          log.log(Level.SEVERE, "SAAJ0005.p2p.", mex);
          throw new SOAPExceptionImpl("Bad URL: " + mex.getMessage());
        }
      }
    }

    if (endPoint instanceof java.lang.String) {
      try {
        endPoint = new URL((String) endPoint);
      } catch (MalformedURLException mex) {
        log.log(Level.SEVERE, "SAAJ0006.p2p.bad.URL", mex);
        throw new SOAPExceptionImpl("Bad URL: " + mex.getMessage());
      }
    }

    if (endPoint instanceof URL)
      try {
        SOAPMessage response = post(message, (URL) endPoint);
        return response;
      } catch (Exception ex) {
        // TBD -- chaining?
        throw new SOAPExceptionImpl(ex);
      }
    else {
      log.severe("SAAJ0007.p2p.bad.endPoint.type");
      throw new SOAPExceptionImpl("Bad endPoint type " + endPoint);
    }
  }