Search in sources :

Example 71 with DataType

use of com.revolsys.datatype.DataType in project com.revolsys.open by revolsys.

the class WebMethodHandler method cookie.

public static WebParameterHandler cookie(final WebAnnotationMethodHandlerAdapter adapter, final Parameter parameter, final Annotation annotation) {
    final Class<?> parameterClass = parameter.getType();
    final DataType dataType = DataTypes.getDataType(parameterClass);
    final CookieValue cookieValue = (CookieValue) annotation;
    final String name = getName(parameter, cookieValue.value());
    final boolean required = cookieValue.required();
    final Object defaultValue = parseDefaultValueAttribute(dataType, cookieValue.defaultValue());
    BiFunction<HttpServletRequest, HttpServletResponse, Object> function;
    if (Cookie.class.equals(parameterClass)) {
        function = (request, response) -> {
            final Cookie cookie = WebUtils.getCookie(request, name);
            return cookie;
        };
    } else {
        function = (request, response) -> {
            final Cookie cookie = WebUtils.getCookie(request, name);
            if (cookie == null) {
                return null;
            } else {
                return cookie.getValue();
            }
        };
    }
    return // 
    WebParameterHandler.function(// 
    name, // 
    function, // 
    dataType, // 
    required, // 
    defaultValue);
}
Also used : MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Cookie(javax.servlet.http.Cookie) CookieValue(org.springframework.web.bind.annotation.CookieValue) DataType(com.revolsys.datatype.DataType) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Example 72 with DataType

use of com.revolsys.datatype.DataType in project com.revolsys.open by revolsys.

the class WebMethodHandler method body.

