Search in sources :

Example 56 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project com.revolsys.open by revolsys.

the class HttpMessageConverterView method renderMergedOutputModel.

@Override
protected void renderMergedOutputModel(final Map<String, Object> model, final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    final String path = (String) requestAttributes.getAttribute("httpMessageConverterTemplatePath", RequestAttributes.SCOPE_REQUEST);
    if (path == null || !Arrays.asList(MediaType.TEXT_HTML, MediaType.APPLICATION_XHTML_XML).contains(this.mediaType)) {
        render(response);
    } else {
        final Charset charSet = this.mediaType.getCharSet();
        if (charSet == null) {
            response.setContentType(this.mediaType.toString() + "; charset=UTF-8");
        } else {
            response.setContentType(this.mediaType.toString());
        }
        final HttpMessageConverterView savedView = getMessageConverterView();
        requestAttributes.setAttribute(NAME, this, RequestAttributes.SCOPE_REQUEST);
        if (!PathViewController.include(request, response, path)) {
            render(response);
        }
        requestAttributes.setAttribute(NAME, savedView, RequestAttributes.SCOPE_REQUEST);
    }
}
Also used : Charset(java.nio.charset.Charset) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 57 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project com.revolsys.open by revolsys.

the class HttpMessageConverterView method getMessageConverterView.

public static HttpMessageConverterView getMessageConverterView() {
    final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    final HttpMessageConverterView view = (HttpMessageConverterView) requestAttributes.getAttribute(NAME, RequestAttributes.SCOPE_REQUEST);
    return view;
}
Also used : RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 58 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project com.revolsys.open by revolsys.

the class PathAliasController method handleRequest.

@Override
public ModelAndView handleRequest(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    String path = request.getServletPath() + request.getPathInfo();
    if (path.startsWith(this.prefix)) {
        if (getOriginalPrefix().length() == 0) {
            final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            requestAttributes.setAttribute(PATH_PREFIX, this.prefix.replaceAll(this.aliasPrefix + "$", ""), RequestAttributes.SCOPE_REQUEST);
        }
        path = path.replaceFirst(this.prefix, this.aliasPrefix);
        if (!forward(request, response, path)) {
            throw new PageNotFoundException();
        }
    }
    return null;
}
Also used : PageNotFoundException(com.revolsys.ui.web.exception.PageNotFoundException) RequestAttributes(org.springframework.web.context.request.RequestAttributes)

Example 59 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project com.revolsys.open by revolsys.

the class RecordHttpMessageConverter method write.

@Override
public void write(final Record record, final MediaType mediaType, final HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    if (!HttpServletUtils.getResponse().isCommitted()) {
        if (record != null) {
            final RecordDefinition recordDefinition = record.getRecordDefinition();
            final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
            requestAttributes.setAttribute(IoConstants.SINGLE_OBJECT_PROPERTY, true, RequestAttributes.SCOPE_REQUEST);
            final ListRecordReader reader = new ListRecordReader(recordDefinition, record);
            this.readerConverter.write(reader, mediaType, outputMessage);
        }
    }
}
Also used : ListRecordReader(com.revolsys.record.io.ListRecordReader) RequestAttributes(org.springframework.web.context.request.RequestAttributes) RecordDefinition(com.revolsys.record.schema.RecordDefinition)

Example 60 with RequestAttributes

use of org.springframework.web.context.request.RequestAttributes in project com.revolsys.open by revolsys.

the class RecordReaderHttpMessageConverter method write.

