@Test
  public void testCanParseBoxServerError()
      throws BoxRestException, IllegalStateException, IOException, BoxJSONException {
    BoxJSONParser jsonParser = new BoxJSONParser(new BoxResourceHub());
    EasyMock.reset(boxResponse, response, entity);
    inputStream =
        new ByteArrayInputStream(jsonParser.convertBoxObjectToJSONString(error).getBytes());
    EasyMock.expect(boxResponse.getHttpResponse()).andStubReturn(response);
    EasyMock.expect(response.getEntity()).andStubReturn(entity);
    EasyMock.expect(entity.getContent()).andStubReturn(inputStream);
    EasyMock.expect(entity.isStreaming()).andStubReturn(false);

    EasyMock.expect(boxResponse.getHttpResponse()).andStubReturn(response);
    EasyMock.expect(response.getStatusLine()).andStubReturn(statusLine);
    EasyMock.expect(statusLine.getStatusCode()).andStubReturn(statusCode);

    EasyMock.replay(boxResponse, response, entity, statusLine);
    ErrorResponseParser parser = new ErrorResponseParser(jsonParser);
    Object object = parser.parse(boxResponse);
    Assert.assertEquals(BoxServerError.class, object.getClass());

    Assert.assertEquals(
        jsonParser.convertBoxObjectToJSONString(error),
        jsonParser.convertBoxObjectToJSONString(object));
    EasyMock.verify(boxResponse, response, entity, statusLine);
  }
 @Override
 public boolean isStreaming() {
   return delegate.isStreaming();
 }
Exemplo n.º 3
0
  /*
   * 下载验证码
   */
  public String downloadFile(
      HttpClient httpclient,
      String url,
      String dataPath,
      String picName,
      String hostUrl,
      String refererUrl)
      throws Exception {
    String authcodePath = InfoUtil.getInstance().getInfo("road", "authcodePath");
    String path1 = authcodePath + "/" + dataPath;

    File file2 = new File(path1);
    if (!file2.exists() && !file2.isDirectory()) {
      file2.mkdir();
    }
    String destfilename = path1 + "/" + picName;
    HttpGet httpget = new HttpGet(url);
    httpget.setHeader("Accept", "image/png, image/svg+xml, image/*;q=0.8, */*;q=0.5");
    httpget.setHeader("Accept-Encoding", "gzip, deflate");
    httpget.setHeader("Accept-Language", "zh-CN");

    if (hostUrl != null && hostUrl.length() > 0) {
      httpget.setHeader("Host", hostUrl);
    }

    if (refererUrl != null && refererUrl.length() > 0) {
      httpget.setHeader("Referer", refererUrl);
    }

    httpget.setHeader(
        "User-Agent", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0)");

    File file = new File(destfilename);
    if (file.exists()) {
      file.delete();
    }

    HttpResponse response = httpclient.execute(httpget);

    if (HttpStatus.SC_OK == response.getStatusLine().getStatusCode()) {
      // 请求成功
      // 取得请求内容
      HttpEntity entity = response.getEntity();
      // 显示内容
      if (entity != null) {
        // 这里可以得到文件的类型 如image/jpg /zip /tiff 等等
        // 但是发现并不是十分有效,有时明明后缀是.rar但是取到的是null,这点特别说明
        System.out.println(entity.getContentType());
        // 可以判断是否是文件数据流
        System.out.println(entity.isStreaming());
        // 设置本地保存的文件
        FileOutputStream output = new FileOutputStream(destfilename);
        // 得到网络资源并写入文件
        InputStream input = entity.getContent();
        byte b[] = new byte[1024];
        int j = 0;
        while ((j = input.read(b)) != -1) {
          output.write(b, 0, j);
        }
        output.flush();
        output.close();
      }
      if (entity != null) {
        entity.consumeContent();
      }
    }

    PicUpload fupload = new PicUpload();
    fupload.upload(file.getAbsolutePath());

    return picName;
  }
 @Override
 public boolean isStreaming() {
   return src.isStreaming();
 }
    public String handleResponse(HttpResponse response) throws HttpResponseException, IOException {
      String clientResponse = null;

      if (client != null) {
        TitaniumHttpClient c = client.get();
        if (c != null) {
          c.response = response;
          c.setReadyState(READY_STATE_LOADED, syncId);
          c.setStatus(response.getStatusLine().getStatusCode());
          c.setStatusText(response.getStatusLine().getReasonPhrase());
          c.setReadyState(READY_STATE_INTERACTIVE, syncId);
        }

        if (DBG) {
          try {
            Log.w(LCAT, "Entity Type: " + response.getEntity().getClass());
            Log.w(LCAT, "Entity Content Type: " + response.getEntity().getContentType().getValue());
            Log.w(LCAT, "Entity isChunked: " + response.getEntity().isChunked());
            Log.w(LCAT, "Entity isStreaming: " + response.getEntity().isStreaming());
          } catch (Throwable t) {
            // Ignore
          }
        }

        StatusLine statusLine = response.getStatusLine();
        if (statusLine.getStatusCode() >= 300) {
          throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase());
        }

        entity = response.getEntity();

        if (c.onDataStreamCallback != null) {
          is = entity.getContent();

          if (is != null) {
            final String cb = c.onDataStreamCallback;
            final TitaniumWebView webView = softWebView.get();

            long contentLength = entity.getContentLength();
            if (DBG) {
              Log.d(LCAT, "Content length: " + contentLength);
            }
            int count = 0;
            int totalSize = 0;
            byte[] buf = new byte[4096];
            if (DBG) {
              Log.d(LCAT, "Available: " + is.available());
            }
            if (aborted) {
              if (entity != null) {
                entity.consumeContent();
              }
            } else {
              while ((count = is.read(buf)) != -1) {
                totalSize += count;
                if (webView != null) {
                  TitaniumModuleManager tmm = weakTmm.get();
                  if (tmm != null) {
                    try {
                      JSONObject o = new JSONObject();
                      o.put("totalCount", contentLength);
                      o.put("totalSize", totalSize);
                      o.put("size", count);
                      byte[] newbuf = new byte[count];
                      for (int i = 0; i < count; i++) {
                        newbuf[i] = buf[i];
                      }
                      TitaniumMemoryBlob blob = new TitaniumMemoryBlob(newbuf);
                      int key = tmm.cacheObject(blob);
                      o.put("key", key);
                      webView.evalJS(cb, o.toString(), syncId);
                    } catch (JSONException e) {
                      Log.e(LCAT, "Unable to send ondatastream event: ", e);
                    }
                  }
                }
                if (!entity.isStreaming()) {
                  break;
                }
              }
              if (entity != null) {
                entity.consumeContent();
              }
            }
          }
        } else {
          setResponseData(entity);
        }
      }
      return clientResponse;
    }
Exemplo n.º 6
0
 @Override
 public boolean isStreaming() {
   return httpEntity.isStreaming();
 }