@SuppressWarnings({ "unchecked", "rawtypes" })
public static WebParameterHandler body(final WebAnnotationMethodHandlerAdapter adapter, final Parameter parameter, final Annotation annotation) {
    final boolean required = ((RequestBody) annotation).required();
    final String parameterName = parameter.getName();
    final Class parameterClass = parameter.getType();
    final DataType dataType = DataTypes.getDataType(parameterClass);
    return // 
    WebParameterHandler.function(// 
    parameterName, (request, response) -> {
        try {
            final HttpInputMessage inputMessage = new ServletServerHttpRequest(request);
            MediaType contentType = MediaTypeUtil.getContentType(request);
            if (contentType == null) {
                contentType = MediaType.APPLICATION_FORM_URLENCODED;
            }
            if (!MediaType.APPLICATION_FORM_URLENCODED.includes(contentType) && !MediaType.MULTIPART_FORM_DATA.includes(contentType)) {
                contentType = MediaTypeUtil.getRequestMediaType(request, adapter.mediaTypes, adapter.mediaTypeOrder, adapter.urlPathHelper, adapter.parameterName, adapter.defaultMediaType, "");
            }
            final HttpHeaders headers = inputMessage.getHeaders();
            if (contentType == null) {
                final StringBuilder builder = new StringBuilder(ClassUtils.getShortName(parameterClass));
                final String paramName = parameterName;
                if (paramName != null) {
                    builder.append(' ');
                    builder.append(paramName);
                }
                throw new HttpMediaTypeNotSupportedException("Cannot extract @RequestBody parameter (" + builder.toString() + "): no Content-Type found");
            } else {
                HttpServletUtils.setContentTypeWithCharset(headers, contentType);
            }
            final List<MediaType> allSupportedMediaTypes = new ArrayList<>();
            if (adapter.messageConverters != null) {
                for (final HttpMessageConverter<?> messageConverter : adapter.messageConverters) {
                    allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes());
                    if (messageConverter.canRead(parameterClass, contentType)) {
                        return messageConverter.read(parameterClass, inputMessage);
                    }
                }
                String body = null;
                if (MediaType.APPLICATION_FORM_URLENCODED.includes(contentType)) {
                    Charset charset = contentType.getCharSet();
                    if (charset == null) {
                        charset = StandardCharsets.UTF_8;
                    }
                    final String urlBody = FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), charset));
                    final String[] pairs = StringUtils.tokenizeToStringArray(urlBody, "&");
                    final MultiValueMap<String, String> values = new LinkedMultiValueMap<>(pairs.length);
                    for (final String pair : pairs) {
                        final int idx = pair.indexOf('=');
                        if (idx == -1) {
                            values.add(URLDecoder.decode(pair, charset.name()), null);
                        } else {
                            final String name = URLDecoder.decode(pair.substring(0, idx), charset.name());
                            final String value = URLDecoder.decode(pair.substring(idx + 1), charset.name());
                            values.add(name, value);
                        }
                    }
                    body = values.getFirst("body");
                } else if (request instanceof MultipartHttpServletRequest) {
                    final MultipartHttpServletRequest multiPartRequest = (MultipartHttpServletRequest) request;
                    final MultipartFile bodyFile = multiPartRequest.getFile("body");
                    contentType = MediaTypeUtil.getRequestMediaType(request, adapter.mediaTypes, adapter.mediaTypeOrder, adapter.urlPathHelper, adapter.parameterName, adapter.defaultMediaType, bodyFile.getOriginalFilename());
                    HttpServletUtils.setContentTypeWithCharset(headers, contentType);
                    final HttpInputMessage newInputMessage = new HttpInputMessage() {

                        @Override
                        public InputStream getBody() throws IOException {
                            return bodyFile.getInputStream();
                        }

                        @Override
                        public HttpHeaders getHeaders() {
                            return headers;
                        }
                    };
                    for (final HttpMessageConverter<?> messageConverter : adapter.messageConverters) {
                        if (messageConverter.canRead(parameterClass, contentType)) {
                            return messageConverter.read(parameterClass, newInputMessage);
                        }
                    }
                }
                if (body == null) {
                    body = request.getParameter("body");
                }
                if (body != null) {
                    contentType = MediaTypeUtil.getRequestMediaType(request, adapter.mediaTypes, adapter.mediaTypeOrder, adapter.urlPathHelper, adapter.parameterName, adapter.defaultMediaType, "");
                    HttpServletUtils.setContentTypeWithCharset(headers, contentType);
                    byte[] bytes;
                    bytes = body.getBytes();
                    final InputStream bodyIn = new ByteArrayInputStream(bytes);
                    final HttpInputMessage newInputMessage = new HttpInputMessage() {

                        @Override
                        public InputStream getBody() throws IOException {
                            return bodyIn;
                        }

                        @Override
                        public HttpHeaders getHeaders() {
                            return headers;
                        }
                    };
                    for (final HttpMessageConverter<?> messageConverter : adapter.messageConverters) {
                        if (messageConverter.canRead(parameterClass, contentType)) {
                            return messageConverter.read(parameterClass, newInputMessage);
                        }
                    }
                }
            }
            throw new HttpMediaTypeNotSupportedException(contentType, allSupportedMediaTypes);
        } catch (final Exception e) {
            return Exceptions.throwUncheckedException(e);
        }
    }, // 
    dataType, // 
    required, // 
    null);
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) HttpHeaders(org.springframework.http.HttpHeaders) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ArrayList(java.util.ArrayList) HttpMediaTypeNotSupportedException(org.springframework.web.HttpMediaTypeNotSupportedException) MultipartFile(org.springframework.web.multipart.MultipartFile) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) DataType(com.revolsys.datatype.DataType) MediaType(org.springframework.http.MediaType) RequestBody(org.springframework.web.bind.annotation.RequestBody) HttpInputMessage(org.springframework.http.HttpInputMessage) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) IOException(java.io.IOException) WrappedException(com.revolsys.util.WrappedException) IOException(java.io.IOException) HttpMediaTypeNotSupportedException(org.springframework.web.HttpMediaTypeNotSupportedException) ByteArrayInputStream(java.io.ByteArrayInputStream) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest)

Example 73 with DataType

use of com.revolsys.datatype.DataType in project com.revolsys.open by revolsys.

the class GmlFieldTypeRegistry method addFieldType.

public void addFieldType(final GmlFieldType fieldType) {
    final DataType dataType = fieldType.getDataType();
    addFieldType(dataType, fieldType);
}
Also used : DataType(com.revolsys.datatype.DataType)

