com.google.common.io.CharStreams Java Examples

The following examples show how to use com.google.common.io.CharStreams. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClasspathResourceUtil.java    From Dream-Catcher with MIT License 6 votes vote down vote up
/**
 * Retrieves a classpath resource using the {@link ClasspathResourceUtil} classloader and converts it to a String using the specified
 * character set. If any error occurs while reading the resource, this method throws
 * {@link net.lightbody.bmp.mitm.exception.UncheckedIOException}. If the classpath resource cannot be found, this
 * method throws a FileNotFoundException wrapped in an UncheckedIOException.
 *
 * @param resource classpath resource to load
 * @param charset charset to use to decode the classpath resource
 * @return a String
 * @throws UncheckedIOException if the classpath resource cannot be found or cannot be read for any reason
 */
public static String classpathResourceToString(String resource, Charset charset) throws UncheckedIOException {
    if (resource == null) {
        throw new IllegalArgumentException("Classpath resource to load cannot be null");
    }

    if (charset == null) {
        throw new IllegalArgumentException("Character set cannot be null");
    }

    try {
        InputStream resourceAsStream = ClasspathResourceUtil.class.getResourceAsStream(resource);
        if (resourceAsStream == null) {
            throw new UncheckedIOException(new FileNotFoundException("Unable to locate classpath resource: " + resource));
        }

        // the classpath resource was found and opened. wrap it in a Reader and return its contents.
        Reader resourceReader = new InputStreamReader(resourceAsStream, charset);

        return CharStreams.toString(resourceReader);
    } catch (IOException e) {
        throw new UncheckedIOException("Error occurred while reading classpath resource", e);
    }
}
 
Example #2
Source File: AdminJsonService.java    From glowroot with Apache License 2.0 6 votes vote down vote up
@GET(path = "/backend/admin/json", permission = "admin:view")
String getAllAdmin() throws Exception {
    Object config;
    if (central) {
        config = configRepository.getAllCentralAdminConfig();
    } else {
        config = configRepository.getAllEmbeddedAdminConfig();
    }
    ObjectNode rootNode = mapper.valueToTree(config);
    AllAdminConfigUtil.removePasswords(rootNode);
    ObjectMappers.stripEmptyContainerNodes(rootNode);
    StringBuilder sb = new StringBuilder();
    JsonGenerator jg = mapper.getFactory().createGenerator(CharStreams.asWriter(sb));
    try {
        jg.setPrettyPrinter(ObjectMappers.getPrettyPrinter());
        jg.writeObject(rootNode);
    } finally {
        jg.close();
    }
    // newline is not required, just a personal preference
    sb.append(ObjectMappers.NEWLINE);
    return sb.toString();
}
 
Example #3
Source File: ApiDefinitionCommand.java    From apiman-cli with Apache License 2.0 6 votes vote down vote up
@Override
public void performFinalAction(JCommander parser) throws CommandException {
    if (!definitionStdIn && null == definitionFile) {
        throw new ExitWithCodeException(1, "API definition must be provided", true);
    }

    // read definition from STDIN or file
    String definition;
    try (InputStream is = (definitionStdIn ? System.in : Files.newInputStream(definitionFile))) {
        definition = CharStreams.toString(new InputStreamReader(is));

    } catch (IOException e) {
        throw new CommandException(e);
    }

    LOGGER.debug("Adding definition to API '{}' with contents: {}", this::getModelName, () -> definition);

    ManagementApiUtil.invokeAndCheckResponse(() ->
            getManagerConfig().buildServerApiClient(VersionAgnosticApi.class, serverVersion).setDefinition(orgName, name, version, definitionType, new TypedString(definition)));
}
 
Example #4
Source File: CacheStoreTest.java    From packagedrone with Eclipse Public License 1.0 6 votes vote down vote up
private static String streamed ( final MetaKey key, final Streamer func ) throws IOException
{
    final Holder<String> result = new Holder<> ();

    final boolean found = func.streamer ( key, ( stream ) -> {
        final InputStreamReader reader = new InputStreamReader ( stream, StandardCharsets.UTF_8 );
        result.value = CharStreams.toString ( reader );
    } );

    if ( !found )
    {
        return null;
    }

    return result.value;
}
 
