List of usage examples for org.springframework.core.io ClassPathResource getInputStream
@Override public InputStream getInputStream() throws IOException
From source file:InvokeGridService.java
public InputStream getResourceInputStream(String fileName) throws Exception { ClassPathResource cpr = new ClassPathResource(fileName); if (!cpr.exists()) { throw new Exception(fileName + " does not exist."); }//w w w . java2s .c o m try { InputStream inputStream = cpr.getInputStream(); return inputStream; } catch (IOException e) { throw new Exception("Error loading file " + fileName); } }
From source file:io.spring.initializr.web.AbstractInitializrIntegrationTests.java
protected JSONObject readJsonFrom(String path) { try {/*from w ww . j a v a 2 s .com*/ ClassPathResource resource = new ClassPathResource(path); try (InputStream stream = resource.getInputStream()) { String json = StreamUtils.copyToString(stream, Charset.forName("UTF-8")); String placeholder = ""; if (this instanceof AbstractInitializrControllerIntegrationTests) { placeholder = ((AbstractInitializrControllerIntegrationTests) this).host; } if (this instanceof AbstractFullStackInitializrIntegrationTests) { AbstractFullStackInitializrIntegrationTests test = (AbstractFullStackInitializrIntegrationTests) this; placeholder = test.host + ":" + test.port; } // Let's parse the port as it is random // TODO: put the port back somehow so it appears in stubs String content = json.replaceAll("@host@", placeholder); return new JSONObject(content); } } catch (Exception e) { throw new IllegalStateException("Cannot read JSON from path=" + path); } }
From source file:com.ephesoft.dcma.core.service.ServerHeartBeatMonitor.java
/** * Gets stream to read workflow properties for the server. * //from w w w. ja v a 2s . c o m * @return {@link BufferedInputStream} * @throws IOException */ private BufferedInputStream getHeartBeatPropertiesReaderStream() throws IOException { BufferedInputStream propertyInputStream = null; String filePath = ICommonConstants.DCMA_HEART_BEAT_PROPERTIES; ClassPathResource classPathResourse = new ClassPathResource(filePath); if (classPathResourse.exists()) { propertyInputStream = new BufferedInputStream(classPathResourse.getInputStream()); } return propertyInputStream; }
From source file:org.openmrs.module.dhisreport.api.DHIS2ReportingServiceDXFTest.java
@Ignore @Test//from w ww.j av a 2 s.c om public void postDhisReportTest() throws Exception { HttpDhis2Server server = new HttpDhis2Server(); server.setUsername("admin"); server.setPassword("district"); server.setUrl(new URL("http://apps.dhis2.org/dev")); service.setDhis2Server(server); ClassPathResource resource = new ClassPathResource("dvset.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(DataValueSet.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); DataValueSet dvset = (DataValueSet) jaxbUnmarshaller.unmarshal(resource.getInputStream()); ImportSummary summary = service.postDataValueSet(dvset); JAXBContext importSummaryContext = JAXBContext.newInstance(ImportSummary.class); Marshaller jaxbmarshaller = importSummaryContext.createMarshaller(); jaxbmarshaller.marshal(summary, System.out); }
From source file:de.ingrid.admin.service.ElasticsearchNodeFactoryBean.java
private void internalCreateNode() { final NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder(); // set inital configurations coming from the property file Properties p = new Properties(); try {// w ww . ja v a 2s .co m ClassPathResource resource = new ClassPathResource("/elasticsearch.properties"); if (resource.exists()) { p.load(resource.getInputStream()); nodeBuilder.getSettings().put(p); } } catch (IOException e) { e.printStackTrace(); } // other possibilities for configuration // TODO: remove those not needed! if (null != configLocation) { internalLoadSettings(nodeBuilder, configLocation); } if (null != configLocations) { for (final Resource location : configLocations) { internalLoadSettings(nodeBuilder, location); } } if (null != settings) { nodeBuilder.getSettings().put(settings); } if (null != properties) { nodeBuilder.getSettings().put(properties); } Node localNode = nodeBuilder.node(); localNode.client().admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet(); node = localNode; }
From source file:com.iflytek.edu.cloud.frame.web.listener.LogBackLoadConfigureListener.java
@Override public void contextInitialized(ServletContextEvent sce) { LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); try {/* w w w. ja v a 2 s. co m*/ JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(context); context.reset(); ClassPathResource resource = new ClassPathResource("logback-default.xml"); if (!resource.exists()) { String profile = EnvUtil.getProfile(); resource = new ClassPathResource("META-INF/logback/logback-" + profile + ".xml"); } configurator.doConfigure(resource.getInputStream()); logger.info("logback?" + resource.getURL().getPath()); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } StatusPrinter.printInCaseOfErrorsOrWarnings(context); }
From source file:org.openmrs.module.dhisreport.api.model.ReportTemplatesTest.java
@Test public void marshallReportTemplates() throws Exception { ClassPathResource resource = new ClassPathResource("templates_ethiopia.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(ReportTemplates.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); ReportTemplates reportTemplates = (ReportTemplates) jaxbUnmarshaller.unmarshal(resource.getInputStream()); Collection<DataValueTemplate> dvts = reportTemplates.getReportDefinitions().get(1).getDataValueTemplates(); for (DataValueTemplate dvt : dvts) { dvt.setQuery("select count(*) from something & something_else"); }//from w w w . j a va 2 s . c o m Marshaller jaxbmarshaller = jaxbContext.createMarshaller(); jaxbmarshaller.marshal(reportTemplates, System.out); }
From source file:com.oembedler.moon.graphql.test.GenericTodoSchemaParserTest.java
public String readClasspathResourceToString(final String resourceName) { String resourceAsString = null; ClassPathResource classPathResource = new ClassPathResource(resourceName); try (InputStream is = classPathResource.getInputStream()) { resourceAsString = StreamUtils.copyToString(is, Charset.forName("UTF-8")); } catch (IOException e) { e.printStackTrace();// ww w .j av a2 s.c om } return resourceAsString; }
From source file:org.openmrs.module.dhisreport.api.model.ReportTemplatesTest.java
@Test public void unMarshallReportTemplates() throws Exception { ClassPathResource resource = new ClassPathResource("templates_ethiopia.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(ReportTemplates.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); ReportTemplates reportTemplates = (ReportTemplates) jaxbUnmarshaller.unmarshal(resource.getInputStream()); assertNotNull(reportTemplates);/*w ww . j ava2s. c o m*/ List<ReportDefinition> reportDefinitions = reportTemplates.getReportDefinitions(); assertEquals(2, reportDefinitions.size()); for (ReportDefinition rd : reportDefinitions) { for (DataValueTemplate dvt : rd.getDataValueTemplates()) { assertNotNull(dvt.getDataelement()); assertNotNull(dvt.getDataelement().getCode()); assertNotNull(dvt.getDataelement().getName()); assertNotNull(dvt.getDataelement().getUid()); assertNotNull(dvt.getDisaggregation()); assertNotNull(dvt.getDisaggregation().getCode()); assertNotNull(dvt.getDisaggregation().getName()); assertNotNull(dvt.getDisaggregation().getUid()); } } }