コード例 #1
0
    private void prettyPrint(byte[] bytes) {
      boolean newLine = true;
      for (int i = 0; i < bytes.length; i++) {
        if (bytes[i] == '<') {
          if (i + 1 < bytes.length) {
            if (bytes[i + 1] == '/') {
              level--;
            } else {
              level++;
            }
          }
          for (int j = 0; newLine && j < level; j++) {
            config.getTraceStream().print("  ");
          }
        }

        config.getTraceStream().write(bytes[i]);

        if (bytes[i] == '>') {
          if (i + 1 < bytes.length && bytes[i + 1] == '<') {
            config.getTraceStream().println();
            newLine = true;
          } else {
            newLine = false;
          }
        }
      }
    }
コード例 #2
0
    public TeeInputStream(ConnectorConfig config, byte[] bytes) {
      this.config = config;
      config.getTraceStream().println("------------ Response start ----------");

      if (config.isPrettyPrintXml()) {
        prettyPrint(bytes);
      } else {
        config.getTraceStream().print(new String(bytes));
      }

      config.getTraceStream().println();
      config.getTraceStream().println("------------ Response end   ----------");
    }
コード例 #3
0
ファイル: SoapConnection.java プロジェクト: forcedotcom/wsc
  public XMLizable send(
      String soapAction,
      QName requestElement,
      XMLizable request,
      QName responseElement,
      Class responseType)
      throws ConnectionException {

    long startTime = System.currentTimeMillis();

    try {
      boolean firstTime = true;
      while (true) {
        try {
          Transport transport = newTransport(config);
          OutputStream out = transport.connect(url, soapAction);
          sendRequest(out, request, requestElement);
          InputStream in = transport.getContent();
          XMLizable result;
          result = receive(transport, responseElement, responseType, in);
          return result;
        } catch (SessionTimedOutException se) {
          if (config.getSessionRenewer() == null || !firstTime) {
            throw (ConnectionException) se.getCause();
          } else {
            SessionRenewer.SessionRenewalHeader sessionHeader =
                config.getSessionRenewer().renewSession(config);
            if (sessionHeader != null) {
              addHeader(sessionHeader.name, sessionHeader.headerElement);
            }
          }
        }
        firstTime = false;
      }
    } catch (SocketTimeoutException e) {
      long timeTaken = System.currentTimeMillis() - startTime;
      throw new ConnectionException(
          "Request to "
              + url
              + " timed out. TimeTaken="
              + timeTaken
              + " ConnectionTimeout="
              + config.getConnectionTimeout()
              + " ReadTimeout="
              + config.getReadTimeout(),
          e);

    } catch (IOException e) {
      throw new ConnectionException("Failed to send request to " + url, e);
    }
  }
コード例 #4
0
ファイル: SoapConnection.java プロジェクト: forcedotcom/wsc
  private Transport newTransport(ConnectorConfig config) throws ConnectionException {
    if (config.getTransportFactory() != null) {
      Transport t = config.getTransportFactory().createTransport();
      t.setConfig(config);
      return t;
    }

    try {
      Transport t = (Transport) config.getTransport().newInstance();
      t.setConfig(config);
      return t;
    } catch (InstantiationException e) {
      throw new ConnectionException("Failed to create new Transport " + config.getTransport());
    } catch (IllegalAccessException e) {
      throw new ConnectionException("Failed to create new Transport " + config.getTransport());
    }
  }
コード例 #5
0
  private OutputStream wrapOutput(OutputStream output, boolean enableCompression)
      throws IOException {
    if (config.getMaxRequestSize() > 0) {
      output = new LimitingOutputStream(config.getMaxRequestSize(), output);
    }

    // when we are writing a zip file we don't bother with compression
    if (enableCompression && config.isCompression()) {
      output = new GZIPOutputStream(output);
    }

    if (config.isTraceMessage()) {
      output = new TeeOutputStream(output);
    }

    if (config.hasMessageHandlers()) {
      output = new MessageHandlerOutputStream(output);
    }

    return output;
  }
コード例 #6
0
  private OutputStream connectRaw(
      String uri, HashMap<String, String> httpHeaders, boolean enableCompression)
      throws IOException {
    url = new URL(uri);

    connection = createConnection(config, url, httpHeaders, enableCompression);
    connection.setRequestMethod("POST");
    connection.setDoInput(true);
    connection.setDoOutput(true);
    if (config.useChunkedPost()) {
      connection.setChunkedStreamingMode(4096);
    }

    return connection.getOutputStream();
  }