Example 74 with DataType

use of com.revolsys.datatype.DataType in project com.revolsys.open by revolsys.

the class FeatureLayer method initialize.

@Override
protected void initialize(final MapEx properties) {
    super.initialize(properties);
    this.boundingBox = ArcGisResponse.newBoundingBox(properties, "extent");
    final PathName pathName = getPathName();
    final List<MapEx> fields = properties.getValue("fields");
    if (fields != null) {
        final RecordDefinitionImpl newRecordDefinition = new RecordDefinitionImpl(pathName);
        newRecordDefinition.setPolygonRingDirection(ClockDirection.CLOCKWISE);
        final String description = properties.getString("description");
        newRecordDefinition.setDescription(description);
        final String geometryType = properties.getString("geometryType");
        for (final MapEx field : fields) {
            addField(newRecordDefinition, geometryType, field);
        }
        if (Property.hasValue(geometryType)) {
            if (!newRecordDefinition.hasGeometryField()) {
                final DataType geometryDataType = getGeometryDataType(geometryType);
                if (geometryDataType == null) {
                    throw new IllegalArgumentException("No geometryType specified for " + getServiceUrl());
                } else {
                    newRecordDefinition.addField("GEOMETRY", geometryDataType);
                }
            }
        }
        if (this.boundingBox != null) {
            final GeometryFactory geometryFactory = this.boundingBox.getGeometryFactory();
            newRecordDefinition.setGeometryFactory(geometryFactory);
        }
        final FieldDefinition objectIdField = newRecordDefinition.getField("OBJECTID");
        if (newRecordDefinition.getIdField() == null && objectIdField != null) {
            final int fieldIndex = objectIdField.getIndex();
            newRecordDefinition.setIdFieldIndex(fieldIndex);
            objectIdField.setRequired(true);
        }
        this.recordDefinition = newRecordDefinition;
    }
}
Also used : GeometryFactory(com.revolsys.geometry.model.GeometryFactory) MapEx(com.revolsys.collection.map.MapEx) FieldDefinition(com.revolsys.record.schema.FieldDefinition) RecordDefinitionImpl(com.revolsys.record.schema.RecordDefinitionImpl) DataType(com.revolsys.datatype.DataType) PathName(com.revolsys.io.PathName)

Example 75 with DataType

use of com.revolsys.datatype.DataType in project com.revolsys.open by revolsys.

the class GeometryFieldDefinition method clone.

@Override
public FieldDefinition clone() {
    final String name = getName();
    final DataType dataType = getDataType();
    final boolean required = isRequired();
    return new GeometryFieldDefinition(this.geometryFactory, name, dataType, required);
}
Also used : DataType(com.revolsys.datatype.DataType)

Aggregations

DataType (com.revolsys.datatype.DataType)85 FieldDefinition (com.revolsys.record.schema.FieldDefinition)32 GeometryFactory (com.revolsys.geometry.model.GeometryFactory)23 PathName (com.revolsys.io.PathName)13 RecordDefinition (com.revolsys.record.schema.RecordDefinition)11 Geometry (com.revolsys.geometry.model.Geometry)10 Record (com.revolsys.record.Record)10 RecordDefinitionImpl (com.revolsys.record.schema.RecordDefinitionImpl)10 ArrayList (java.util.ArrayList)8 ArrayRecord (com.revolsys.record.ArrayRecord)6 PrintWriter (java.io.PrintWriter)6 EsriGeodatabaseXmlFieldType (com.revolsys.record.io.format.esri.gdb.xml.type.EsriGeodatabaseXmlFieldType)5 MapEx (com.revolsys.collection.map.MapEx)4 CoordinateSystem (com.revolsys.geometry.cs.CoordinateSystem)4 Point (com.revolsys.geometry.model.Point)4 IOException (java.io.IOException)4 Map (java.util.Map)4 CollectionDataType (com.revolsys.datatype.CollectionDataType)3 EnumerationDataType (com.revolsys.datatype.EnumerationDataType)3 CodeTable (com.revolsys.record.code.CodeTable)3