Search in sources :

Example 1 with ContentDisposition

use of org.springframework.http.ContentDisposition in project spring-framework by spring-projects.

the class StandardMultipartHttpServletRequest method parseRequest.

private void parseRequest(HttpServletRequest request) {
    try {
        Collection<Part> parts = request.getParts();
        this.multipartParameterNames = new LinkedHashSet<>(parts.size());
        MultiValueMap<String, MultipartFile> files = new LinkedMultiValueMap<>(parts.size());
        for (Part part : parts) {
            String headerValue = part.getHeader(HttpHeaders.CONTENT_DISPOSITION);
            ContentDisposition disposition = ContentDisposition.parse(headerValue);
            String filename = disposition.getFilename();
            if (filename != null) {
                if (filename.startsWith("=?") && filename.endsWith("?=")) {
                    filename = MimeDelegate.decode(filename);
                }
                files.add(part.getName(), new StandardMultipartFile(part, filename));
            } else {
                this.multipartParameterNames.add(part.getName());
            }
        }
        setMultipartFiles(files);
    } catch (Throwable ex) {
        handleParseFailure(ex);
    }
}
Also used : MultipartFile(org.springframework.web.multipart.MultipartFile) ContentDisposition(org.springframework.http.ContentDisposition) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Part(jakarta.servlet.http.Part)

Example 2 with ContentDisposition

use of org.springframework.http.ContentDisposition in project spring-framework by spring-projects.

the class ResourceHttpMessageReaderTests method readResourceAsMono.

@Test
void readResourceAsMono() throws IOException {
    String filename = "test.txt";
    String body = "Test resource content";
    ContentDisposition contentDisposition = ContentDisposition.attachment().name("file").filename(filename).build();
    MockClientHttpResponse response = new MockClientHttpResponse(HttpStatus.OK);
    response.getHeaders().setContentType(MediaType.TEXT_PLAIN);
    response.getHeaders().setContentDisposition(contentDisposition);
    response.setBody(Mono.just(stringBuffer(body)));
    Resource resource = reader.readMono(ResolvableType.forClass(ByteArrayResource.class), response, Collections.emptyMap()).block();
    assertThat(resource).isNotNull();
    assertThat(resource.getFilename()).isEqualTo(filename);
    assertThat(resource.getInputStream()).hasContent(body);
}
Also used : ContentDisposition(org.springframework.http.ContentDisposition) ByteArrayResource(org.springframework.core.io.ByteArrayResource) Resource(org.springframework.core.io.Resource) MockClientHttpResponse(org.springframework.web.testfixture.http.client.reactive.MockClientHttpResponse) Test(org.junit.jupiter.api.Test)

Example 3 with ContentDisposition

use of org.springframework.http.ContentDisposition in project OsmAnd-tools by osmandapp.

the class GpxController method calculateSrtmAltitude.

public GPXFile calculateSrtmAltitude(GPXFile gpxFile, File[] missingFile) {
    if (srtmLocation == null) {
        return null;
    }
    if (srtmLocation.startsWith("http://") || srtmLocation.startsWith("https://")) {
        String serverUrl = srtmLocation + "/gpx/process-srtm";
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GPXUtilities.writeGpx(new OutputStreamWriter(baos), gpxFile, null);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        MultiValueMap<String, String> fileMap = new LinkedMultiValueMap<>();
        ContentDisposition contentDisposition = ContentDisposition.builder("form-data").name("file").filename("route.gpx").build();
        fileMap.add(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString());
        HttpEntity<byte[]> fileEntity = new HttpEntity<>(baos.toByteArray(), fileMap);
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", fileEntity);
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<byte[]> response = restTemplate.postForEntity(serverUrl, requestEntity, byte[].class);
        if (response.getStatusCode().is2xxSuccessful()) {
            return GPXUtilities.loadGPXFile(new ByteArrayInputStream(response.getBody()));
        } else {
            return null;
        }
    } else {
        File srtmFolder = new File(srtmLocation);
        if (!srtmFolder.exists()) {
            return null;
        }
        IndexHeightData hd = new IndexHeightData();
        hd.setSrtmData(srtmFolder);
        for (Track tr : gpxFile.tracks) {
            for (TrkSegment s : tr.segments) {
                for (int i = 0; i < s.points.size(); i++) {
                    WptPt wpt = s.points.get(i);
                    double h = hd.getPointHeight(wpt.lat, wpt.lon, missingFile);
                    if (h != IndexHeightData.INEXISTENT_HEIGHT) {
                        wpt.ele = h;
                    } else if (i == 0) {
                        return null;
                    }
                }
            }
        }
    }
    return gpxFile;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) WptPt(net.osmand.GPXUtilities.WptPt) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TrkSegment(net.osmand.GPXUtilities.TrkSegment) ContentDisposition(org.springframework.http.ContentDisposition) ByteArrayInputStream(java.io.ByteArrayInputStream) IndexHeightData(net.osmand.obf.preparation.IndexHeightData) RestTemplate(org.springframework.web.client.RestTemplate) OutputStreamWriter(java.io.OutputStreamWriter) GPXFile(net.osmand.GPXUtilities.GPXFile) GPXSessionFile(net.osmand.server.controllers.pub.UserSessionResources.GPXSessionFile) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Track(net.osmand.GPXUtilities.Track)

Aggregations

ContentDisposition (org.springframework.http.ContentDisposition)3 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)2 MultipartFile (org.springframework.web.multipart.MultipartFile)2 Part (jakarta.servlet.http.Part)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 OutputStreamWriter (java.io.OutputStreamWriter)1 GPXFile (net.osmand.GPXUtilities.GPXFile)1 Track (net.osmand.GPXUtilities.Track)1 TrkSegment (net.osmand.GPXUtilities.TrkSegment)1 WptPt (net.osmand.GPXUtilities.WptPt)1 IndexHeightData (net.osmand.obf.preparation.IndexHeightData)1 GPXSessionFile (net.osmand.server.controllers.pub.UserSessionResources.GPXSessionFile)1 Test (org.junit.jupiter.api.Test)1 ByteArrayResource (org.springframework.core.io.ByteArrayResource)1 Resource (org.springframework.core.io.Resource)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 MultiValueMap (org.springframework.util.MultiValueMap)1