@Override
public void write(final RecordReader reader, final MediaType mediaType, final HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
    if (!HttpServletUtils.getResponse().isCommitted()) {
        MediaType actualMediaType;
        if (mediaType == null) {
            actualMediaType = getDefaultMediaType();
        } else {
            actualMediaType = mediaType;
        }
        if (actualMediaType != null) {
            final Charset charset = HttpServletUtils.setContentTypeWithCharset(outputMessage, actualMediaType);
            final String mediaTypeString = actualMediaType.getType() + "/" + actualMediaType.getSubtype();
            final RecordWriterFactory writerFactory = IoFactory.factoryByMediaType(RecordWriterFactory.class, mediaTypeString);
            if (writerFactory == null) {
                throw new IllegalArgumentException("Media type " + actualMediaType + " not supported");
            } else {
                final RecordDefinition recordDefinition = reader.getRecordDefinition();
                final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
                String baseName = (String) requestAttributes.getAttribute("contentDispositionFileName", RequestAttributes.SCOPE_REQUEST);
                if (baseName == null) {
                    baseName = HttpServletUtils.getRequestBaseFileName();
                }
                String contentDisposition = (String) requestAttributes.getAttribute("contentDisposition", RequestAttributes.SCOPE_REQUEST);
                if (contentDisposition == null) {
                    contentDisposition = "attachment";
                }
                final String fileName = baseName + "." + writerFactory.getFileExtension(mediaTypeString);
                final HttpHeaders headers = outputMessage.getHeaders();
                headers.set("Content-Disposition", contentDisposition + "; filename=" + fileName);
                final OutputStream body = outputMessage.getBody();
                final Writer<Record> writer = writerFactory.newRecordWriter(baseName, recordDefinition, body, charset);
                if (Boolean.FALSE.equals(requestAttributes.getAttribute("wrapHtml", RequestAttributes.SCOPE_REQUEST))) {
                    writer.setProperty(IoConstants.WRAP_PROPERTY, false);
                }
                final HttpServletRequest request = HttpServletUtils.getRequest();
                String callback = request.getParameter("jsonp");
                if (callback == null) {
                    callback = request.getParameter("callback");
                }
                if (callback != null) {
                    writer.setProperty(IoConstants.JSONP_PROPERTY, callback);
                }
                for (final String attributeName : requestAttributes.getAttributeNames(RequestAttributes.SCOPE_REQUEST)) {
                    final Object value = requestAttributes.getAttribute(attributeName, RequestAttributes.SCOPE_REQUEST);
                    if (value != null && attributeName.startsWith("java:") || this.requestAttributeNames.contains(attributeName)) {
                        writer.setProperty(attributeName, value);
                    }
                }
                final Iterator<Record> iterator = reader.iterator();
                if (iterator.hasNext()) {
                    Record record = iterator.next();
                    final Geometry geometry = record.getGeometry();
                    if (geometry != null) {
                        final GeometryFactory geometryFactory = geometry.getGeometryFactory();
                        writer.setProperty(IoConstants.GEOMETRY_FACTORY, geometryFactory);
                    }
                    writer.write(record);
                    while (iterator.hasNext()) {
                        record = iterator.next();
                        writer.write(record);
                    }
                }
                writer.close();
            }
        }
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) GeometryFactory(com.revolsys.geometry.model.GeometryFactory) OutputStream(java.io.OutputStream) Charset(java.nio.charset.Charset) RequestAttributes(org.springframework.web.context.request.RequestAttributes) RecordWriterFactory(com.revolsys.record.io.RecordWriterFactory) RecordDefinition(com.revolsys.record.schema.RecordDefinition) HttpServletRequest(javax.servlet.http.HttpServletRequest) Geometry(com.revolsys.geometry.model.Geometry) MediaType(org.springframework.http.MediaType) Record(com.revolsys.record.Record)

Aggregations

RequestAttributes (org.springframework.web.context.request.RequestAttributes)81 ServletRequestAttributes (org.springframework.web.context.request.ServletRequestAttributes)51 HttpServletRequest (javax.servlet.http.HttpServletRequest)20 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)15 Test (org.junit.Test)11 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)3 DefaultPrettyPrinter (com.fasterxml.jackson.core.util.DefaultPrettyPrinter)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 Response (javax.ws.rs.core.Response)3 XWorkRequestAttributes (org.onebusaway.presentation.impl.users.XWorkRequestAttributes)3 DBUnitTest (org.orcid.test.DBUnitTest)3 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)3 ProjectService (ca.corefacility.bioinformatics.irida.service.ProjectService)2 UserService (ca.corefacility.bioinformatics.irida.service.user.UserService)2 UserSession (com.haulmont.cuba.security.global.UserSession)2 ActionContext (com.opensymphony.xwork2.ActionContext)2 RecordDefinition (com.revolsys.record.schema.RecordDefinition)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2