Example #5
Source File: YamlConfigLoader.java    From digdag with Apache License 2.0 6 votes vote down vote up
ObjectNode loadParameterizedInclude(Path path, Config params)
    throws IOException
{
    String content;
    try (InputStream in = Files.newInputStream(path)) {
        content = CharStreams.toString(new InputStreamReader(in, StandardCharsets.UTF_8));
    }

    Yaml yaml = new Yaml(new YamlParameterizedConstructor(), new Representer(), new DumperOptions(), new YamlTagResolver());
    ObjectNode object = normalizeValidateObjectNode(yaml.load(content));

    Path includeDir = path.toAbsolutePath().getParent();
    if (includeDir == null) {
        throw new FileNotFoundException("Loading file named '/' is invalid");
    }

    return new ParameterizeContext(includeDir, params).evalObjectRecursive(object);
}
 
Example #6
Source File: DetectorFactory.java    From weslang with Apache License 2.0 6 votes vote down vote up
public static void loadDefaultProfiles() throws LangDetectException,
                                                IOException,
                                                UnsupportedEncodingException {
    // Return immediately in case profiles were already loaded.
    if (DetectorFactory.getLangList().size() != 0) {
        return;
    }
    InputStream is = DetectorFactory.class.getResourceAsStream(profilesFile);
    List<String> languages = CharStreams.readLines(
        new InputStreamReader(is, StandardCharsets.UTF_8));
    List<String> jsonProfiles = new ArrayList<String>();
    for (String language : languages) {
        jsonProfiles.add(
            getStringFromFile(profilesPath + language));
    }
    if (jsonProfiles.size() > 1) {
        DetectorFactory.loadProfile(jsonProfiles);
    }
}
 
Example #7
Source File: PosixFileOperations.java    From util with Apache License 2.0 6 votes vote down vote up
public static int getPID() throws IOException {
    Process proc = Runtime.getRuntime().exec(new String[]{"bash", "-c", "echo $PPID"});
    try {
        int result = proc.waitFor();
        if (result != 0) {
            throw new IOException(
                    formatWithStdStreams(
                            proc,
                            "error running bash -c \"echo $PPID\", exit code: " + result + "\nstdout:\n%s\nstderr:\n%s"
                    ));
        }
        StringWriter out = new StringWriter();
        CharStreams.copy(new InputStreamReader(proc.getInputStream(), Charsets.UTF_8), out);
        return Integer.parseInt(out.toString().trim());
    } catch (InterruptedException e) {
        log.error("exception during exec", e);
        throw new IOException("exec failed", e);
    }
}
 
Example #8
Source File: PosixFileOperations.java    From util with Apache License 2.0 6 votes vote down vote up
public static String lsla(File file) throws IOException {
    Process proc = Runtime.getRuntime().exec(new String[]{"ls", "-la"}, null, file);
    try {
        final int result = proc.waitFor();
        if (result != 0) {
            throw new IOException(
                    formatWithStdStreams(
                            proc,
                            "error running ls -la process, exit code: " + result + "\nstdout:\n%s\nstderr:\n%s"
                    )
            );
        }
        final StringWriter out = new StringWriter();
        CharStreams.copy(new InputStreamReader(proc.getInputStream(), Charsets.UTF_8), out);
        return out.toString();
    } catch (InterruptedException e) {
        log.error("exception during exec", e);
        throw new IOException("exec failed", e);
    }
}
 
Example #9
Source File: WechatMpMessageController.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
/**
 * Receiving messages sent by the wechat server
 *
 * @param request
 * @param response
 * @throws IOException
 * @throws Exception
 */
@PostMapping(path = URI_S_WECHAT_MP_RECEIVE, consumes = { "application/xml;charset=UTF-8",
		"text/xml;charset=UTF-8" }, produces = { "application/xml;charset=UTF-8", "text/xml;charset=UTF-8" })
public void postReceive(@NotBlank @RequestParam(name = "signature") String signature,
		@NotBlank @RequestParam(name = "timestamp") String timestamp, @NotBlank @RequestParam(name = "nonce") String nonce,
		@RequestParam(name = "openid", required = false) String openId, HttpServletRequest request,
		HttpServletResponse response) throws IOException {
	log.info("Receive from WeChat post [{}]", getFullRequestURI(request));
	// Validation
	assertionSignature(signature, timestamp, nonce);

	// Processing
	String input = CharStreams.toString(new InputStreamReader(request.getInputStream(), Charsets.UTF_8));
	String output = onReceive(input);
	log.info("Reply to WeChat Server => {}", output);

	write(response, HttpServletResponse.SC_OK, MediaType.APPLICATION_XML_UTF_8.toString(), output.getBytes(Charsets.UTF_8));
}
 
