public static ProcessEngineConfiguration parseProcessEngineConfigurationFromInputStream(InputStream inputStream, String beanName) { Resource springResource = new InputStreamResource(inputStream); return parseProcessEngineConfiguration(springResource, beanName); }
@Override public InputStream getInputStream() throws IOException, IllegalStateException { verifyExists(); return super.getInputStream(); }
@Test(expected = IllegalArgumentException.class) @SuppressWarnings("unchecked") public void toResourceRegionExceptionLength() throws IOException { InputStreamResource resource = mock(InputStreamResource.class); given(resource.contentLength()).willThrow(IOException.class); HttpRange range = HttpRange.createByteRange(0, 9); range.toResourceRegion(resource); }
private Resource getFormResource(Object arg) { if (byte[].class == arg.getClass()) { return new ByteArrayResource((byte[]) arg); } else if (Resource.class.isAssignableFrom(arg.getClass())) { return (Resource) arg; } else if (InputStreamSource.class.isAssignableFrom(arg.getClass())) { try { return new InputStreamResource(((InputStreamResource) arg).getInputStream()); } catch (IOException e) { logger.error(e.getMessage(), e); throw new WxAppException("处理IO转换异常", e); } } else if (File.class.isAssignableFrom(arg.getClass())) { return new FileSystemResource((File) arg); } else if (InputStream.class.isAssignableFrom(arg.getClass())) { return new InputStreamResource((InputStream) arg); } else if (Reader.class.isAssignableFrom(arg.getClass())) { Reader reader = (Reader) arg; ReaderInputStream readerInputStream = new ReaderInputStream(reader, StandardCharsets.UTF_8); return new InputStreamResource(readerInputStream); } throw new WxAppException("不支持的Resource类型"); }
@Override protected Resource decodeDataBuffer(DataBuffer dataBuffer, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { byte[] bytes = new byte[dataBuffer.readableByteCount()]; dataBuffer.read(bytes); DataBufferUtils.release(dataBuffer); if (logger.isDebugEnabled()) { logger.debug(Hints.getLogPrefix(hints) + "Read " + bytes.length + " bytes"); } Class<?> clazz = elementType.toClass(); if (clazz == InputStreamResource.class) { return new InputStreamResource(new ByteArrayInputStream(bytes)); } else if (Resource.class.isAssignableFrom(clazz)) { return new ByteArrayResource(bytes); } else { throw new IllegalStateException("Unsupported resource class: " + clazz); } }
@Override public InputStream getInputStream() throws IOException, IllegalStateException { verifyExists(); return super.getInputStream(); }
/** * The PasswordPlaceholderConfigurer will read Kylin properties as the Spring resource */ public PasswordPlaceholderConfigurer() throws IOException { Resource[] resources = new Resource[1]; //Properties prop = KylinConfig.getKylinProperties(); Properties prop = getAllKylinProperties(); StringWriter writer = new StringWriter(); prop.store(new PrintWriter(writer), "kylin properties"); String propString = writer.getBuffer().toString(); IOUtils.closeQuietly(writer); InputStream is = IOUtils.toInputStream(propString, Charset.defaultCharset()); resources[0] = new InputStreamResource(is); this.setLocations(resources); }
@Override public InputStream getInputStream() throws IOException, IllegalStateException { return super.getInputStream(); //To change body of generated methods, choose Tools | Templates. }
@Override protected Resource decodeDataBuffer(DataBuffer dataBuffer, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map<String, Object> hints) { byte[] bytes = new byte[dataBuffer.readableByteCount()]; dataBuffer.read(bytes); DataBufferUtils.release(dataBuffer); if (logger.isDebugEnabled()) { logger.debug(Hints.getLogPrefix(hints) + "Read " + bytes.length + " bytes"); } Class<?> clazz = elementType.toClass(); if (clazz == InputStreamResource.class) { return new InputStreamResource(new ByteArrayInputStream(bytes)); } else if (Resource.class.isAssignableFrom(clazz)) { return new ByteArrayResource(bytes); } else { throw new IllegalStateException("Unsupported resource class: " + clazz); } }
@RequestMapping(value = "{cluster}/{appId}/compose", method = POST, consumes = {APPLICATION_OCTET_STREAM_VALUE}) public ResponseEntity<Application> uploadComposeFileAsStream(@PathVariable("cluster") String cluster, @PathVariable("appId") String appId, @RequestBody InputStreamResource resource) throws Exception { try (InputStream inputStream = resource.getInputStream()) { File file = ComposeUtils.applicationPath(composeExecutor.getBasedir(), cluster, appId, null, true); byte[] buffer = new byte[inputStream.available()]; inputStream.read(buffer); Files.write(buffer, file); return launchComposeFile(file, cluster, appId); } }
@RequestMapping(value = "/stuff/{stuffId}", method = RequestMethod.GET) public ResponseEntity<InputStreamResource> downloadStuff(@PathVariable int stuffId) throws IOException { String fullPath = stuffService.figureOutFileNameFor(stuffId); File file = new File(fullPath); HttpHeaders respHeaders = new HttpHeaders(); respHeaders.setContentType("application/pdf"); respHeaders.setContentLength(12345678); respHeaders.setContentDispositionFormData("attachment", "fileNameIwant.pdf"); InputStreamResource isr = new InputStreamResource(new FileInputStream(file)); return new ResponseEntity<InputStreamResource>(isr, respHeaders, HttpStatus.OK); }
@Test public void testInputStreamResource() throws IOException { InputStream is = new ByteArrayInputStream("testString".getBytes()); Resource resource = new InputStreamResource(is); assertTrue(resource.exists()); assertTrue(resource.isOpen()); String content = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream())); assertEquals("testString", content); assertEquals(resource, new InputStreamResource(is)); }
@Test public void testInputStreamResourceWithDescription() throws IOException { InputStream is = new ByteArrayInputStream("testString".getBytes()); Resource resource = new InputStreamResource(is, "my description"); assertTrue(resource.exists()); assertTrue(resource.isOpen()); String content = FileCopyUtils.copyToString(new InputStreamReader(resource.getInputStream())); assertEquals("testString", content); assertTrue(resource.getDescription().contains("my description")); assertEquals(resource, new InputStreamResource(is)); }
@Test(expected = BeanDefinitionStoreException.class) public void withOpenInputStream() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); Resource resource = new InputStreamResource(getClass().getResourceAsStream("test.xml")); new XmlBeanDefinitionReader(registry).loadBeanDefinitions(resource); }
@Test public void withOpenInputStreamAndExplicitValidationMode() { SimpleBeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry(); Resource resource = new InputStreamResource(getClass().getResourceAsStream("test.xml")); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry); reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_DTD); reader.loadBeanDefinitions(resource); testBeanDefinitions(registry); }
public InputStreamPart(String name, InputStream inputStream) { super(name, new InputStreamResource(inputStream)); } }
/** * @param stream Input stream containing Spring XML configuration. * @return Context. * @throws IgniteCheckedException In case of error. */ private ApplicationContext initContext(InputStream stream) throws IgniteCheckedException { GenericApplicationContext springCtx; try { springCtx = new GenericApplicationContext(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(springCtx); reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD); reader.loadBeanDefinitions(new InputStreamResource(stream)); springCtx.refresh(); } catch (BeansException e) { if (X.hasCause(e, ClassNotFoundException.class)) throw new IgniteCheckedException("Failed to instantiate Spring XML application context " + "(make sure all classes used in Spring configuration are present at CLASSPATH) ", e); else throw new IgniteCheckedException("Failed to instantiate Spring XML application context" + ", err=" + e.getMessage() + ']', e); } return springCtx; }
/** * Creates Spring application context. Optionally excluded properties can be specified, * it means that if such a property is found in {@link org.apache.ignite.configuration.IgniteConfiguration} * then it is removed before the bean is instantiated. * For example, {@code streamerConfiguration} can be excluded from the configs that Visor uses. * * @param cfgStream Stream where config file is located. * @param excludedProps Properties to be excluded. * @return Spring application context. * @throws IgniteCheckedException If configuration could not be read. */ public static ApplicationContext applicationContext(InputStream cfgStream, final String... excludedProps) throws IgniteCheckedException { try { GenericApplicationContext springCtx = prepareSpringContext(excludedProps); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(springCtx); reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD); reader.loadBeanDefinitions(new InputStreamResource(cfgStream)); springCtx.refresh(); return springCtx; } catch (BeansException e) { if (X.hasCause(e, ClassNotFoundException.class)) throw new IgniteCheckedException("Failed to instantiate Spring XML application context " + "(make sure all classes used in Spring configuration are present at CLASSPATH) ", e); else throw new IgniteCheckedException("Failed to instantiate Spring XML application context [err=" + e.getMessage() + ']', e); } }
@Test //SPR-16754 public void disableRangeSupportForStreamingResponses() throws Exception { InputStream is = new ByteArrayInputStream("Content".getBytes(StandardCharsets.UTF_8)); InputStreamResource resource = new InputStreamResource(is, "test"); ResponseEntity<Resource> returnValue = ResponseEntity.ok(resource); servletRequest.addHeader("Range", "bytes=0-5"); given(resourceMessageConverter.canWrite(any(), eq(null))).willReturn(true); given(resourceMessageConverter.canWrite(any(), eq(APPLICATION_OCTET_STREAM))).willReturn(true); processor.handleReturnValue(returnValue, returnTypeResponseEntityResource, mavContainer, webRequest); then(resourceMessageConverter).should(times(1)).write( any(InputStreamResource.class), eq(APPLICATION_OCTET_STREAM), any(HttpOutputMessage.class)); assertEquals(200, servletResponse.getStatus()); assertThat(servletResponse.getHeader(HttpHeaders.ACCEPT_RANGES), Matchers.isEmptyOrNullString()); }
public static ProcessEngineConfiguration parseProcessEngineConfigurationFromInputStream(InputStream inputStream, String beanName) { Resource springResource = new InputStreamResource(inputStream); return parseProcessEngineConfiguration(springResource, beanName); }