List of usage examples for org.apache.commons.io Charsets UTF_8
Charset UTF_8
To view the source code for org.apache.commons.io Charsets UTF_8.
Click Source Link
Eight-bit Unicode Transformation Format.
From source file:org.jenkinsci.plugins.workflow.cps.CpsScript.java
@Override public Object getProperty(String property) { if (property.equals(PROP_ENV)) { return $env(); } else if (property.equals(PROP_BUILD)) { try {//from w ww . ja v a 2 s. c om return new RunWrapper($build(), true); } catch (IOException x) { throw new InvokerInvocationException(x); } } else if (property.equals("foobar")) {// Test to get the point across GroovyShell shell = execution.getShell(); return shell .parse(new InputStreamReader(shell.getClassLoader().getResourceAsStream("test/FooBar.groovy"), Charsets.UTF_8), "test/FooBar.groovy"); } return super.getProperty(property); }
From source file:org.kaaproject.kaa.client.transport.AndroidHttpClient.java
private byte[] getResponseBody(HttpResponse response, boolean verifyResponse) throws IOException, GeneralSecurityException { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { byte[] body = toByteArray(resEntity); consume(resEntity);//from w w w . j a v a 2 s .c o m if (verifyResponse) { Header signatureHeader = response.getFirstHeader(CommonEpConstans.SIGNATURE_HEADER_NAME); if (signatureHeader == null) { throw new IOException("can't verify message"); } byte[] signature; if (signatureHeader.getValue() != null) { signature = android.util.Base64.decode(signatureHeader.getValue().getBytes(Charsets.UTF_8), android.util.Base64.DEFAULT); } else { signature = new byte[0]; } // LOG.debug("Remote Public Key: {}" + // messageEncDec.getRemotePublicKey().getEncoded().length); // LOG.debug(MessageEncoderDecoder.bytesToHex(messageEncDec.getRemotePublicKey() // .getEncoded())); // LOG.debug("Signature size: {}" + signature.length); // LOG.debug(MessageEncoderDecoder.bytesToHex(signature)); // LOG.debug("Body size: {}" + body.length); // LOG.debug(MessageEncoderDecoder.bytesToHex(body)); return verifyResponse(body, signature); } else { return body; } } else { throw new IOException("can't read message"); } }
From source file:org.kaaproject.kaa.client.transport.DesktopHttpClient.java
private byte[] getResponseBody(HttpResponse response, boolean verifyResponse) throws IOException, GeneralSecurityException { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { byte[] body = EntityUtils.toByteArray(resEntity); EntityUtils.consume(resEntity);// www .j a v a2 s .c o m if (verifyResponse) { Header signatureHeader = response.getFirstHeader(CommonEpConstans.SIGNATURE_HEADER_NAME); if (signatureHeader == null) { throw new IOException("can't verify message"); } byte[] signature; if (signatureHeader.getValue() != null) { signature = Base64.decodeBase64(signatureHeader.getValue().getBytes(Charsets.UTF_8)); } else { signature = new byte[0]; } return verifyResponse(body, signature); } else { return body; } } else { throw new IOException("can't read message"); } }
From source file:org.mitre.mpf.rest.client.CustomRestClient.java
public <T> T customPostObject(String urlStr, Object objToSend, Class<T> entityClass) throws IOException, RestClientException { HttpPost post = contentTypeJson(newHttpPost(urlStr)); HttpEntity entity = new StringEntity(toJson(objToSend).toString(), Charsets.UTF_8); post.setEntity(entity);//from w ww.j a v a 2 s . c om HttpResponse response = execute(interceptor, post, 201 /*expectedStatus*/); //201 for post //System.out.println(Arrays.toString(response.getAllHeaders())); String jsonContentStr = contentAsString(response); //System.out.println("response: " + jsonContentStr); if (jsonContentStr != null) { return bindObject(jsonContentStr, entityClass); } else { return null; } }
From source file:org.nuxeo.apidoc.introspection.ComponentInfoImpl.java
@Override public String getXmlFileContent() { if (xmlFileUrl == null) { return ""; }/*from ww w .j av a2 s.co m*/ String path = xmlFileUrl.getPath(); String[] parts = path.split("!"); File jar = new File(parts[0].replace("file:", "")); if (!jar.exists()) { return "Unable to locate Bundle :" + parts[0]; } try { String xml; if (jar.getAbsolutePath().endsWith(".xml")) { try (InputStream in = new FileInputStream(jar)) { xml = IOUtils.toString(in, Charsets.UTF_8); } } else if (jar.isDirectory()) { File file = new File(new Path(jar.getAbsolutePath()).append(parts[1]).toString()); if (!file.exists()) { return "Unable to locate file :" + file.getAbsolutePath(); } xml = FileUtils.readFileToString(file); } else { try (ZipFile jarArchive = new ZipFile(jar)) { ZipEntry entry = jarArchive.getEntry(parts[1].substring(1)); xml = IOUtils.toString(jarArchive.getInputStream(entry), Charsets.UTF_8); } } return DocumentationHelper.secureXML(xml); } catch (IOException e) { log.error("Error while getting XML file", e); return ""; } }
From source file:org.nuxeo.apidoc.introspection.EmbeddedDocExtractor.java
public static void extractEmbeddedDoc(ZipFile jarFile, BundleInfoImpl bi) throws IOException { Enumeration<? extends ZipEntry> entries = jarFile.entries(); Map<String, ResourceDocumentationItem> localDocs = new HashMap<>(); Map<String, ResourceDocumentationItem> parentDocs = new HashMap<>(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); try (InputStream is = jarFile.getInputStream(entry)) { if (entry.getName().startsWith(PARENT_DOC_PREFIX) && !entry.isDirectory()) { String content = IOUtils.toString(is, Charsets.UTF_8); String name = new Path(entry.getName()).lastSegment(); if (name.length() >= 6 && name.substring(0, 6).equalsIgnoreCase("readme")) { ResourceDocumentationItem docItem = new ResourceDocumentationItem(name, content, DefaultDocumentationType.DESCRIPTION.toString(), bi); parentDocs.put(DefaultDocumentationType.DESCRIPTION.toString(), docItem); } else { ResourceDocumentationItem docItem = new ResourceDocumentationItem(name, content, DefaultDocumentationType.HOW_TO.toString(), bi); parentDocs.put(DefaultDocumentationType.HOW_TO.toString(), docItem); }/*from ww w .j a va 2s . c o m*/ } if (entry.getName().startsWith(DOC_PREFIX) && !entry.isDirectory()) { String content = IOUtils.toString(is, Charsets.UTF_8); String name = new Path(entry.getName()).lastSegment(); if (name.length() >= 6 && name.substring(0, 6).equalsIgnoreCase("readme")) { ResourceDocumentationItem docItem = new ResourceDocumentationItem(name, content, DefaultDocumentationType.DESCRIPTION.toString(), bi); localDocs.put(DefaultDocumentationType.DESCRIPTION.toString(), docItem); } else { ResourceDocumentationItem docItem = new ResourceDocumentationItem(name, content, DefaultDocumentationType.HOW_TO.toString(), bi); localDocs.put(DefaultDocumentationType.HOW_TO.toString(), docItem); } } } } bi.setLiveDoc(localDocs); bi.setParentLiveDoc(parentDocs); }
From source file:org.nuxeo.apidoc.introspection.ServerInfo.java
protected static BundleInfoImpl computeBundleInfo(Bundle bundle) { RuntimeService runtime = Framework.getRuntime(); BundleInfoImpl binfo = new BundleInfoImpl(bundle.getSymbolicName()); binfo.setFileName(runtime.getBundleFile(bundle).getName()); binfo.setLocation(bundle.getLocation()); if (!(bundle instanceof BundleImpl)) { return binfo; }/*from w w w. j a va2 s. co m*/ BundleImpl nxBundle = (BundleImpl) bundle; File jarFile = nxBundle.getBundleFile().getFile(); if (jarFile == null) { return binfo; } try { if (jarFile.isDirectory()) { // directory: run from Eclipse in unit tests // .../nuxeo-runtime/nuxeo-runtime/bin // or sometimes // .../nuxeo-runtime/nuxeo-runtime/bin/main File manifest = new File(jarFile, META_INF_MANIFEST_MF); if (manifest.exists()) { InputStream is = new FileInputStream(manifest); String mf = IOUtils.toString(is, Charsets.UTF_8); binfo.setManifest(mf); } // find and parse pom.xml File up = new File(jarFile, ".."); File pom = new File(up, POM_XML); if (!pom.exists()) { pom = new File(new File(up, ".."), POM_XML); if (!pom.exists()) { pom = null; } } if (pom != null) { DocumentBuilder b = documentBuilderFactory.newDocumentBuilder(); Document doc = b.parse(new FileInputStream(pom)); XPath xpath = xpathFactory.newXPath(); String groupId = (String) xpath.evaluate("//project/groupId", doc, XPathConstants.STRING); if ("".equals(groupId)) { groupId = (String) xpath.evaluate("//project/parent/groupId", doc, XPathConstants.STRING); } String artifactId = (String) xpath.evaluate("//project/artifactId", doc, XPathConstants.STRING); if ("".equals(artifactId)) { artifactId = (String) xpath.evaluate("//project/parent/artifactId", doc, XPathConstants.STRING); } String version = (String) xpath.evaluate("//project/version", doc, XPathConstants.STRING); if ("".equals(version)) { version = (String) xpath.evaluate("//project/parent/version", doc, XPathConstants.STRING); } binfo.setArtifactId(artifactId); binfo.setGroupId(groupId); binfo.setArtifactVersion(version); } } else { try (ZipFile zFile = new ZipFile(jarFile)) { ZipEntry mfEntry = zFile.getEntry(META_INF_MANIFEST_MF); if (mfEntry != null) { try (InputStream mfStream = zFile.getInputStream(mfEntry)) { String mf = IOUtils.toString(mfStream, Charsets.UTF_8); binfo.setManifest(mf); } } Enumeration<? extends ZipEntry> entries = zFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (entry.getName().endsWith(POM_PROPERTIES)) { try (InputStream is = zFile.getInputStream(entry)) { PropertyResourceBundle prb = new PropertyResourceBundle(is); String groupId = prb.getString("groupId"); String artifactId = prb.getString("artifactId"); String version = prb.getString("version"); binfo.setArtifactId(artifactId); binfo.setGroupId(groupId); binfo.setArtifactVersion(version); } break; } } } try (ZipFile zFile = new ZipFile(jarFile)) { EmbeddedDocExtractor.extractEmbeddedDoc(zFile, binfo); } } } catch (IOException | ParserConfigurationException | SAXException | XPathException | NuxeoException e) { log.error(e, e); } return binfo; }
From source file:org.nuxeo.common.utils.TestZipUtils.java
@Test public void testGetZipContentByFile() throws Exception { String path = "test-data/hello.odt"; File sourceFile = FileUtils.getResourceFileFromContext(path); List<String> contentNames = ZipUtils.getEntryNames(sourceFile); assertEquals("Number of elements", 9, contentNames.size()); assertTrue("Contains mimetype file", ZipUtils.hasEntry(sourceFile, "mimetype")); InputStream entryContent = ZipUtils.getEntryContentAsStream(sourceFile, "mimetype"); assertEquals("Mimetype content", "application/vnd.oasis.opendocument.text", IOUtils.toString(entryContent, Charsets.UTF_8)); // need to close returned InputStream entryContent.close();//ww w . j a v a 2 s. c o m // direct access to content - No need to close returned InputStream String directString = ZipUtils.getEntryContentAsString(sourceFile, "mimetype"); assertEquals("Mimetype content", "application/vnd.oasis.opendocument.text", directString); byte[] bytes = ZipUtils.getEntryContentAsBytes(sourceFile, "mimetype"); assertEquals("Mimetype file length", 39, bytes.length); }
From source file:org.nuxeo.common.utils.TestZipUtils.java
@Test public void testGetZipContentByByStream() throws Exception { String path = "test-data/hello.odt"; File sourceFile = FileUtils.getResourceFileFromContext(path); InputStream stream = new FileInputStream(sourceFile); List<String> contentNames = ZipUtils.getEntryNames(stream); assertEquals("Number of elements", 9, contentNames.size()); stream = new FileInputStream(sourceFile); assertTrue("Contains mimetype file", ZipUtils.hasEntry(stream, "mimetype")); stream = new FileInputStream(sourceFile); try (InputStream entryContent = ZipUtils.getEntryContentAsStream(stream, "mimetype")) { assertEquals("Mimetype content", "application/vnd.oasis.opendocument.text", IOUtils.toString(entryContent, Charsets.UTF_8)); }// w w w .java 2s. c om // direct access to content - No need to close returned InputStream stream = new FileInputStream(sourceFile); String directString = ZipUtils.getEntryContentAsString(stream, "mimetype"); assertEquals("Mimetype content", "application/vnd.oasis.opendocument.text", directString); stream = new FileInputStream(sourceFile); byte[] bytes = ZipUtils.getEntryContentAsBytes(stream, "mimetype"); assertEquals("Mimetype file length", 39, bytes.length); }
From source file:org.nuxeo.common.utils.TestZipUtils.java
@Test public void testGetZipContentBytByURL() throws Exception { String path = "test-data/hello.odt"; File sourceFile = FileUtils.getResourceFileFromContext(path); URL url = sourceFile.toURI().toURL(); List<String> contentNames = ZipUtils.getEntryNames(url); assertEquals("Number of elements", 9, contentNames.size()); assertTrue("Contains mimetype file", ZipUtils.hasEntry(url, "mimetype")); try (InputStream entryContent = ZipUtils.getEntryContentAsStream(url, "mimetype")) { assertEquals("Mimetype content", "application/vnd.oasis.opendocument.text", IOUtils.toString(entryContent, Charsets.UTF_8)); }//w w w.ja va 2 s.com // direct access to content - No need to close returned InputStream String directString = ZipUtils.getEntryContentAsString(url, "mimetype"); assertEquals("Mimetype content", "application/vnd.oasis.opendocument.text", directString); byte[] bytes = ZipUtils.getEntryContentAsBytes(url, "mimetype"); assertEquals("Mimetype file length", 39, bytes.length); }