List of usage examples for org.springframework.util StreamUtils copy
public static void copy(String in, Charset charset, OutputStream out) throws IOException
From source file:fi.helsinki.opintoni.config.http.converter.CsvHttpMessageConverter.java
@Override protected void writeInternal(CsvResponse<T> response, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { outputMessage.getHeaders().set("Content-Disposition", "attachment; filename=" + response.filename); String csv = getCsv(response); StreamUtils.copy(csv, Charset.forName("UTF-8"), outputMessage.getBody()); }
From source file:com.eu.evaluation.server.mvc.UTF8StringHttpMessageConverter.java
@Override protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException { if (this.writeAcceptCharset) { outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets()); }// w w w . j a v a 2 s .co m Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType()); StreamUtils.copy(s, charset, outputMessage.getBody()); }
From source file:com.lingcaibao.web.interceptor.StringHttpMessageConverter.java
@Override protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException { if (this.writeAcceptCharset) { outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets()); }/*from www . j a v a 2 s . co m*/ Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType()); System.out.println(s); StreamUtils.copy(s, charset, outputMessage.getBody()); }
From source file:com.jy51.app.base.converter.StringHttpMessageConverter.java
@Override protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException { if (this.writeAcceptCharset) { outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets()); }//from w w w.ja v a2 s. c o m // Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType()); MediaType mediaType = new MediaType(MediaType.TEXT_HTML.getType(), MediaType.TEXT_HTML.getSubtype(), charset); outputMessage.getHeaders().setContentType(mediaType); StreamUtils.copy(s, charset, outputMessage.getBody()); }
From source file:com.iflytek.edu.cloud.frame.spring.RequestResponseBodyMethodProcessorExt.java
private void write(final String content, MediaType contentType, ServletServerHttpResponse outputMessage) throws IOException, HttpMessageNotWritableException { final HttpHeaders headers = outputMessage.getHeaders(); headers.setContentType(contentType); if (headers.getContentLength() == -1) { Long contentLength = getContentLength(content, headers.getContentType()); if (contentLength != null) { headers.setContentLength(contentLength); }//from w w w .j av a 2 s. c om } StreamUtils.copy(content, charset, outputMessage.getBody()); outputMessage.getBody().flush(); }
From source file:alfio.controller.api.admin.ResourceController.java
@RequestMapping(value = "/overridable-template/{name}/{locale}/preview", method = RequestMethod.POST) public void previewTemplate(@PathVariable("name") TemplateResource name, @PathVariable("locale") String locale, @RequestParam(required = false, value = "organizationId") Integer organizationId, @RequestParam(required = false, value = "eventId") Integer eventId, @RequestBody UploadBase64FileModification template, Principal principal, HttpServletResponse response) throws IOException { Locale loc = Locale.forLanguageTag(locale); if (organizationId != null && eventId != null) { checkAccess(organizationId, eventId, principal); Event event = eventRepository.findById(eventId); Organization organization = organizationRepository.getById(organizationId); Optional<TemplateResource.ImageData> image = TemplateProcessor.extractImageModel(event, fileUploadManager);/*from w w w . ja va 2s . co m*/ Map<String, Object> model = name.prepareSampleModel(organization, event, image); String renderedTemplate = templateManager.renderString(template.getFileAsString(), model, loc, name.getTemplateOutput()); if ("text/plain".equals(name.getRenderedContentType())) { response.addHeader("Content-Disposition", "attachment; filename=" + name.name() + ".txt"); response.setContentType("text/plain"); response.setCharacterEncoding("UTF-8"); try (OutputStream os = response.getOutputStream()) { StreamUtils.copy(renderedTemplate, StandardCharsets.UTF_8, os); } } else if ("application/pdf".equals(name.getRenderedContentType())) { response.setContentType("application/pdf"); response.addHeader("Content-Disposition", "attachment; filename=" + name.name() + ".pdf"); try (OutputStream os = response.getOutputStream()) { TemplateProcessor.prepareItextRenderer(renderedTemplate).createPDF(os); } } else { throw new IllegalStateException("cannot enter here!"); } } }
From source file:com.ctc.storefront.controllers.pages.CartPageController.java
@RequestMapping(value = "/export", method = RequestMethod.GET, produces = "text/csv") public String exportCsvFile(final HttpServletResponse response, final RedirectAttributes redirectModel) throws IOException { response.setHeader("Content-Disposition", "attachment;filename=cart.csv"); try (final StringWriter writer = new StringWriter()) { try {/*ww w . j a va2 s .c o m*/ final List<String> headers = new ArrayList<String>(); headers.add(getMessageSource().getMessage("basket.export.cart.item.sku", null, getI18nService().getCurrentLocale())); headers.add(getMessageSource().getMessage("basket.export.cart.item.quantity", null, getI18nService().getCurrentLocale())); headers.add(getMessageSource().getMessage("basket.export.cart.item.name", null, getI18nService().getCurrentLocale())); headers.add(getMessageSource().getMessage("basket.export.cart.item.price", null, getI18nService().getCurrentLocale())); final CartData cartData = getCartFacade().getSessionCartWithEntryOrdering(true); csvFacade.generateCsvFromCart(headers, true, cartData, writer); StreamUtils.copy(writer.toString(), StandardCharsets.UTF_8, response.getOutputStream()); } catch (final IOException e) { LOG.error(e.getMessage(), e); GlobalMessages.addFlashMessage(redirectModel, GlobalMessages.ERROR_MESSAGES_HOLDER, "basket.export.cart.error", null); return REDIRECT_CART_URL; } } return null; }
From source file:com.daimler.spm.storefront.controllers.pages.CartPageController.java
@RequestMapping(value = "/export", method = RequestMethod.GET, produces = "text/csv") public String exportCsvFile(final HttpServletResponse response, final RedirectAttributes redirectModel) throws IOException { response.setHeader("Content-Disposition", "attachment;filename=cart.csv"); try (final StringWriter writer = new StringWriter()) { try {/*from w w w . ja v a 2 s . c o m*/ final List<String> headers = new ArrayList<String>(); headers.add(getMessageSource().getMessage("basket.export.cart.item.sku", null, getI18nService().getCurrentLocale())); headers.add(getMessageSource().getMessage("basket.export.cart.item.quantity", null, getI18nService().getCurrentLocale())); headers.add(getMessageSource().getMessage("basket.export.cart.item.name", null, getI18nService().getCurrentLocale())); headers.add(getMessageSource().getMessage("basket.export.cart.item.price", null, getI18nService().getCurrentLocale())); final CartData cartData = getCartFacade().getSessionCartWithEntryOrdering(false); csvFacade.generateCsvFromCart(headers, true, cartData, writer); StreamUtils.copy(writer.toString(), StandardCharsets.UTF_8, response.getOutputStream()); } catch (final IOException e) { LOG.error(e.getMessage(), e); GlobalMessages.addFlashMessage(redirectModel, GlobalMessages.ERROR_MESSAGES_HOLDER, "basket.export.cart.error", null); return REDIRECT_CART_URL; } } return null; }
From source file:io.spring.initializr.generator.ProjectGenerator.java
private void writeText(File target, String body) { try (OutputStream stream = new FileOutputStream(target)) { StreamUtils.copy(body, Charset.forName("UTF-8"), stream); } catch (Exception e) { throw new IllegalStateException("Cannot write file " + target, e); }//from w w w.ja v a2 s . co m }
From source file:org.geoserver.opensearch.rest.CollectionsController.java
@GetMapping(path = "{collection}/metadata", produces = { MediaType.TEXT_XML_VALUE }) public void getCollectionMetadata(@PathVariable(name = "collection", required = true) String collection, HttpServletResponse response) throws IOException { // query one collection and grab its OGC links Feature feature = queryCollection(collection, q -> { q.setProperties(Collections.singletonList(FF.property(OpenSearchAccess.METADATA_PROPERTY_NAME))); });/*from w w w. ja v a2s . c o m*/ // grab the metadata Property metadataProperty = feature.getProperty(OpenSearchAccess.METADATA_PROPERTY_NAME); if (metadataProperty != null && metadataProperty.getValue() instanceof String) { String value = (String) metadataProperty.getValue(); response.setContentType("text/xml"); StreamUtils.copy(value, Charset.forName("UTF-8"), response.getOutputStream()); } else { throw new ResourceNotFoundException("Metadata for collection '" + collection + "' could not be found"); } }