Example usage for org.springframework.http MediaType TEXT_PLAIN

List of usage examples for org.springframework.http MediaType TEXT_PLAIN

Introduction

In this page you can find the example usage for org.springframework.http MediaType TEXT_PLAIN.

Prototype

MediaType TEXT_PLAIN

To view the source code for org.springframework.http MediaType TEXT_PLAIN.

Click Source Link

Document

Public constant media type for text/plain .

Usage

From source file:it.geosolutions.opensdi.operations.FolderManagerOperationController.java

/**
 * Download a file with a stream/*from   w w w .ja  v  a2 s  .c  o m*/
 * 
 * @param resp
 * @param fileName
 * @param filePath
 * @return
 */
@SuppressWarnings("resource")
private ResponseEntity<byte[]> download(HttpServletResponse resp, String fileName, String filePath) {

    final HttpHeaders headers = new HttpHeaders();
    File toServeUp = new File(filePath);
    InputStream inputStream = null;

    try {
        inputStream = new FileInputStream(toServeUp);
    } catch (FileNotFoundException e) {

        // Also useful, this is a good was to serve down an error message
        String msg = "ERROR: Could not find the file specified.";
        headers.setContentType(MediaType.TEXT_PLAIN);
        return new ResponseEntity<byte[]>(msg.getBytes(), headers, HttpStatus.NOT_FOUND);

    }

    resp.setContentType("application/octet-stream");
    resp.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

    Long fileSize = toServeUp.length();
    resp.setContentLength(fileSize.intValue());

    OutputStream outputStream = null;

    try {
        outputStream = resp.getOutputStream();
    } catch (IOException e) {
        String msg = "ERROR: Could not generate output stream.";
        headers.setContentType(MediaType.TEXT_PLAIN);
        return new ResponseEntity<byte[]>(msg.getBytes(), headers, HttpStatus.NOT_FOUND);
    }

    byte[] buffer = new byte[1024];

    int read = 0;
    try {

        while ((read = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, read);
        }

        // close the streams to prevent memory leaks
        outputStream.flush();
        outputStream.close();
        inputStream.close();

    } catch (Exception e) {
        String msg = "ERROR: Could not read file.";
        headers.setContentType(MediaType.TEXT_PLAIN);
        return new ResponseEntity<byte[]>(msg.getBytes(), headers, HttpStatus.NOT_FOUND);
    }

    return null;
}

From source file:org.bonitasoft.web.designer.controller.ImportControllerTest.java

@Test
public void should_import_a_page_with_its_dependencies() throws Exception {
    //We construct a mockfile (the first arg is the name of the property expected in the controller
    MockMultipartFile file = new MockMultipartFile("file", "myfile.zip", "application/zip", "foo".getBytes());
    ImportReport expectedReport = anImportReportFor(aPage().withId("aPage").withName("thePage"))
            .withUUID("UUIDZipFile").withStatus(ImportReport.Status.CONFLICT)
            .withAdded(aWidget().id("addedWidget").name("newWidget"))
            .withOverridden(aWidget().id("overriddenWidget").name("oldWidget")).build();
    when(pathImporter.importFromPath(unzipedPath, pageImporter)).thenReturn(expectedReport);

    mockMvc.perform(fileUpload("/import/page").file(file))
            .andExpect(content().contentType(MediaType.TEXT_PLAIN)).andExpect(status().isCreated())
            .andExpect(jsonPath("uuid").value("UUIDZipFile"))
            .andExpect(jsonPath("extractedDirName").doesNotExist())
            .andExpect(jsonPath("element.id").value("aPage")).andExpect(jsonPath("status").value("conflict"))
            .andExpect(jsonPath("element.name").value("thePage"))
            .andExpect(jsonPath("dependencies.added.widget[0].id").value("addedWidget"))
            .andExpect(jsonPath("dependencies.added.widget[0].name").value("newWidget"))
            .andExpect(jsonPath("dependencies.overridden.widget[0].id").value("overriddenWidget"))
            .andExpect(jsonPath("dependencies.overridden.widget[0].name").value("oldWidget"));
}

From source file:khs.trouble.service.impl.TroubleService.java

public String exception(String serviceName, String instanceId, String ltoken) {
    if (token != ltoken) {
        throw new RuntimeException("Invalid Access Token");
    }/*from   ww  w.j av  a  2  s  .  co m*/

    String url = FormatUrl.url(serviceInstanceURL(serviceName, instanceId) + "/trouble/exception", ssl);

    // invoke kill api...
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_PLAIN));
    headers.add("token", token);
    HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);

    try {
        restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        eventService.exception(serviceName, url);
    } catch (Exception e) {
        eventService.attempted("Attempted to throw exception at service " + serviceName + " at " + url
                + " Failed due to exception " + e.getMessage());
    }
    return serviceName;
}

From source file:khs.trouble.service.impl.TroubleService.java

public String memory(String serviceName, String instanceId, String ltoken) {
    if (token != ltoken) {
        throw new RuntimeException("Invalid Access Token");
    }/*  w  w  w.  jav  a  2 s. co m*/

    String url = FormatUrl.url(serviceInstanceURL(serviceName, instanceId) + "/trouble/memory", ssl);

    // invoke memory api...
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.TEXT_PLAIN));
    headers.add("token", token);
    headers.add("timeout", "" + timeout);
    HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);

    try {
        restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        eventService.memory(serviceName, url);
    } catch (Exception e) {
        eventService.attempted("Attempted to consume memory at service " + serviceName + " at " + url
                + " Failed due to exception " + e.getMessage());
    }
    return serviceName;
}