Example #10
Source File: Closure_31_Compiler_s.java    From coming with MIT License 6 votes vote down vote up
/** Load a library as a resource */
@VisibleForTesting
Node loadLibraryCode(String resourceName) {
  String originalCode;
  try {
    originalCode = CharStreams.toString(new InputStreamReader(
        Compiler.class.getResourceAsStream(
            String.format("js/%s.js", resourceName)),
        Charsets.UTF_8));
  } catch (IOException e) {
    throw new RuntimeException(e);
  }

  return Normalize.parseAndNormalizeSyntheticCode(
      this, originalCode,
      String.format("jscomp_%s_", resourceName));
}
 
Example #11
Source File: DefaultSecurityProviderTool.java    From Dream-Catcher with MIT License 6 votes vote down vote up
@Override
public X509Certificate decodePemEncodedCertificate(Reader certificateReader) {
    // JCA supports reading PEM-encoded X509Certificates fairly easily, so there is no need to use BC to read the cert
    Certificate certificate;

    // the JCA CertificateFactory takes an InputStream, so convert the reader to a stream first. converting to a String first
    // is not ideal, but is relatively straightforward. (PEM certificates should only contain US_ASCII-compatible characters.)
    try {
        InputStream certificateAsStream = new ByteArrayInputStream(CharStreams.toString(certificateReader).getBytes(Charset.forName("US-ASCII")));
        CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
        certificate = certificateFactory.generateCertificate(certificateAsStream);
    } catch (CertificateException | IOException e) {
        throw new ImportException("Unable to read PEM-encoded X509Certificate", e);
    }

    if (!(certificate instanceof X509Certificate)) {
        throw new ImportException("Attempted to import non-X.509 certificate as X.509 certificate");
    }

    return (X509Certificate) certificate;
}
 
Example #12
Source File: PmdTemplateTest.java    From sonar-p3c-pmd with MIT License 6 votes vote down vote up
@Test
public void should_process_input_file() throws Exception {
  doAnswer(new Answer<Void>() {
    @Override
    public Void answer(InvocationOnMock invocation) throws Throwable {
      InputStream inputStreamArg = (InputStream) invocation.getArguments()[0];
      List<String> inputStreamLines = CharStreams.readLines(new InputStreamReader(inputStreamArg));
      assertThat(inputStreamLines).containsExactly("Example source");
      return null;
    }
  }).when(processor).processSourceCode(any(InputStream.class), eq(rulesets), eq(ruleContext));
  
  new PmdTemplate(configuration, processor).process(inputFile, rulesets, ruleContext);

  verify(ruleContext).setSourceCodeFilename(inputFile.getAbsolutePath());
  verify(processor).processSourceCode(any(InputStream.class), eq(rulesets), eq(ruleContext));
}
 
Example #13
Source File: AbstractLexerTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private LinkedHashMap<Integer, String> tokenNames(Reader tokensReader) throws Exception {
	LinkedHashMap<Integer, String> result = new LinkedHashMap<>();
	List<String> lines = CharStreams.readLines(tokensReader);
	for (String line : lines) {
		String[] s = line.split("=");
		String name = s[0];
		int index = Integer.parseInt(s[1]);
		String nameOrKEYWORD = null;
		if (name.startsWith("KEYWORD")) {
			nameOrKEYWORD = "KEYWORD";
		} else {
			nameOrKEYWORD = name;
		}
		result.put(index, nameOrKEYWORD);
	}
	return result;
}
 
Example #14
Source File: UploaderTest.java    From connector-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void testUrlInputStreamContent() throws Exception {
  String fileContent = "This is the file content";
  File file = tempFolder.newFile("content.txt");
  try (FileWriter writer = new FileWriter(file)) {
    writer.write(fileContent);
  }
  Uploader uploader =
      new Uploader.Builder()
          .setServiceAccountKeyFilePath(SERVICE_ACCOUNT_FILE_PATH)
          .setUploaderHelper(uploaderHelper)
          .build();

  Uploader.UrlInputStreamContent input = uploader.new UrlInputStreamContent(
      "text/plain", file.toURI().toURL());
  assertTrue(input.retrySupported());
  assertEquals(fileContent.length(), input.getLength());
  try (InputStream in = input.getInputStream()) {
    String content = CharStreams.toString(new InputStreamReader(in, StandardCharsets.UTF_8));
    assertEquals(fileContent, content);
  }
}
 
