List of usage examples for org.springframework.core.io ByteArrayResource ByteArrayResource
public ByteArrayResource(byte[] byteArray)
From source file:com.flipkart.phantom.runtime.impl.spring.web.HandlerConfigController.java
@RequestMapping(value = { "/deploy/**" }, method = RequestMethod.POST) public String deployModifiedConfig(ModelMap model, HttpServletRequest request, @ModelAttribute("handlerName") String handlerName, @RequestParam(defaultValue = "") String XMLFileContents, @RequestParam(defaultValue = "0") String identifier) { //Save the file XMLFileContents = XMLFileContents.trim(); if (identifier.equals("Save")) { try {/*from www.j av a 2s.c o m*/ this.configService.modifyHandlerConfig(handlerName, new ByteArrayResource(XMLFileContents.getBytes())); } catch (Exception e) { //Loading failed model.addAttribute("XMLFileError", "Unable to deploy file"); StringWriter errors = new StringWriter(); e.printStackTrace(new PrintWriter(errors)); model.addAttribute("LoadingError", errors.toString()); if (errors.toString() == null) { model.addAttribute("LoadingError", "Unexpected error"); } model.addAttribute("XMLFileContents", ConfigFileUtils.getContents(this.configService.getHandlerConfig(handlerName))); return "modifyConfig"; } } //Loading success model.addAttribute("SuccessMessage", "Successfully Deployed the new Handler Configuration"); model.addAttribute("taskHandlers", this.configService.getAllHandlers()); Resource handlerFile = this.configService.getHandlerConfig(handlerName); model.addAttribute("XMLFileContents", ConfigFileUtils.getContents(handlerFile)); try { model.addAttribute("XMLFileName", handlerFile.getURI()); } catch (IOException e) { model.addAttribute("XMLFileName", "File not found"); } return "viewConfig"; }
From source file:org.trpr.platform.batch.impl.spring.web.JobConfigController.java
/** * Controller for new job. Just adds an attribute jobName to the model. And redirects to the job edit page. *//* w w w . j av a 2s. c o m*/ @RequestMapping(value = "/configuration/modify_job", method = RequestMethod.POST) public String addNewJob(ModelMap model, @RequestParam MultipartFile jobFile) { String jobFileName = jobFile.getOriginalFilename(); //Check if file is empty or doesn't have an extension if (jobFile.isEmpty() || (jobFileName.lastIndexOf('.') < 0)) { model.remove("jobFile"); model.addAttribute("Error", "File is Empty or invalid. Only .xml files can be uploaded"); return "redirect:/configuration"; } else if (!jobFileName.substring(jobFileName.lastIndexOf('.')).equals(".xml")) { //Check if file is .xml model.remove("jobFile"); model.addAttribute("Error", "Only .xml files can be uploaded"); return "redirect:/configuration"; } else { //Read file to view boolean invalidJobFile = false; List<String> jobNameList = null; try { byte[] buffer = jobFile.getBytes(); String XMLFileContents = new String(buffer); model.addAttribute("XMLFileContents", XMLFileContents); jobNameList = ConfigFileUtils.getJobName(new ByteArrayResource(jobFile.getBytes())); if (jobNameList == null || jobNameList.size() == 0) { throw new PlatformException("Empty list"); } } catch (UnsupportedEncodingException e) { invalidJobFile = true; } catch (IOException e) { invalidJobFile = true; } catch (PlatformException p) { invalidJobFile = true; } for (String jobName : jobNameList) { if (jobName == null || invalidJobFile) { model.clear(); model.addAttribute("Error", "invalid jobFile. Couldn't find job's name"); return "redirect:/configuration"; } if (jobService.contains(jobName)) { model.clear(); model.addAttribute("Error", "The JobName '" + jobName + "' already exists. Please choose another name"); return "redirect:/configuration"; } } model.addAttribute("jobName", jobNameList); return "configuration/modify/jobs/job"; } }
From source file:org.cloudfoundry.identity.uaa.config.YamlServletProfileInitializerTests.java
@Test public void testLoadServletConfiguredFilename() throws Exception { Mockito.when(servletConfig.getInitParameter("APPLICATION_CONFIG_FILE")).thenReturn("/config/path/foo.yml"); Mockito.when(context.getResource(Matchers.eq("file:/config/path/foo.yml"))) .thenReturn(new ByteArrayResource("foo: bar\nspam:\n foo: baz".getBytes())); initializer.initialize(context);/*w w w . ja va 2s. c o m*/ assertEquals("bar", environment.getProperty("foo")); assertEquals("baz", environment.getProperty("spam.foo")); }
From source file:org.apache.servicecomb.demo.springmvc.server.DownloadSchema.java
@GetMapping(path = "/entityResource") @ApiResponses({ @ApiResponse(code = 200, response = File.class, message = ""), }) public ResponseEntity<Resource> entityResource(String content) throws IOException { return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE) .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=entityResource.txt") .body(new ByteArrayResource(content.getBytes(StandardCharsets.UTF_8))); }
From source file:it.jugpadova.blo.ParticipantBo.java
public void sendCertificateToParticipant(long participantId, String baseUrl) { try {//from w ww . ja va2s .com WebContext wctx = WebContextFactory.get(); ScriptSession session = wctx.getScriptSession(); Util util = new Util(session); Participant participant = participantDao.read(Long.valueOf(participantId)); Event event = participant.getEvent(); InputStream jugCertificateTemplate = jugBo .retrieveJugCertificateTemplate(event.getOwner().getJug().getId()); byte[] certificate = buildCertificate(jugCertificateTemplate, participant.getFirstName() + " " + participant.getLastName(), event.getTitle(), event.getStartDate(), event.getOwner().getJug().getName()); ByteArrayResource attachment = new ByteArrayResource(certificate); sendEmail(participant, baseUrl, "Your certificate is here", "it/jugpadova/participant-certificate.vm", attachment, "certificate" + event.getId() + ".pdf"); participant.setLastCertificateSentDate(new Date()); util.setValue("certificateMsg" + participantId, "Sent"); logger.info("Sent certificate for the participant " + participantId); } catch (Exception ex) { logger.error("Error generating a certificate", ex); throw new RuntimeException(ex); } }
From source file:net.solarnetwork.node.dao.jdbc.reactor.JdbcInstructionDao.java
/** * Default constructor.//w w w .j av a2s . co m */ public JdbcInstructionDao() { super(); setTableName(TABLE_INSTRUCTION); setTablesVersion(DEFAULT_TABLES_VERSION); setSqlGetTablesVersion(DEFAULT_SQL_GET_TABLES_VERSION); setSqlResourcePrefix("derby-instruction"); setInitSqlResource(new ByteArrayResource(getSqlResource("init").getBytes())); }
From source file:org.easit.core.controllers.twitter.TwitterTimelineController.java
@RequestMapping(value = "/twitter/tweet", method = RequestMethod.POST) public String postTweet(@Valid UploadItem uploadItem, BindingResult result) { if (result.hasErrors()) { return null; }/*from w ww .j a va2 s . c o m*/ String message = uploadItem.getCaption(); Resource photo = null; // StatusDetails details = null; if (!uploadItem.getFileData().isEmpty()) { final String fileName = uploadItem.getFileData().getOriginalFilename(); photo = new ByteArrayResource(uploadItem.getFileData().getBytes()) { public String getFilename() throws IllegalStateException { return fileName; }; }; twitter.timelineOperations().updateStatus(message, photo); return "redirect:/twitter"; } else { twitter.timelineOperations().updateStatus(message); return "redirect:/twitter"; } }
From source file:org.apache.servicecomb.it.schema.DownloadSchema.java
@GetMapping(path = "/entityResource") @ApiResponses({ @ApiResponse(code = 200, response = File.class, message = ""), }) public ResponseEntity<Resource> entityResource(String content) { return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN_VALUE) .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=entityResource.txt") .body(new ByteArrayResource(content.getBytes(StandardCharsets.UTF_8))); }
From source file:com.foilen.smalltools.email.EmailBuilder.java
/** * Include an inline attachment. Used with images. Warning: the content of the input stream is first loaded into memory since JavaMail needs to be able to replay it. * * @param contentId//w w w.ja va 2 s . c om * the content id to use. This is the "Content-ID" header in the body part. Can be used in HTML source with src="cid:theId" * @param inputStream * the input stream * @return this */ public EmailBuilder addInlineAttachmentFromStream(String contentId, InputStream inputStream) { inlineAttachments.add( new EmailAttachment(contentId, new ByteArrayResource(StreamsTools.consumeAsBytes(inputStream)))); return this; }