コード例 #7
0
  @Override
  public InputStream getContent() throws IOException {
    InputStream in;

    try {
      successful = true;
      in = connection.getInputStream();
    } catch (IOException e) {
      successful = false;
      in = connection.getErrorStream();
      if (in == null) {
        throw e;
      }
    }

    String encoding = connection.getHeaderField("Content-Encoding");

    if (config.getMaxResponseSize() > 0) {
      in = new LimitingInputStream(config.getMaxResponseSize(), in);
    }

    if ("gzip".equals(encoding)) {
      in = new GZIPInputStream(in);
    }

    if (config.hasMessageHandlers() || config.isTraceMessage()) {
      byte[] bytes = FileUtil.toBytes(in);
      in = new ByteArrayInputStream(bytes);

      if (config.hasMessageHandlers()) {
        Iterator<MessageHandler> it = config.getMessagerHandlers();
        while (it.hasNext()) {
          MessageHandler handler = it.next();
          if (handler instanceof MessageHandlerWithHeaders) {
            ((MessageHandlerWithHeaders) handler)
                .handleResponse(url, bytes, connection.getHeaderFields());
          } else {
            handler.handleResponse(url, bytes);
          }
        }
      }

      if (config.isTraceMessage()) {
        new TeeInputStream(config, bytes);
      }
    }

    return in;
  }
コード例 #8
0
ファイル: SoapConnection.java プロジェクト: forcedotcom/wsc
  private void sendRequest(OutputStream out, XMLizable request, QName requestElement)
      throws IOException {
    XmlOutputStream xout = new XmlOutputStream(out, config.isPrettyPrintXml());
    xout.startDocument();

    xout.setPrefix("env", Constants.SOAP_ENVELOPE_NS);
    xout.setPrefix("xsd", Constants.SCHEMA_NS);
    xout.setPrefix("xsi", Constants.SCHEMA_INSTANCE_NS);

    xout.writeStartTag(Constants.SOAP_ENVELOPE_NS, "Envelope");

    if (headers.size() > 0) {
      writeHeaders(xout);
    }

    writeBody(xout, requestElement, request);

    xout.writeEndTag(Constants.SOAP_ENVELOPE_NS, "Envelope");

    xout.endDocument();
    xout.close();
  }
コード例 #9
0
  private static HttpURLConnection createConnection(
      ConnectorConfig config,
      URL url,
      HashMap<String, String> httpHeaders,
      boolean enableCompression)
      throws IOException {

    if (config.isTraceMessage()) {
      config
          .getTraceStream()
          .println(
              "WSC: Creating a new connection to "
                  + url
                  + " Proxy = "
                  + config.getProxy()
                  + " username "
                  + config.getProxyUsername());
    }

    HttpURLConnection connection = (HttpURLConnection) url.openConnection(config.getProxy());
    connection.addRequestProperty("User-Agent", VersionInfo.info());

    /*
     * Add all the client specific headers here
     */
    if (config.getHeaders() != null) {
      for (Entry<String, String> ent : config.getHeaders().entrySet()) {
        connection.setRequestProperty(ent.getKey(), ent.getValue());
      }
    }

    if (enableCompression && config.isCompression()) {
      connection.addRequestProperty("Content-Encoding", "gzip");
      connection.addRequestProperty("Accept-Encoding", "gzip");
    }

    if (config.getProxyUsername() != null) {
      String token = config.getProxyUsername() + ":" + config.getProxyPassword();
      String auth = "Basic " + new String(Base64.encode(token.getBytes()));
      connection.addRequestProperty("Proxy-Authorization", auth);
      connection.addRequestProperty("Https-Proxy-Authorization", auth);
    }

    if (httpHeaders != null) {
      for (Map.Entry<String, String> entry : httpHeaders.entrySet()) {
        connection.addRequestProperty(entry.getKey(), entry.getValue());
      }
    }

    if (config.getReadTimeout() != 0) {
      connection.setReadTimeout(config.getReadTimeout());
    }

    if (config.getConnectionTimeout() != 0) {
      connection.setConnectTimeout(config.getConnectionTimeout());
    }

    return connection;
  }