From source file:org.bonitasoft.web.designer.controller.ImportControllerTest.java

@Test
public void should_force_a_page_import() throws Exception {
    //We construct a mockfile (the first arg is the name of the property expected in the controller
    MockMultipartFile file = new MockMultipartFile("file", "myfile.zip", "application/zip", "foo".getBytes());
    ImportReport expectedReport = anImportReportFor(aPage().withId("aPage").withName("thePage"))
            .withUUID("UUIDZipFile").withStatus(ImportReport.Status.IMPORTED)
            .withAdded(aWidget().id("addedWidget").name("newWidget"))
            .withOverridden(aWidget().id("overriddenWidget").name("oldWidget")).build();
    when(pathImporter.forceImportFromPath(unzipedPath, pageImporter)).thenReturn(expectedReport);

    mockMvc.perform(fileUpload("/import/page?force=true").file(file))
            .andExpect(content().contentType(MediaType.TEXT_PLAIN)).andExpect(status().isCreated())
            .andExpect(jsonPath("uuid").value("UUIDZipFile"))
            .andExpect(jsonPath("extractedDirName").doesNotExist())
            .andExpect(jsonPath("element.id").value("aPage")).andExpect(jsonPath("status").value("imported"))
            .andExpect(jsonPath("element.name").value("thePage"))
            .andExpect(jsonPath("dependencies.added.widget[0].id").value("addedWidget"))
            .andExpect(jsonPath("dependencies.added.widget[0].name").value("newWidget"))
            .andExpect(jsonPath("dependencies.overridden.widget[0].id").value("overriddenWidget"))
            .andExpect(jsonPath("dependencies.overridden.widget[0].name").value("oldWidget"));
}

From source file:khs.trouble.service.impl.TroubleService.java

public void spawnLoadThread(final String serviceName, String instanceId, final long sleep) {

    Runnable run = new Runnable() {

        public void run() {
            try {

                String url = FormatUrl.url(serviceInstanceURL(serviceName, instanceId) + "/trouble/load", ssl);

                // invoke kill api...
                RestTemplate restTemplate = new RestTemplate();
                HttpHeaders headers = new HttpHeaders();
                headers.setAccept(Arrays.asList(MediaType.TEXT_PLAIN));
                headers.add("token", token);
                headers.add("timeout", "" + timeout);
                HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);

                try {
                    restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
                } catch (Exception e) {
                    eventService.attempted("Attempted to Load service " + serviceName + " at " + url
                            + " Failed due to exception " + e.getMessage());
                }//from  w  ww. ja v  a 2  s .  co  m
                Thread.sleep(sleep);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    };

    Thread thread = new Thread(run);
    thread.start();
}

From source file:org.bonitasoft.web.designer.controller.ImportControllerTest.java

@Test
public void should_respond_an_error_with_ok_code_when_import_exception_occurs_while_importing_a_page()
        throws Exception {
    MockMultipartFile file = new MockMultipartFile("file", "myfile.zip", "application/zip", "foo".getBytes());
    when(pathImporter.importFromPath(unzipedPath, pageImporter))
            .thenThrow(new ImportException(Type.SERVER_ERROR, "an error messge"));

    mockMvc.perform(fileUpload("/import/page").file(file))
            .andExpect(content().contentType(MediaType.TEXT_PLAIN)).andExpect(status().isAccepted())
            .andExpect(jsonPath("type").value("SERVER_ERROR"))
            .andExpect(jsonPath("message").value("an error messge"));
}

From source file:org.apigw.authserver.web.controller.CertifiedClientsController.java

@RequestMapping(value = "/oauth/clients/{clientId}/icon", method = RequestMethod.GET)
public ResponseEntity<?> getClientIcon(@PathVariable String clientId) {
    log.debug("getClientIcon");
    log.debug("Trying to load client icon");
    CertifiedClientIcon icon = clientDetailsService.findClientIconByClientId(clientId);
    HttpHeaders headers = new HttpHeaders();
    if (icon == null) {
        headers.setContentType(MediaType.TEXT_PLAIN);
        log.debug("getClientIcon: no icon found");
        return new ResponseEntity<String>("No icon found for client with clientId: " + clientId, headers,
                HttpStatus.NOT_FOUND);/* w w w  .j  a  va  2 s  . c  o  m*/
    } else {
        headers.setContentType(MediaType.valueOf(icon.getContentType()));
        log.debug("getClientIcon: returning with icon");
        return new ResponseEntity<byte[]>(icon.getIcon(), headers, HttpStatus.OK);
    }
}

From source file:org.bonitasoft.web.designer.controller.ImportControllerTest.java

@Test
public void should_import_a_widget() throws Exception {
    //We construct a mockfile (the first arg is the name of the property expected in the controller
    MockMultipartFile file = new MockMultipartFile("file", "myfile.zip", "application/zip", "foo".getBytes());
    ImportReport expectedReport = anImportReportFor(aWidget().id("aWidget").name("myWidgetName")).build();
    when(pathImporter.importFromPath(unzipedPath, widgetImporter)).thenReturn(expectedReport);

    mockMvc.perform(fileUpload("/import/widget").file(file))
            .andExpect(content().contentType(MediaType.TEXT_PLAIN)).andExpect(status().isCreated())
            .andExpect(jsonPath("element.id").value("aWidget"))
            .andExpect(jsonPath("element.name").value("myWidgetName"));
}