Example #15
Source File: Shell.java    From kurento-java with Apache License 2.0 6 votes vote down vote up
public static String runAndWaitNoLog(final String... command) {
  Process p;
  try {
    p = new ProcessBuilder(command).redirectErrorStream(true).start();

    String output = CharStreams.toString(new InputStreamReader(p.getInputStream(), "UTF-8"));

    p.destroy();

    return output;

  } catch (IOException e) {
    throw new KurentoException(
        "Exception executing command on the shell: " + Arrays.toString(command), e);
  }
}
 
Example #16
Source File: Output.java    From immutables with Apache License 2.0 5 votes vote down vote up
private void readExistingEntriesInto(Collection<String> services) {
  try {
    FileObject existing = getFiler().getResource(StandardLocation.CLASS_OUTPUT, key.packageName, key.relativeName);
    FluentIterable.from(CharStreams.readLines(existing.openReader(true)))
        .filter(Predicates.not(Predicates.contains(SERVICE_FILE_COMMENT_LINE)))
        .copyInto(services);
  } catch (Exception ex) {
    // unable to read existing file
  }
}
 
Example #17
Source File: UserAccessControlRequestMessageBodyReader.java    From emodb with Apache License 2.0 5 votes vote down vote up
@Override
public UserAccessControlRequest readFrom(Class<UserAccessControlRequest> type, Type genericType, Annotation[] annotations,
                  MediaType mediaType, MultivaluedMap<String, String> httpHeaders,
                  InputStream entityStream)
        throws IOException, WebApplicationException {
    String json = CharStreams.toString(new InputStreamReader(entityStream, Charsets.UTF_8));
    return JsonHelper.fromJson(json, type);
}
 
Example #18
Source File: OAuthUtils.java    From data-transfer-project with Apache License 2.0 5 votes vote down vote up
static String makeRawPostRequest(HttpTransport httpTransport, String url, HttpContent httpContent)
    throws IOException {
  HttpRequestFactory factory = httpTransport.createRequestFactory();
  HttpRequest postRequest = factory.buildPostRequest(new GenericUrl(url), httpContent);
  HttpResponse response = postRequest.execute();
  int statusCode = response.getStatusCode();
  if (statusCode != 200) {
    throw new IOException(
        "Bad status code: " + statusCode + " error: " + response.getStatusMessage());
  }
  return CharStreams
      .toString(new InputStreamReader(response.getContent(), Charsets.UTF_8));
}
 
Example #19
Source File: TransferServiceImpl.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
private Set<String> getAspects ( final ZipFile zip ) throws IOException
{
    final ZipEntry ze = zip.getEntry ( "aspects" );
    if ( ze == null )
    {
        return Collections.emptySet ();
    }

    try ( InputStream stream = zip.getInputStream ( ze ) )
    {
        final List<String> lines = CharStreams.readLines ( new InputStreamReader ( stream, StandardCharsets.UTF_8 ) );
        return new HashSet<> ( lines );
    }
}
 
Example #20
Source File: ZipEntryJavaFileObject.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the contents of the {@link ZipEntry} as a string. Ensures that the entry is read at
 * most once.
 */
private synchronized String getContentsAsString() {
  if (contents != null) {
    return contents;
  }

  try (InputStream inputStream = zipFile.getInputStream(zipEntry);
      InputStreamReader isr = new InputStreamReader(inputStream, Charsets.UTF_8)) {
    contents = CharStreams.toString(isr);
  } catch (IOException e) {
    throw new RuntimeException(e);
  }

  return contents;
}
 
Example #21
Source File: Streams.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
/**
 * Consider using {@link #readFullyAndClose(Reader)} instead.
 */
public static String readFully(Reader is) {
    try {
        return CharStreams.toString(is);
    } catch (IOException ioe) {
        throw Exceptions.propagate(ioe);
    }
}
 
