public String toString() {
   String postData = mConn.getPostData() == null ? "null" : new String(mConn.getPostData());
   return "url: "
       + mConn.getRequestUrl()
       + "\nheaders: "
       + mConn.getRequestHeaders()
       + "\npostData: "
       + postData;
 }
Example #2
0
  @Test
  public void connection_should_be_refreshed_on_failure()
      throws JMSException, InterruptedException {
    // Initialization
    underTest.start();
    connection = Whitebox.getInternalState(underTest, "connection");
    Assertions.assertThat(connection).isNotNull();
    // Mock the created session
    ManagedSession session = Mockito.mock(ManagedSession.class);
    Whitebox.setInternalState(underTest, "sessions", Lists.newArrayList(session));

    // Failure
    Object jmsFactoryImpl = Whitebox.getInternalState(underTest, "jmsFactoryImpl");
    Whitebox.setInternalState(underTest, "jmsFactoryImpl", new FakeConnectionFactoryImpl());
    underTest.setExceptionListener(
        new javax.jms.ExceptionListener() {

          @Override
          public void onException(JMSException exception) {}
        });
    underTest.onException(new JMSException("Connection closed"));

    // Reset
    connection = Whitebox.getInternalState(underTest, "connection");
    Assertions.assertThat(connection).isNull();
    Mockito.verify(session, Mockito.times(1)).reset(); // session is reset on cascade

    // The connection is back
    Whitebox.setInternalState(underTest, "jmsFactoryImpl", jmsFactoryImpl);

    // wait for the timer to refresh the connection
    Thread.sleep(200);

    connection = Whitebox.getInternalState(underTest, "connection"); // connection is back
    Assertions.assertThat(connection).isNotNull();
    Mockito.verify(session, Mockito.times(1)).refresh(connection); // session is refreshed
  }
Example #3
0
 @Test
 public void
     test_that_wraped_exceptionlistener_from_managedConnection_differs_from_jmsbroker_connection()
         throws InterruptedException, JMSException {
   Whitebox.setInternalState(underTest, "jmsFactoryImpl", new FakeConnectionFactoryImpl());
   javax.jms.ExceptionListener exceptionListener =
       new javax.jms.ExceptionListener() {
         @Override
         public void onException(JMSException e) {}
       };
   underTest.setExceptionListener(exceptionListener);
   Connection jmsConnection = Whitebox.getInternalState(underTest, "connection");
   javax.jms.ExceptionListener exceptionListenerAQ = jmsConnection.getExceptionListener();
   javax.jms.ExceptionListener exceptionListenerMC =
       Whitebox.getInternalState(underTest, "exceptionListener");
   Assertions.assertThat(exceptionListenerAQ).isNotEqualTo(exceptionListenerMC);
   Assertions.assertThat(exceptionListenerMC).isEqualTo(exceptionListener);
 }
 /**
  * Gets the URL that this HTTP response (or error) corresponds to.
  *
  * @return The URL that this HTTP response (or error) corresponds to.
  */
 /*package*/ String getUrl() {
   return mManagedConnection.getRequestUrl();
 }
    public void run() {
      try {
        HttpUriRequest request = mConn.getHttpRequest();
        InetAddress ia = InetAddress.getByName(request.getURI().getHost());
        LogUtils.logd("Requesting: " + mConn.getRequestUrl() + " (" + ia.getHostAddress() + ")");

        request.addHeader("User-Agent", USER_AGENT);
        for (NameValuePair header : mConn.getRequestHeaders()) {
          request.addHeader(header.getName(), header.getValue());
        }

        LogUtils.logd(
            "Headers: " + Arrays.asList(mConn.getHttpRequest().getAllHeaders()).toString());
        if (mConn.getHttpRequest() instanceof HttpPost) {
          String postBody = EntityUtils.toString(((HttpPost) mConn.getHttpRequest()).getEntity());
          LogUtils.logd("POST to " + mConn.getRequestUrl() + ": " + postBody);
        }

        HttpResponse response;
        try {
          response = mHttpClient.execute(request);
        } catch (IOException e) {
          // XXX Mediocre way to match exceptions from aborted requests:
          if (request.isAborted() && e.getMessage().contains("abort")) {
            throw new AbortedRequestException();
          } else {
            throw e;
          }
        }

        if (request.isAborted()) throw new AbortedRequestException();

        // Fetching the status code allows the response interceptor to have a chance to un-gzip the
        // entity before we fetch it.
        response.getStatusLine().getStatusCode();

        HttpResponseHeaders headers = HttpResponseHeaders.fromResponse(response, request);

        HttpEntity entity = response.getEntity();
        byte[] responseBody;
        if (entity == null) {
          responseBody = new byte[0];
        } else {
          responseBody = EntityUtils.toByteArray(entity);
          entity.consumeContent();

          final Header encoding = entity.getContentEncoding();
          if (encoding != null) {
            for (HeaderElement element : encoding.getElements()) {
              if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) {
                GZIPInputStream gis = new GZIPInputStream(new ByteArrayInputStream(responseBody));
                responseBody = IoUtils.readAndClose(gis, true);
                break;
              }
            }
          }
        }

        AsyncHttpResponse ahr;
        String statusLine = response.getStatusLine().toString();
        String bodyStr = new String(responseBody);
        int bodySubStrLen = bodyStr.length() > 300 ? 300 : bodyStr.length();

        switch (response.getStatusLine().getStatusCode()) {
          case HttpStatus.SC_OK:
            // Normal success
          case HttpStatus.SC_NOT_MODIFIED:
            // From mobile_config_and_baseurl called with an Etag
          case HttpStatus.SC_MOVED_PERMANENTLY:
          case HttpStatus.SC_SEE_OTHER:
          case HttpStatus.SC_TEMPORARY_REDIRECT:
          case HttpStatus.SC_MOVED_TEMPORARILY:
            // for UPS-1390 - don't error on 302s from token URL
          case HttpStatus.SC_CREATED:
            // Response from the Engage trail creation and maybe URL shortening calls
            LogUtils.logd(statusLine + ": " + bodyStr.substring(0, bodySubStrLen));
            ahr = new AsyncHttpResponse(mConn, null, headers, responseBody);
            break;
          default:
            LogUtils.loge(statusLine + "\n" + bodyStr.substring(0, bodySubStrLen));
            ahr = new AsyncHttpResponse(mConn, new Exception(statusLine), headers, responseBody);
        }

        mConn.setResponse(ahr);
        invokeCallback(callBack);
      } catch (IOException e) {
        LogUtils.loge(this.toString());
        LogUtils.loge("IOException while executing HTTP request.", e);
        mConn.setResponse(new AsyncHttpResponse(mConn, e, null, null));
        invokeCallback(callBack);
      } catch (AbortedRequestException e) {
        LogUtils.loge("Aborted request: " + mConn.getRequestUrl());
        mConn.setResponse(new AsyncHttpResponse(mConn, null, null, null));
        invokeCallback(callBack);
      }
    }