You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2017/10/18 23:20:49 UTC

[sling-org-apache-sling-models-jacksonexporter] 16/26: SLING-6349 - provide custom serialization for request objects

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-jacksonexporter.git

commit 7f32d44c2fddccc58428f2c658cc0d0313627a9f
Author: Justin Edelson <ju...@apache.org>
AuthorDate: Wed Nov 30 20:36:27 2016 +0000

    SLING-6349 - provide custom serialization for request objects
    
    git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1772113 13f79535-47bb-0310-9956-ffa450edef68
---
 pom.xml                                            |  11 +++
 .../impl/EnumerationSerializer.java                |  49 ++++++++++
 .../impl/HttpServletRequestMixin.java              | 101 +++++++++++++++++++++
 .../impl/RequestModuleProvider.java                |  54 +++++++++++
 .../jacksonexporter/impl/ServletRequestMixin.java  |  86 ++++++++++++++++++
 .../impl/SlingHttpServletRequestMixin.java         |  46 ++++++++++
 6 files changed, 347 insertions(+)

diff --git a/pom.xml b/pom.xml
index 7fc7f9c..9c7008a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -115,6 +115,17 @@
             <version>2.4.0</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>commons-collections</groupId>
+            <artifactId>commons-collections</artifactId>
+            <version>3.2.1</version>
+            <scope>provided</scope>
+        </dependency>
         <!-- *************************************************************** -->
         <!-- JACKSON -->
         <!-- *************************************************************** -->
diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/EnumerationSerializer.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/EnumerationSerializer.java
new file mode 100644
index 0000000..06b3398
--- /dev/null
+++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/EnumerationSerializer.java
@@ -0,0 +1,49 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.jacksonexporter.impl;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.JsonSerializer;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.ser.ResolvableSerializer;
+import org.apache.commons.collections.iterators.EnumerationIterator;
+
+import java.io.IOException;
+import java.util.Calendar;
+import java.util.Enumeration;
+import java.util.Iterator;
+
+/**
+ * Trivial serializer for Enumeration types (needed for Servlet APIs) which leverages
+ * the existing Jackson support for Iterators.
+ */
+public class EnumerationSerializer extends JsonSerializer<Enumeration> implements ResolvableSerializer {
+
+    private JsonSerializer<Object> iteratorSerializer;
+
+    @Override
+    public void resolve(SerializerProvider provider) throws JsonMappingException {
+        this.iteratorSerializer = provider.findValueSerializer(Iterator.class, null);
+    }
+
+    @Override
+    public void serialize(Enumeration value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
+        iteratorSerializer.serialize(new EnumerationIterator(value), jgen, provider);
+    }
+}
diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/HttpServletRequestMixin.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/HttpServletRequestMixin.java
new file mode 100644
index 0000000..383ae2a
--- /dev/null
+++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/HttpServletRequestMixin.java
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.jacksonexporter.impl;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonGetter;
+
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import java.security.Principal;
+import java.util.Enumeration;
+
+@JsonAutoDetect(getterVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE)
+public abstract class HttpServletRequestMixin extends ServletRequestMixin implements HttpServletRequest {
+
+    @JsonGetter
+    @Override
+    public abstract String getAuthType();
+
+    @JsonGetter
+    @Override
+    public abstract Cookie[] getCookies();
+
+    @JsonGetter
+    @Override
+    public abstract String getMethod();
+
+    @JsonGetter
+    @Override
+    public abstract String getPathInfo();
+
+    @JsonGetter
+    @Override
+    public abstract String getPathTranslated();
+
+    @JsonGetter
+    @Override
+    public abstract String getContextPath();
+
+    @JsonGetter
+    @Override
+    public abstract String getQueryString();
+
+    @JsonGetter
+    @Override
+    public abstract  String getRemoteUser();
+
+    @JsonGetter
+    @Override
+    public abstract String getRemoteHost();
+
+    @JsonGetter
+    @Override
+    public abstract Principal getUserPrincipal();
+
+    @JsonGetter
+    @Override
+    public abstract String getRequestedSessionId();
+
+    @JsonGetter
+    @Override
+    public abstract String getRequestURI();
+
+    @JsonGetter
+    @Override
+    public abstract boolean isRequestedSessionIdFromCookie();
+
+    @JsonGetter
+    @Override
+    public abstract boolean isRequestedSessionIdFromURL();
+
+    @JsonGetter
+    @Override
+    public abstract boolean isRequestedSessionIdValid();
+
+    @JsonGetter
+    @Override
+    public abstract boolean isRequestedSessionIdFromUrl();
+
+    @JsonGetter
+    @Override
+    public abstract Enumeration getHeaderNames();
+
+    @JsonGetter
+    @Override
+    public abstract String getServletPath();
+}
diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/RequestModuleProvider.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/RequestModuleProvider.java
new file mode 100644
index 0000000..0ee0d8c
--- /dev/null
+++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/RequestModuleProvider.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.jacksonexporter.impl;
+
+import com.fasterxml.jackson.databind.Module;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+import com.fasterxml.jackson.databind.module.SimpleSerializers;
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.jacksonexporter.ModuleProvider;
+import org.osgi.framework.Constants;
+
+import javax.servlet.ServletRequest;
+import javax.servlet.http.HttpServletRequest;
+import java.util.Enumeration;
+
+@Component
+@Service
+@Property(name = Constants.SERVICE_RANKING, intValue = 0)
+public class RequestModuleProvider implements ModuleProvider {
+
+    private final SimpleModule moduleInstance;
+
+    public RequestModuleProvider() {
+        this.moduleInstance = new SimpleModule();
+        moduleInstance.setMixInAnnotation(SlingHttpServletRequest.class, SlingHttpServletRequestMixin.class);
+        moduleInstance.setMixInAnnotation(HttpServletRequest.class, HttpServletRequestMixin.class);
+        moduleInstance.setMixInAnnotation(ServletRequest.class, ServletRequestMixin.class);
+        moduleInstance.addSerializer(Enumeration.class, new EnumerationSerializer());
+    }
+
+    @Override
+    public Module getModule() {
+        return moduleInstance;
+    }
+}
diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/ServletRequestMixin.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/ServletRequestMixin.java
new file mode 100644
index 0000000..a98c9f7
--- /dev/null
+++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/ServletRequestMixin.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.jacksonexporter.impl;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonGetter;
+
+import javax.servlet.ServletRequest;
+import java.util.Enumeration;
+import java.util.Locale;
+import java.util.Map;
+
+@JsonAutoDetect(getterVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE)
+public abstract class ServletRequestMixin implements ServletRequest {
+
+    @JsonGetter("parameters")
+    @Override
+    public abstract Map getParameterMap();
+
+    @JsonGetter
+    @Override
+    public abstract Locale getLocale();
+
+    @JsonGetter
+    @Override
+    public abstract String getContentType();
+
+    @JsonGetter
+    @Override
+    public abstract int getContentLength();
+
+    @JsonGetter
+    @Override
+    public abstract int getRemotePort();
+
+    @JsonGetter
+    @Override
+    public abstract String getRemoteAddr();
+
+    @JsonGetter
+    @Override
+    public abstract int getServerPort();
+
+    @JsonGetter
+    @Override
+    public abstract String getServerName();
+
+    @JsonGetter
+    @Override
+    public abstract boolean isSecure();
+
+    @Override
+    public abstract Enumeration getLocales();
+
+    @Override
+    public abstract String getCharacterEncoding();
+
+    @Override
+    public abstract int getLocalPort();
+
+    @Override
+    public abstract String getLocalAddr();
+
+    @Override
+    public abstract String getLocalName();
+
+    @Override
+    public abstract String getProtocol();
+
+    @Override
+    public abstract String getScheme();
+}
diff --git a/src/main/java/org/apache/sling/models/jacksonexporter/impl/SlingHttpServletRequestMixin.java b/src/main/java/org/apache/sling/models/jacksonexporter/impl/SlingHttpServletRequestMixin.java
new file mode 100644
index 0000000..a4567c4
--- /dev/null
+++ b/src/main/java/org/apache/sling/models/jacksonexporter/impl/SlingHttpServletRequestMixin.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.models.jacksonexporter.impl;
+
+import com.fasterxml.jackson.annotation.JsonAutoDetect;
+import com.fasterxml.jackson.annotation.JsonGetter;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.request.RequestPathInfo;
+import org.apache.sling.api.resource.Resource;
+
+import java.util.Enumeration;
+
+@JsonAutoDetect(getterVisibility = JsonAutoDetect.Visibility.NONE, fieldVisibility = JsonAutoDetect.Visibility.NONE, isGetterVisibility = JsonAutoDetect.Visibility.NONE)
+public abstract class SlingHttpServletRequestMixin extends HttpServletRequestMixin implements SlingHttpServletRequest {
+
+    @JsonGetter
+    @Override
+    public abstract Resource getResource();
+
+    @JsonGetter
+    @Override
+    public abstract RequestPathInfo getRequestPathInfo();
+
+    @JsonGetter
+    @Override
+    public abstract String getResponseContentType();
+
+    @JsonGetter()
+    @Override
+    public abstract Enumeration<String> getResponseContentTypes();
+
+}

-- 
To stop receiving notification emails like this one, please contact
"commits@sling.apache.org" <co...@sling.apache.org>.