Example #22
Source File: ComposerJsonExtractorTest.java    From nexus-repository-composer with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void extractInfoFromZipballWithJsonComposerArchived() throws Exception {
  String expected;
  try (InputStream in = getClass().getResourceAsStream("extractInfoFromZipballWithJsonComposerArchived.composer.json")) {
    expected = CharStreams.toString(new InputStreamReader(in, StandardCharsets.UTF_8));
  }
  String actual;
  try (InputStream in = getClass().getResourceAsStream("extractInfoFromZipBallWithJsonComposerArchived.zip")) {
    when(blob.getInputStream()).thenReturn(in);
    actual = new ObjectMapper().writeValueAsString(underTest.extractFromZip(blob));
  }
  assertEquals(expected, actual, true);
}
 
Example #23
Source File: StreamUtils.java    From neural with MIT License 5 votes vote down vote up
public static String loadScript(String name) {
    try {
        return CharStreams.toString(new InputStreamReader(
                StreamUtils.class.getResourceAsStream(name), Charsets.UTF_8));
    } catch (IOException e) {
        log.error(e.getMessage(), e);
        return null;
    }
}
 
Example #24
Source File: RxOkHttpClientTest.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
@Test
public void testGet() throws Exception {

    MockResponse mockResponse = new MockResponse()
            .setBody(TEST_RESPONSE_BODY)
            .setResponseCode(StatusCode.OK.getCode());
    server.enqueue(mockResponse);

    RxHttpClient client = RxOkHttpClient.newBuilder()
            .build();

    Request request = new Request.Builder()
            .url(server.url("/").toString())
            .get()
            .build();

    Response response = client.execute(request).toBlocking().first();
    Assertions.assertThat(response.isSuccessful()).isTrue();

    InputStream inputStream = response.getBody().get(InputStream.class);
    String actualResponseBody = CharStreams.toString(new InputStreamReader(inputStream, Charsets.UTF_8));
    Assertions.assertThat(actualResponseBody).isEqualTo(TEST_RESPONSE_BODY);

    RecordedRequest recordedRequest = server.takeRequest(1, TimeUnit.MILLISECONDS);
    Assertions.assertThat(recordedRequest).isNotNull();
    Assertions.assertThat(recordedRequest.getBodySize()).isLessThanOrEqualTo(0);
}
 
