예제 #1
0
  @Override
  public Response execute(Request request) throws IOException {
    Response response = wrappedClient.execute(request);

    boolean gzipped = false;
    for (Header h : response.getHeaders()) {
      if (h.getName() != null
          && h.getName().toLowerCase().equals("content-encoding")
          && h.getValue() != null
          && h.getValue().toLowerCase().equals("gzip")) {
        gzipped = true;
        break;
      }
    }

    Response r = null;
    if (gzipped) {
      InputStream is = null;
      ByteArrayOutputStream bos = null;

      try {
        is = new BufferedInputStream(new GZIPInputStream(response.getBody().in()));
        bos = new ByteArrayOutputStream();

        int b;
        while ((b = is.read()) != -1) {
          bos.write(b);
        }

        TypedByteArray body = new TypedByteArray(response.getBody().mimeType(), bos.toByteArray());
        r =
            new Response(
                response.getUrl(),
                response.getStatus(),
                response.getReason(),
                response.getHeaders(),
                body);
      } finally {
        if (is != null) {
          is.close();
        }
        if (bos != null) {
          bos.close();
        }
      }
    } else {
      r = response;
    }
    return r;
  }
예제 #2
0
 /**
  * handles the retrofit errors<br>
  * (which is always thrown when http status != 200)<br>
  * and print some error msg
  *
  * @param re the Retrofit error
  */
 private void handleRetrofitError(RetrofitError re) {
   Response r = re.getResponse();
   String msg = "";
   if (r != null) {
     msg = r.getStatus() + " " + r.getReason();
     if (r.getBody() != null && r.getBody().length() > 0) {
       try {
         InputStream in = r.getBody().in();
         String body = " - " + IOUtils.toString(in, "UTF-8");
         in.close();
         LOGGER.trace(body);
       } catch (IOException e1) {
         LOGGER.warn("IOException on Trakt error", e1);
       }
     }
   } else {
     msg = re.getMessage();
   }
   LOGGER.error("Trakt error (wrong settings?) " + msg);
   MessageManager.instance.pushMessage(new Message(MessageLevel.ERROR, msg, "Settings.trakttv"));
 }
  @Override
  protected IStatus run(IProgressMonitor monitor) {
    monitor.beginTask("Syncing Languages", selectLangs.length);
    for (ProjectLanguage lang : selectLangs) {
      if (monitor.isCanceled()) {
        return Status.CANCEL_STATUS;
      } else {
        try {
          Response resp =
              service.exportTranslation(projectId, lang.getCode(), "strings.xml", "strings.xml");

          if (resp.getStatus() == 204) {
            continue; // skip empty content file
          }

          String androidCode;
          if (lang.getCode().contains("-")) {
            androidCode = lang.getCode().replaceFirst("-", "-r");
          } else {
            androidCode = lang.getCode();
          }
          IFile file = project.getFile("/res/values-" + androidCode + "/strings.xml");
          prepareFolder((IFolder) file.getParent().getAdapter(IFolder.class));
          if (file.exists()) {
            file.setContents(resp.getBody().in(), true, true, monitor);
          } else {
            file.create(resp.getBody().in(), true, monitor);
          }
          // exportFile.mkdir();
          // FileUtils.copyInputStreamToFile(
          // resp.getBody().in(), exportFile);
          monitor.worked(1);
        } catch (Exception e) {
          return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Error while downloading file", e);
        }
      }
    }
    monitor.done();
    return Status.OK_STATUS;
  }
 private static ImportSummary getPostImportSummary(Response response) {
   ImportSummary importSummary = null;
   try {
     String body = new StringConverter().fromBody(response.getBody(), String.class);
     Log.d(CLASS_TAG, body);
     importSummary =
         DhisController.getInstance().getObjectMapper().readValue(body, ImportSummary.class);
   } catch (IOException e) {
     e.printStackTrace();
   } catch (ConversionException e) {
     e.printStackTrace();
   }
   return importSummary;
 }
 private static ImportSummary getPutImportSummary(Response response) {
   ApiResponse apiResponse = null;
   try {
     String body = new StringConverter().fromBody(response.getBody(), String.class);
     Log.d(CLASS_TAG, body);
     apiResponse =
         DhisController.getInstance().getObjectMapper().readValue(body, ApiResponse.class);
     if (apiResponse != null
         && apiResponse.getImportSummaries() != null
         && !apiResponse.getImportSummaries().isEmpty()) {
       return (apiResponse.getImportSummaries().get(0));
     }
   } catch (IOException e) {
     e.printStackTrace();
   } catch (ConversionException e) {
     e.printStackTrace();
   }
   return null;
 }
 private static ImportSummary getImportSummary(Response response) {
   // because the web api almost randomly gives the responses in different forms, this
   // method checks which one it is that is being returned, and parses accordingly.
   try {
     JsonNode node =
         DhisController.getInstance()
             .getObjectMapper()
             .readTree(new StringConverter().fromBody(response.getBody(), String.class));
     if (node == null) {
       return null;
     }
     if (node.has("response")) {
       return getPutImportSummary(response);
     } else {
       return getPostImportSummary(response);
     }
   } catch (IOException e) {
     e.printStackTrace();
   } catch (ConversionException e) {
     e.printStackTrace();
   }
   return null;
 }