List of usage examples for org.apache.commons.io IOUtils write
public static void write(StringBuffer data, OutputStream output, String encoding) throws IOException
StringBuffer
to bytes on an OutputStream
using the specified character encoding. From source file:ch.entwine.weblounge.dispatcher.impl.handler.RobotsRequestHandlerImpl.java
/** * {@inheritDoc}//from w ww.jav a 2 s . c o m * * @see ch.entwine.weblounge.dispatcher.RequestHandler#service(ch.entwine.weblounge.common.request.WebloungeRequest, * ch.entwine.weblounge.common.request.WebloungeResponse) */ public boolean service(WebloungeRequest request, WebloungeResponse response) { WebUrl url = request.getUrl(); String path = url.getPath(); // Is the request intended for this handler? if (!URI_PREFIX.equals(path)) { logger.debug("Skipping request for {}, request path does not start with {}", URI_PREFIX); return false; } // Check the request method. Only GET is supported right now. String requestMethod = request.getMethod().toUpperCase(); if ("OPTIONS".equals(requestMethod)) { String verbs = "OPTIONS,GET"; logger.trace("Answering options request to {} with {}", url, verbs); response.setHeader("Allow", verbs); response.setContentLength(0); return true; } else if (!"GET".equals(requestMethod)) { logger.debug("Robots request handler does not support {} requests", requestMethod); DispatchUtils.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, request, response); return true; } // Decide on which directives to send String robotsDirective = null; if (Environment.Production.equals(request.getEnvironment())) { // TODO: Get hold of the site bundle and check for an existing robots.txt robotsDirective = allowRobotsTxt; } else { robotsDirective = disallowRobotsTxt; } // Send the response try { response.setContentType("text/plain"); response.setContentLength(robotsDirective.length()); response.setModificationDate(modificationDate); IOUtils.write(robotsDirective, response.getOutputStream(), "UTF-8"); } catch (IOException e) { logger.warn("Error sending robots.txt to client: {}", e.getMessage()); } return true; }
From source file:net.sourceforge.jweb.maven.mojo.PropertiesOverideMojo.java
public void execute() throws MojoExecutionException, MojoFailureException { if (disabled) { this.getLog().info("plugin was disabled"); return;//from w w w .ja v a 2 s . c o m } processConfiguration(); if (replacements.isEmpty()) { this.getLog().info("Nothing to replace with"); return; } String name = this.builddir.getAbsolutePath() + File.separator + this.finalName + "." + this.packing;//the final package this.getLog().debug("final artifact: " + name);// the final package try { File finalWarFile = new File(name); File tempFile = File.createTempFile(finalWarFile.getName(), null); tempFile.delete();//check deletion boolean renameOk = finalWarFile.renameTo(tempFile); if (!renameOk) { getLog().error("Can not rename file, please check."); return; } ZipOutputStream out = new ZipOutputStream(new FileOutputStream(finalWarFile)); ZipFile zipFile = new ZipFile(tempFile); Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (acceptMime(entry)) { getLog().info("applying replacements for " + entry.getName()); InputStream inputStream = zipFile.getInputStream(entry); String src = IOUtils.toString(inputStream, encoding); //do replacement for (Entry<String, String> e : replacements.entrySet()) { src = src.replaceAll("#\\{" + e.getKey() + "}", e.getValue()); } out.putNextEntry(new ZipEntry(entry.getName())); IOUtils.write(src, out, encoding); inputStream.close(); } else { //not repalce, just put entry back to out zip out.putNextEntry(entry); InputStream inputStream = zipFile.getInputStream(entry); byte[] buf = new byte[512]; int len = -1; while ((len = inputStream.read(buf)) > 0) { out.write(buf, 0, len); } inputStream.close(); continue; } } zipFile.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:ch.cyberduck.core.s3.S3SingleUploadServiceTest.java
@Test public void testUploadSSE() throws Exception { final S3Session session = new S3Session(new Host(new S3Protocol(), new S3Protocol().getDefaultHostname(), new Credentials(System.getProperties().getProperty("s3.key"), System.getProperties().getProperty("s3.secret")))); session.open(new DisabledHostKeyCallback()); session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback()); final S3SingleUploadService service = new S3SingleUploadService(session, new S3WriteFeature(session, new S3DisabledMultipartService())); final Path container = new Path("test-us-east-1-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume)); final String name = UUID.randomUUID().toString() + ".txt"; final Path test = new Path(container, name, EnumSet.of(Path.Type.file)); final Local local = new Local(System.getProperty("java.io.tmpdir"), name); final String random = new RandomStringGenerator.Builder().build().generate(1000); final OutputStream out = local.getOutputStream(false); IOUtils.write(random, out, Charset.defaultCharset()); out.close();//w w w .j av a 2 s .c o m final TransferStatus status = new TransferStatus(); status.setLength(random.getBytes().length); status.setMime("text/plain"); status.setEncryption(KMSEncryptionFeature.SSE_KMS_DEFAULT); service.upload(test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(), status, new DisabledLoginCallback()); assertTrue(new S3FindFeature(session).find(test)); final PathAttributes attributes = new S3AttributesFinderFeature(session).find(test); assertEquals(random.getBytes().length, attributes.getSize()); final Map<String, String> metadata = new S3MetadataFeature(session, new S3AccessControlListFeature(session)) .getMetadata(test); assertFalse(metadata.isEmpty()); assertEquals("text/plain", metadata.get("Content-Type")); assertEquals("aws:kms", metadata.get("server-side-encryption")); assertNotNull(metadata.get("server-side-encryption-aws-kms-key-id")); new S3DefaultDeleteFeature(session).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback()); local.delete(); session.close(); }
From source file:de.tudarmstadt.ukp.csniper.resbuild.stuff.CasFlusher.java
@Override public void process(CAS aCas) throws AnalysisEngineProcessException { try {/* w ww . j a va2 s . c o m*/ int aBegin = 112715;//98877; int aEnd = 112734;//98993; OutputStream aOutputStream = System.out; Collection<? extends Annotation> annos; annos = JCasUtil.select(aCas.getJCas(), Sentence.class); Annotation a = new ArrayList<Annotation>(annos).get(92); aBegin = a.getBegin(); aEnd = a.getEnd(); if (aBegin > -1 && aEnd > -1) { annos = JCasUtil.selectCovered(aCas.getJCas(), Annotation.class, aBegin, aEnd); } else { annos = JCasUtil.select(aCas.getJCas(), Annotation.class); } for (Annotation anno : annos) { StringBuilder sb = new StringBuilder(); sb.append("[" + anno.getClass().getSimpleName() + "] "); sb.append("(" + anno.getBegin() + "," + anno.getEnd() + ") "); sb.append(anno.getCoveredText() + "\n"); try { IOUtils.write(sb, aOutputStream, "UTF-8"); } catch (IOException e) { e.printStackTrace(); } } for (PennTree pt : JCasUtil.selectCovered(aCas.getJCas(), PennTree.class, aBegin, aEnd)) { IOUtils.write(StringUtils.normalizeSpace(pt.getPennTree()), aOutputStream, "UTF-8"); } } catch (CASException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:io.inkstand.scribble.jcr.rules.util.XMLContentLoaderTest.java
/** * This tests implements an exploit to the XML External Entity Attack {@see http://www.ws-attacks.org/index.php/XML_Entity_Reference_Attack}. * The attack targets a file in the filesystem containing a secret, i.e. a password, configurations, etc. The * attacking file defines an entity that resolves to the file containing the secret. The entity (&xxe;) is used * in the xml file and will be resolved to provide the title of the test node. If the code is not vulnerable, the * attack will fail./* ww w . jav a 2s . c om*/ * * @throws Throwable */ @Test(expected = AssertionError.class) public void testLoadContent_ExternalitEntityAttack_notVulnerable() throws Throwable { //prepare //the attacked file containing the secret final File attackedFile = folder.newFile("attackedFile.txt"); try (FileOutputStream fos = new FileOutputStream(attackedFile)) { //the lead-padding of 4-chars is ignored for some mysterious reasons... IOUtils.write(" secretContent", fos, Charset.forName("UTF-8")); } //as attacker file we use a template and replacing a %s placeholder with the url of the attacked file //in a real-world attack we would use a valuable target such as /etc/passwd final File attackerFile = folder.newFile("attackerFile.xml"); //load the template file from the classpath try (InputStream is = getClass() .getResourceAsStream("XMLContentLoaderTest_inkstandJcrImport_v1-0_xxe-attack.xml"); FileOutputStream fos = new FileOutputStream(attackerFile)) { final String attackerContent = prepareAttackerContent(is, attackedFile); IOUtils.write(attackerContent, fos); } final Session actSession = repository.getAdminSession(); //act //when the code is not vulnerable, the following call will cause a runtime exception //as the dtd processing of external entities is not allowed. subject.loadContent(actSession, attackerFile.toURI().toURL()); }
From source file:ch.cyberduck.core.s3.S3ThresholdUploadServiceTest.java
@Test public void testUploadSinglePartUsEast() throws Exception { final S3Session session = new S3Session(new Host(new S3Protocol(), new S3Protocol().getDefaultHostname(), new Credentials(System.getProperties().getProperty("s3.key"), System.getProperties().getProperty("s3.secret")))); session.open(new DisabledHostKeyCallback()); session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback()); final S3ThresholdUploadService service = new S3ThresholdUploadService(session, 5 * 1024L );//w w w . j ava 2 s .c o m final Path container = new Path("test-us-east-1-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume)); final String name = UUID.randomUUID().toString(); final Path test = new Path(container, name, EnumSet.of(Path.Type.file)); final Local local = new Local(System.getProperty("java.io.tmpdir"), name); final String random = new RandomStringGenerator.Builder().build().generate(1000); IOUtils.write(random, local.getOutputStream(false), Charset.defaultCharset()); final TransferStatus status = new TransferStatus(); status.setLength((long) random.getBytes().length); status.setMime("text/plain"); status.setStorageClass(S3Object.STORAGE_CLASS_REDUCED_REDUNDANCY); service.upload(test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(), status, new DisabledLoginCallback()); assertEquals((long) random.getBytes().length, status.getOffset(), 0L); assertTrue(status.isComplete()); assertTrue(new S3FindFeature(session).find(test)); final PathAttributes attributes = new S3AttributesFinderFeature(session).find(test); assertEquals(random.getBytes().length, attributes.getSize()); assertEquals(S3Object.STORAGE_CLASS_REDUCED_REDUNDANCY, new S3StorageClassFeature(session).getClass(test)); final Map<String, String> metadata = new S3MetadataFeature(session, new S3AccessControlListFeature(session)) .getMetadata(test); assertFalse(metadata.isEmpty()); assertEquals("text/plain", metadata.get("Content-Type")); new S3DefaultDeleteFeature(session).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback()); local.delete(); session.close(); }
From source file:io.tourniquet.junit.jcr.rules.util.XMLContentLoaderTest.java
/** * This tests implements an exploit to the XML External Entity Attack {@see http://www.ws-attacks.org/index.php/XML_Entity_Reference_Attack}. * The attack targets a file in the filesystem containing a secret, i.e. a password, configurations, etc. The * attacking file defines an entity that resolves to the file containing the secret. The entity (&xxe;) is used * in the xml file and will be resolved to provide the title of the test node. If the code is not vulnerable, the * attack will fail.//from w w w. j a v a2 s. co m * * @throws Throwable */ @Test(expected = AssertionError.class) public void testLoadContent_ExternalitEntityAttack_notVulnerable() throws Throwable { //prepare //the attacked file containing the secret final File attackedFile = folder.newFile("attackedFile.txt"); try (FileOutputStream fos = new FileOutputStream(attackedFile)) { //the lead-padding of 4-chars is ignored for some mysterious reasons... IOUtils.write(" secretContent", fos, Charset.forName("UTF-8")); } //as attacker file we use a template and replacing a %s placeholder with the url of the attacked file //in a real-world attack we would use a valuable target such as /etc/passwd final File attackerFile = folder.newFile("attackerFile.xml"); //load the template file from the classpath try (InputStream is = getClass() .getResourceAsStream("XMLContentLoaderTest_tourniquetJcrImport_v1-0_xxe-attack.xml"); FileOutputStream fos = new FileOutputStream(attackerFile)) { final String attackerContent = prepareAttackerContent(is, attackedFile); IOUtils.write(attackerContent, fos); } final Session actSession = repository.getAdminSession(); //act //when the code is not vulnerable, the following call will cause a runtime exception //as the dtd processing of external entities is not allowed. subject.loadContent(actSession, attackerFile.toURI().toURL()); }
From source file:datadidit.helpful.hints.processors.csv.converter.ConvertCSVToJSON.java
@Override public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException { FlowFile flowFile = session.get();/* w w w .j ava 2 s . c o m*/ if (flowFile == null) { return; } try { //Read in Data InputStream stream = session.read(flowFile); String csv = IOUtils.toString(stream, "UTF-8"); stream.close(); //Convert CSV data to JSON List<Map<?, ?>> objects = this.readObjectsFromCsv(csv); //Convert to JSON String String json = this.writeAsJson(objects); //Output Flowfile FlowFile output = session.write(flowFile, new OutputStreamCallback() { @Override public void process(OutputStream outputStream) throws IOException { IOUtils.write(json, outputStream, "UTF-8"); } }); output = session.putAttribute(output, CoreAttributes.MIME_TYPE.key(), APPLICATION_JSON); //TODO: May want to have a better default name.... output = session.putAttribute(output, CoreAttributes.FILENAME.key(), UUID.randomUUID().toString() + ".json"); session.transfer(output, REL_SUCCESS); } catch (IOException e) { getLogger().error("Unable to process Change CSV to JSON for this file " + flowFile.getAttributes().get(CoreAttributes.FILENAME)); session.transfer(flowFile, REL_FAILURE); } }
From source file:de.tudarmstadt.ukp.clarin.webanno.tsv.WebannoCustomTsvWriter.java
private void convertToTsv(JCas aJCas, OutputStream aOs, String aEncoding) throws IOException, ResourceInitializationException, CASRuntimeException, CASException { tokenIds = new HashMap<Integer, String>(); setTokenId(aJCas, tokenIds);//from ww w.ja va 2 s .c o m tokenPositions = new TreeMap<Integer, Integer>(); setTokenPosition(aJCas, tokenPositions); Map<Integer, Integer> getTokensPerSentence = new TreeMap<Integer, Integer>(); setTokenSentenceAddress(aJCas, getTokensPerSentence); // list of annotation types Set<Type> allTypes = new LinkedHashSet<Type>(); for (Annotation a : select(aJCas, Annotation.class)) { if (!(a instanceof Token || a instanceof Sentence || a instanceof DocumentMetaData || a instanceof TagsetDescription || a instanceof CoreferenceLink)) { allTypes.add(a.getType()); } } Set<Type> relationTypes = new LinkedHashSet<Type>(); // get all arc types for (Type type : allTypes) { if (type.getFeatures().size() == 0) { continue; } for (Feature feature : type.getFeatures()) { if (feature.getShortName().equals(GOVERNOR)) { relationTypes.add(type); break; } } } allTypes.removeAll(relationTypes); // relation annotations Map<Type, String> relationTypesMap = new HashMap<Type, String>(); for (Type type : relationTypes) { if (type.getName().equals(Dependency.class.getName())) { relationTypesMap.put(type, POS.class.getName()); continue; } for (AnnotationFS anno : CasUtil.select(aJCas.getCas(), type)) { for (Feature feature : type.getFeatures()) { if (feature.getShortName().equals(GOVERNOR)) { relationTypesMap.put(type, anno.getFeatureValue(feature).getType().getName()); } } } } // all span annotation first Map<Feature, Type> spanFeatures = new LinkedHashMap<Feature, Type>(); allTypes: for (Type type : allTypes) { if (type.getFeatures().size() == 0) { continue; } for (Feature feature : type.getFeatures()) { // coreference annotation not supported if (feature.getShortName().equals(FIRST) || feature.getShortName().equals(NEXT)) { continue allTypes; } } IOUtils.write(" # " + type.getName(), aOs, aEncoding); for (Feature feature : type.getFeatures()) { if (feature.toString().equals("uima.cas.AnnotationBase:sofa") || feature.toString().equals("uima.tcas.Annotation:begin") || feature.toString().equals("uima.tcas.Annotation:end")) { continue; } spanFeatures.put(feature, type); IOUtils.write(" | " + feature.getShortName(), aOs, aEncoding); } } // write all relation annotation first Set<Feature> relationFeatures = new LinkedHashSet<Feature>(); for (Type type : relationTypes) { IOUtils.write(" # " + type.getName(), aOs, aEncoding); for (Feature feature : type.getFeatures()) { if (feature.toString().equals("uima.cas.AnnotationBase:sofa") || feature.toString().equals("uima.tcas.Annotation:begin") || feature.toString().equals("uima.tcas.Annotation:end") || feature.getShortName().equals(GOVERNOR) || feature.getShortName().equals(DEPENDENT)) { continue; } relationFeatures.add(feature); IOUtils.write(" | " + feature.getShortName(), aOs, aEncoding); } // Add the attach type for the realtion anotation IOUtils.write(" | AttachTo=" + relationTypesMap.get(type), aOs, aEncoding); } IOUtils.write("\n", aOs, aEncoding); Map<Feature, Map<Integer, String>> allAnnos = new HashMap<Feature, Map<Integer, String>>(); allTypes: for (Type type : allTypes) { for (Feature feature : type.getFeatures()) { // coreference annotation not supported if (feature.getShortName().equals(FIRST) || feature.getShortName().equals(NEXT)) { continue allTypes; } } for (Feature feature : type.getFeatures()) { if (feature.toString().equals("uima.cas.AnnotationBase:sofa") || feature.toString().equals("uima.tcas.Annotation:begin") || feature.toString().equals("uima.tcas.Annotation:end")) { continue; } Map<Integer, String> tokenAnnoMap = new TreeMap<Integer, String>(); setTokenAnnos(aJCas.getCas(), tokenAnnoMap, type, feature); allAnnos.put(feature, tokenAnnoMap); } } // get tokens where dependents are drown to Map<Feature, Map<Integer, String>> relAnnos = new HashMap<Feature, Map<Integer, String>>(); for (Type type : relationTypes) { for (Feature feature : type.getFeatures()) { if (feature.toString().equals("uima.cas.AnnotationBase:sofa") || feature.toString().equals("uima.tcas.Annotation:begin") || feature.toString().equals("uima.tcas.Annotation:end") || feature.getShortName().equals(GOVERNOR) || feature.getShortName().equals(DEPENDENT)) { continue; } Map<Integer, String> tokenAnnoMap = new HashMap<Integer, String>(); setRelationFeatureAnnos(aJCas.getCas(), tokenAnnoMap, type, feature); relAnnos.put(feature, tokenAnnoMap); } } // get tokens where dependents are drown from - the governor Map<Type, Map<Integer, String>> governorAnnos = new HashMap<Type, Map<Integer, String>>(); for (Type type : relationTypes) { Map<Integer, String> govAnnoMap = new HashMap<Integer, String>(); setRelationGovernorPos(aJCas.getCas(), govAnnoMap, type); governorAnnos.put(type, govAnnoMap); } int sentId = 1; for (Sentence sentence : select(aJCas, Sentence.class)) { IOUtils.write("#id=" + sentId++ + "\n", aOs, aEncoding); IOUtils.write("#text=" + sentence.getCoveredText().replace("\n", "") + "\n", aOs, aEncoding); for (Token token : selectCovered(Token.class, sentence)) { IOUtils.write(tokenIds.get(token.getAddress()) + "\t" + token.getCoveredText() + "\t", aOs, aEncoding); // all span annotations on this token for (Feature feature : spanFeatures.keySet()) { String annos = allAnnos.get(feature).get(token.getAddress()); if (annos == null) { if (multipleSpans.contains(spanFeatures.get(feature).getName())) { IOUtils.write("O\t", aOs, aEncoding); } else { IOUtils.write("_\t", aOs, aEncoding); } } else { IOUtils.write(annos + "\t", aOs, aEncoding); } } // for all relation features for (Type type : relationTypes) { for (Feature feature : type.getFeatures()) { if (feature.toString().equals("uima.cas.AnnotationBase:sofa") || feature.toString().equals("uima.tcas.Annotation:begin") || feature.toString().equals("uima.tcas.Annotation:end") || feature.getShortName().equals(GOVERNOR) || feature.getShortName().equals(DEPENDENT)) { continue; } String annos = relAnnos.get(feature).get(token.getAddress()); if (annos == null) { IOUtils.write("_\t", aOs, aEncoding); } else { IOUtils.write(annos + "\t", aOs, aEncoding); } } // the governor positions String govPos = governorAnnos.get(type).get(token.getAddress()); if (govPos == null) { IOUtils.write("_\t", aOs, aEncoding); } else { IOUtils.write(governorAnnos.get(type).get(token.getAddress()) + "\t", aOs, aEncoding); } } IOUtils.write("\n", aOs, aEncoding); } IOUtils.write("\n", aOs, aEncoding); } }
From source file:com.trifork.batchcopy.client.SosiUtil.java
/** * Sends a request to a given url/* w w w .j a v a2 s. co m*/ * @param url service url * @param docXml the data that should be sent * @param failOnError throw exception on error? * @return The reply from the service * @throws IOException */ private String sendRequest(String url, String action, String docXml, boolean failOnError) throws IOException { HttpURLConnection uc = null; OutputStream os = null; InputStream is = null; try { URL u = new URL(url); uc = (HttpURLConnection) u.openConnection(); uc.setDoOutput(true); uc.setDoInput(true); uc.setRequestMethod("POST"); uc.setRequestProperty("SOAPAction", "\"" + action + "\""); uc.setRequestProperty("Content-Type", "text/xml; encoding=utf-8"); os = uc.getOutputStream(); IOUtils.write(docXml, os, "UTF-8"); os.flush(); if (uc.getResponseCode() != 200) { is = uc.getErrorStream(); } else { is = uc.getInputStream(); } String res = IOUtils.toString(is, "UTF-8"); if (uc.getResponseCode() != 200 && (uc.getResponseCode() != 500 || failOnError)) { throw new RuntimeException("Got unexpected response " + uc.getResponseCode() + " from " + url); } return res; } finally { if (os != null) IOUtils.closeQuietly(os); if (is != null) IOUtils.closeQuietly(is); if (uc != null) uc.disconnect(); } }