Example #25
Source File: TaskResultReader.java    From scheduling with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Serializable readFrom(Class<Serializable> type, Type genericType, Annotation[] annotations,
        MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
        throws IOException, WebApplicationException {
    Closer closer = Closer.create();
    try {
        entityStream = closer.register(entityStream);
        return CharStreams.toString(new InputStreamReader(entityStream));
    } catch (IOException ioe) {
        throw closer.rethrow(ioe);
    } finally {
        closer.close();
    }
}
 
Example #26
Source File: SubprocessTextTransformer.java    From DataflowTemplates with Apache License 2.0 5 votes vote down vote up
/**
 * Loads into memory scripts from a File System from a given path. Supports any file system that
 * {@link FileSystems} supports.
 *
 * @return a collection of scripts loaded as UF8 Strings
 */
private static Collection<String> getScripts(String path) throws IOException {
  MatchResult result = FileSystems.match(path);
  checkArgument(
      result.status() == Status.OK && !result.metadata().isEmpty(),
      "Failed to match any files with the pattern: " + path);

  LOG.info("getting script!");

  List<String> scripts =
      result
          .metadata()
          .stream()
          .filter(metadata -> metadata.resourceId().getFilename().endsWith(".py"))
          .map(Metadata::resourceId)
          .map(
              resourceId -> {
                try (Reader reader =
                    Channels.newReader(
                        FileSystems.open(resourceId), StandardCharsets.UTF_8.name())) {
                  return CharStreams.toString(reader);
                } catch (IOException e) {
                  throw new UncheckedIOException(e);
                }
              })
          .collect(Collectors.toList());
  return scripts;
}
 
Example #27
Source File: WhitelistWarningsGuard.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Loads legacy warnings list from the file.
 * @return The lines of the file.
 */
// TODO(nicksantos): This is a weird API.
static Set<String> loadWhitelistedJsWarnings(Reader reader)
    throws IOException {
  Preconditions.checkNotNull(reader);
  Set<String> result = Sets.newHashSet();

  for (String line : CharStreams.readLines(reader)) {
    result.add(line);
  }

  return result;
}
 
Example #28
Source File: CSVFileManagerTest.java    From connector-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testCsvFileManagerEmptyField() throws IOException {
  File tmpfile = temporaryFolder.newFile("testEmptyField.csv");
  createFile(tmpfile, UTF_8, testCSVEmptyField);
  Properties config = new Properties();
  config.put(CSVFileManager.FILEPATH, tmpfile.getAbsolutePath());
  config.put(UrlBuilder.CONFIG_COLUMNS, "term, definition");
  config.put(UrlBuilder.CONFIG_FORMAT, "/lookup?term={0}&definition={1}");
  config.put(UrlBuilder.CONFIG_COLUMNS_TO_ESCAPE, "term, definition");
  config.put(CONTENT_TITLE, "term");
  config.put(CONTENT_HIGH, "term,definition");
  config.put(CONTENT_LOW, "author");
  config.put(CSVFileManager.UNIQUE_KEY_COLUMNS, "term");
  setupConfig.initConfig(config);

  CSVFileManager csvFileManager = CSVFileManager.fromConfiguration();
  CloseableIterable<CSVRecord> csvFile = csvFileManager.getCSVFile();
  CSVRecord csvRecord = getOnlyElement(csvFile);
  Item item = csvFileManager.createItem(csvRecord);

  ByteArrayContent content = csvFileManager.createContent(csvRecord);
  String html = CharStreams
      .toString(new InputStreamReader(content.getInputStream(), UTF_8));
  // Definition field has empty value.
  assertThat(html, containsString("<p>definition:</p>\n" + "  <h1></h1>"));
  // Empty field is OK in UrlBuilder.
  assertEquals("/lookup?term=moma%20search&definition=",
      item.getMetadata().getSourceRepositoryUrl());
}
 
Example #29
Source File: GrokDictionary.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private void addDictionaryAux(Reader reader) throws IOException {

    for (String currentFileLine : CharStreams.readLines(reader)) {

      final String currentLine = currentFileLine.trim();

      if (currentLine.length() == 0 || currentLine.startsWith("#")) {
        continue;
      }

      int entryDelimiterPosition = currentLine.indexOf(" ");

      if (entryDelimiterPosition < 0) {
        throw new GrokCompilationException("Dictionary entry (name and value) must be space-delimited: " + currentLine);
      }

      if (entryDelimiterPosition == 0) {
        throw new GrokCompilationException("Dictionary entry must contain a name. " + currentLine);
      }

      final String dictionaryEntryName = currentLine.substring(0, entryDelimiterPosition);
      final String dictionaryEntryValue = currentLine.substring(entryDelimiterPosition + 1, currentLine.length()).trim();

      if (dictionaryEntryValue.length() == 0) {
        throw new GrokCompilationException("Dictionary entry must contain a value: " + currentLine);
      }

      regexDictionary.put(dictionaryEntryName, dictionaryEntryValue);
    }
  }
 
Example #30
Source File: CustomResponseErrorHandler.java    From jeesuite-libs with Apache License 2.0 5 votes vote down vote up
@Override
public void handleError(ClientHttpResponse response) throws IOException {
	int code = response.getRawStatusCode();
	String content = CharStreams.toString(new InputStreamReader(response.getBody(), StandardCharsets.UTF_8));
	
	Map<?, ?> responseItmes = null;
	if(code == 404 && StringUtils.isNotBlank(content)){
		responseItmes = JsonUtils.toObject(content, Map.class);
		throw new JeesuiteBaseException(404, "Page Not Found["+responseItmes.get("path")+"]");
	}

	int errorCode = 500;
	String errorMsg = content;
	try {responseItmes = JsonUtils.toObject(content, Map.class);} catch (Exception e) {}
	if(responseItmes != null){
		if(responseItmes.containsKey("code")){
			errorCode = Integer.parseInt(responseItmes.get("code").toString());
		}
		if(responseItmes.containsKey("msg")){
			errorMsg = responseItmes.get("msg").toString();
		}else if(responseItmes.containsKey("message")){
			errorMsg = responseItmes.get("message").toString();
		}
	}
	
	if(StringUtils.isBlank(errorMsg)){
		errorMsg = DEFAULT_ERROR_MSG;
	}
	
	throw new JeesuiteBaseException(errorCode, errorMsg + "(Remote)");
}