static String readFile(final File file) { try { final BufferedInputStream bin = new BufferedInputStream(new FileInputStream(file)); try { return IOUtils.toString(bin); } finally { bin.close(); } } catch (final IOException e) { throw new RuntimeException("Could not read file " + file, e); } }
protected void outputAs(String id, String ext) { String url = url(id, ext); File file = new File(StringUtils.substringAfter(url, "file:")); String content; try { content = IOUtils.toString(new FileInputStream(file)); } catch (IOException e) { content = e.getMessage(); } MultiLineLabel output = (MultiLineLabel) get("output"); output.setDefaultModelObject(content); }
public void load() { mimeTypesMap = new HashMap<>(); try { JsonNode jsonNode = MAPPER.readTree(IOUtils.toString(getClass().getResourceAsStream("/MIMETypes.json"))); for (JsonNode node : jsonNode) { JsonNode type = node.path("name"); JsonNode ext = node.path("extension"); if (!type.isMissingNode()) { mimeTypesMap.put(type.asText(), ext.isMissingNode() ? "" : ext.asText()); } } mimeTypesMap = Collections.unmodifiableMap(mimeTypesMap); LOG.debug("MIME types loaded: {}", mimeTypesMap); mimeTypes = new ArrayList<>(mimeTypesMap.keySet()); Collections.sort(mimeTypes); mimeTypes = Collections.unmodifiableList(mimeTypes); } catch (Exception e) { LOG.error("Error reading file MIMETypes from resources", e); } }
protected void outputAs(String id, String ext) { String url = url(id, ext); File file = new File(StringUtils.substringAfter(url, "file:")); String content; try { content = IOUtils.toString(new FileInputStream(file)); } catch (IOException e) { content = e.getMessage(); } MultiLineLabel output = (MultiLineLabel) get("output"); output.setDefaultModelObject(content); }
public static String svgAsString(PackageResourceReference reference, String... cssClasses) throws ResourceStreamNotFoundException, IOException { final PackageResource resource = reference.getResource(); final IResourceStream resourceStream = resource.getResourceStream(); if (resourceStream == null) { throw new ResourceStreamNotFoundException("Cannot find SVG icon " + resource); } String svgAsString = IOUtils.toString(resourceStream.getInputStream()); int rootIndex = svgAsString.indexOf("<svg"); if (rootIndex == -1) { throw new IllegalArgumentException("Cannot find SVG root element in " + resource); } //skip everything (comments, xml declaration and dtd definition) before <svg element svgAsString = svgAsString.substring(rootIndex); //append css classes if present final String cssClassesAsString = cssClassesAsString(cssClasses); if (StringUtils.isNotEmpty(cssClassesAsString)) { //check if class attribute is present and part of <svg element final int classAttributeIndex = svgAsString.indexOf("class=\""); if (classAttributeIndex > -1 && classAttributeIndex < svgAsString.indexOf(">")) { int insertCssClassesAt = classAttributeIndex + 7; svgAsString = svgAsString.substring(0, insertCssClassesAt) + cssClassesAsString + " " + svgAsString.substring(insertCssClassesAt); } else { svgAsString = "<svg class=\"" + cssClassesAsString + "\"" + svgAsString.substring(4); } } return svgAsString; }
/** * @param address * @return * @throws IOException */ public GLatLng geocode(final String address) throws IOException { InputStream is = invokeService(encode(address)); if (is != null) { try { String content = org.apache.wicket.util.io.IOUtils.toString(is); return decode(content); } finally { is.close(); } } return null; }
outputStream.close(); String response = IOUtils.toString(urlConnection.getInputStream(), "UTF-8");
/** * Invoke a geocoder request to the GoogleMaps API.<br/> * Only return the first element of {@link #geocoderResult} to be backward compatible.<br/> * After successful call of {@link #geocode(String)} you can get all results from * {@link #geocoderResult} * * @param address * - Requested address * @return {@link GLatLng} - only first hit of the request * @throws IOException */ public GLatLng geocode(final String address) throws IOException { InputStream is = invokeService(encode(address)); if (is != null) { try { String content = org.apache.wicket.util.io.IOUtils.toString(is); return decode(content); } finally { is.close(); } } return null; }
@BeforeClass public static void before() throws IOException { content = IOUtils.toString(YuiCssCompressorTest.class.getResourceAsStream("test.css")); compressor = new YuiCssCompressor(); }
String content = IOUtils.toString(httpRequest.getInputStream()); ODocument log = OLoggerModule.storeOLoggerEvent(content); out = log.toJSON();
IOUtils.toString(request.getInputStream()), Credentials.class); final String username = credentials.getUsername(); final String password = credentials.getPassword().isEmpty() ? null : credentials.getPassword();
@Override protected void onBefore() { try { cssContent = IOUtils.toString(BootstrapCssReference.INSTANCE.getResource().getResourceStream().getInputStream()); } catch (IOException e) { throw new RuntimeException(e); } catch (ResourceStreamNotFoundException e) { throw new RuntimeException(e); } }
/** * compiles a given less file * * @param fileName The file name (relative to this class) * @return compiled content */ private String compileLessFile(final String fileName) { final String content; try { content = IOUtils.toString(Less4JCompilerTest.class.getResourceAsStream("responsive.less")); } catch (IOException e) { throw new WicketRuntimeException(e); } return compile(content); }
@Override public String asText() { InputStream stream = null; try { stream = getInputStream(); return IOUtils.toString(stream, Bootstrap.getSettings(Application.get()).getBootstrapLessCompilerSettings().getCharset().name()); } catch (IOException e) { throw new WicketRuntimeException(e); } finally { IOUtils.closeQuietly(stream); } }
@Override public Component preview(final byte[] uploadedBytes) { Fragment fragment = new Fragment("preview", "noPreviewFragment", this); if (uploadedBytes.length > 0) { try { fragment = new Fragment("preview", "previewFragment", this); InputStream stream = new ByteArrayInputStream(uploadedBytes); TextArea<String> jsonEditor = new TextArea<>("jsonEditorInfo", new Model<>(IOUtils.toString(stream))); jsonEditor.setOutputMarkupPlaceholderTag(true); jsonEditorInfoId = jsonEditor.getMarkupId(); fragment.add(jsonEditor); } catch (IOException e) { LOG.error("Error evaluating text file", e); } } WebMarkupContainer previewContainer = new WebMarkupContainer("previewContainer"); previewContainer.setOutputMarkupId(true); previewContainer.add(fragment); return this.addOrReplace(previewContainer); }
@Test public void findOnGAE() throws ResourceStreamNotFoundException, IOException { System.setProperty("com.google.appengine.runtime.environment", "Production"); WebjarsResourceFinder finder = new WebjarsResourceFinder(WicketWebjars.settings()); IResourceStream stream = finder.find(IWebjarsResourceReference.class, "/webjars/jquery/2.2.1/jquery.min.js"); System.setProperty("com.google.appengine.runtime.environment", ""); assertThat(stream, is(not(nullValue()))); assertThat(IOUtils.toString(stream.getInputStream()), startsWith("/*! jQuery v2.2.1")); }
@Test public void findFile() throws ResourceStreamNotFoundException, IOException { WebjarsResourceFinder finder = new WebjarsResourceFinder(WicketWebjars.settings()); IResourceStream stream = finder.find(IWebjarsResourceReference.class, "/webjars/jquery/2.2.1/jquery.min.js"); assertThat(stream, is(not(nullValue()))); assertThat(IOUtils.toString(stream.getInputStream()), startsWith("/*! jQuery v2.2.1")); }
/** * compiles given content with {@link Less4JCompiler} * * @param content to compile * @return compiled content as string */ private String compile(final String content) { try { return removeLineBreaks(IOUtils.toString(compiler.compile(on(content)).getInputStream(), Charsets.UTF_8.toString())); } catch (Exception e) { if (e instanceof Less4jException) { return new Less4JCompiler.ErrorLogger("test", (Less4jException) e).toString(); } throw new RuntimeException(e); } }
@Override protected void onBefore() { try { cssContent = IOUtils.toString(BootswatchCssReference.CERULEAN.getResource().getResourceStream().getInputStream()); } catch (IOException e) { throw new RuntimeException(e); } catch (ResourceStreamNotFoundException e) { throw new RuntimeException(e); } getBootstrapSettings().getBootstrapLessCompilerSettings() .setCacheStrategy(IBootstrapLessCompilerSettings.CacheStrategy.Never) .setUseLessCompiler(true); }
@Test public void findFileWithoutVersion() throws ResourceStreamNotFoundException, IOException { WebjarsResourceFinder finder = new WebjarsResourceFinder(WicketWebjars.settings()); IResourceStream stream = finder.find(IWebjarsResourceReference.class, useRecent("/webjars/jquery/current/jquery.min.js")); assertThat(stream, is(not(nullValue()))); assertThat(IOUtils.toString(stream.getInputStream()), startsWith("/*! jQuery v2.2.1")); }