You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ju...@apache.org on 2016/11/30 17:11:03 UTC

svn commit: r1772087 - in /sling/trunk/bundles/extensions/models: impl/src/main/java/org/apache/sling/models/impl/ integration-tests/ integration-tests/src/main/java/org/apache/sling/models/it/exporter/

Author: justin
Date: Wed Nov 30 17:11:02 2016
New Revision: 1772087

URL: http://svn.apache.org/viewvc?rev=1772087&view=rev
Log:
SLING-6346 - fixing null return for sling models adapting from SlingHttpServletRequest

This closes #187

Added:
    sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/BaseRequestComponent.java
    sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ExtendedRequestComponent.java
    sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/RequestComponentImpl.java
Modified:
    sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
    sling/trunk/bundles/extensions/models/integration-tests/pom.xml
    sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ExporterTest.java

Modified: sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java?rev=1772087&r1=1772086&r2=1772087&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java (original)
+++ sling/trunk/bundles/extensions/models/impl/src/main/java/org/apache/sling/models/impl/ModelAdapterFactory.java Wed Nov 30 17:11:02 2016
@@ -1161,9 +1161,7 @@ public class ModelAdapterFactory impleme
             throw new ModelClassException("Could find model registered for request path: " + request.getServletPath());
         }
         Result<?> result = internalCreateModel(request, clazz);
-        handleAndExportResult(result, name, targetClass, options);
-        // unreachable
-        return null;
+        return handleAndExportResult(result, name, targetClass, options);
     }
 
     private <T> T handleAndExportResult(Result<?> result, String name, Class<T> targetClass, Map<String, String> options) throws ExportException, MissingExporterException {

Modified: sling/trunk/bundles/extensions/models/integration-tests/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/integration-tests/pom.xml?rev=1772087&r1=1772086&r2=1772087&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/integration-tests/pom.xml (original)
+++ sling/trunk/bundles/extensions/models/integration-tests/pom.xml Wed Nov 30 17:11:02 2016
@@ -120,7 +120,10 @@
                         <Sling-Model-Classes>
                             org.apache.sling.models.it.exporter.BaseComponent,
                             org.apache.sling.models.it.exporter.ComponentImpl,
-                            org.apache.sling.models.it.exporter.ExtendedComponent
+                            org.apache.sling.models.it.exporter.ExtendedComponent,
+                            org.apache.sling.models.it.exporter.BaseRequestComponent,
+                            org.apache.sling.models.it.exporter.RequestComponentImpl,
+                            org.apache.sling.models.it.exporter.ExtendedRequestComponent
                         </Sling-Model-Classes>
                         <Sling-Test-Regexp>.*Test</Sling-Test-Regexp>
                         <Export-Package>org.apache.sling.models.it</Export-Package>
@@ -303,7 +306,7 @@
         <dependency>
             <groupId>org.apache.sling</groupId>
             <artifactId>org.apache.sling.models.impl</artifactId>
-            <version>1.3.3-SNAPSHOT</version>
+            <version>1.3.5-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
         <dependency>

Added: sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/BaseRequestComponent.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/BaseRequestComponent.java?rev=1772087&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/BaseRequestComponent.java (added)
+++ sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/BaseRequestComponent.java Wed Nov 30 17:11:02 2016
@@ -0,0 +1,62 @@
+/*
+ * 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.it.exporter;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Exporter;
+import org.apache.sling.models.annotations.Model;
+import org.apache.sling.models.annotations.Via;
+import org.apache.sling.models.annotations.injectorspecific.SlingObject;
+
+import javax.inject.Inject;
+
+@Model(adaptables = { SlingHttpServletRequest.class }, resourceType = "sling/exp-request/base")
+@Exporter(name = "jackson", extensions = "json")
+public class BaseRequestComponent {
+
+    @Inject @SlingObject
+    private Resource resource;
+
+    @Inject @Via("resource")
+    private String sampleValue;
+
+    private final SlingHttpServletRequest request;
+
+    public BaseRequestComponent(SlingHttpServletRequest request) {
+        this.request = request;
+    }
+
+    public String getId() {
+        return this.resource.getPath();
+    }
+
+    public String getSampleValue() {
+        return sampleValue;
+    }
+
+    @JsonProperty(value="UPPER")
+    public String getSampleValueToUpperCase() {
+        return sampleValue.toUpperCase();
+    }
+
+    public Resource getResource() {
+        return resource;
+    }
+
+}

Modified: sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ExporterTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ExporterTest.java?rev=1772087&r1=1772086&r2=1772087&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ExporterTest.java (original)
+++ sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ExporterTest.java Wed Nov 30 17:11:02 2016
@@ -57,6 +57,9 @@ public class ExporterTest {
     private final String childComponentPath = "/content/exp/childComponent";
     private final String extendedComponentPath = "/content/exp/extendedComponent";
     private final String interfaceComponentPath = "/content/exp/interfaceComponent";
+    private final String baseRequestComponentPath = "/content/exp-request/baseComponent";
+    private final String extendedRequestComponentPath = "/content/exp-request/extendedComponent";
+    private final String interfaceRequestComponentPath = "/content/exp-request/interfaceComponent";
     private Calendar testDate;
 
     @Before
@@ -69,6 +72,10 @@ public class ExporterTest {
             properties.put(SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE,
                     "sling/exp/base");
             ResourceUtil.getOrCreateResource(adminResolver, baseComponentPath, properties, null, false);
+
+            properties.put(SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE,
+                    "sling/exp-request/base");
+            ResourceUtil.getOrCreateResource(adminResolver, baseRequestComponentPath, properties, null, false);
             properties.clear();
 
             properties.put("sampleValue", "childTESTValue");
@@ -88,12 +95,20 @@ public class ExporterTest {
             testDate.set(2015, 6, 29);
             properties.put("date", testDate);
             ResourceUtil.getOrCreateResource(adminResolver, extendedComponentPath, properties, null, false);
+
+            properties.put(SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE,
+                    "sling/exp-request/extended");
+            ResourceUtil.getOrCreateResource(adminResolver, extendedRequestComponentPath, properties, null, false);
             properties.clear();
 
             properties.put("sampleValue", "interfaceTESTValue");
             properties.put(SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE,
                     "sling/exp/interface");
             ResourceUtil.getOrCreateResource(adminResolver, interfaceComponentPath, properties, null, false);
+
+            properties.put(SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE,
+                    "sling/exp-request/interface");
+            ResourceUtil.getOrCreateResource(adminResolver, interfaceRequestComponentPath, properties, null, false);
             properties.clear();
 
             adminResolver.commit();
@@ -155,7 +170,7 @@ public class ExporterTest {
     }
 
     @Test
-    public void testServlets() throws Exception {
+    public void testResourceServlets() throws Exception {
         ResourceResolver resolver = null;
         try {
             resolver = rrFactory.getAdministrativeResourceResolver(null);
@@ -169,16 +184,50 @@ public class ExporterTest {
             response = new FakeResponse();
             slingRequestProcessor.processRequest(new FakeRequest(extendedComponentPath + ".model.json"), response, resolver);
             obj = new JSONObject(response.getStringWriter().toString());
+            Assert.assertEquals("application/json", response.getContentType());
             Assert.assertEquals(extendedComponentPath, obj.getString("id"));
             Assert.assertEquals(testDate.getTimeInMillis(), obj.getLong("date"));
 
             response = new FakeResponse();
             slingRequestProcessor.processRequest(new FakeRequest(interfaceComponentPath + ".model.json"), response, resolver);
             obj = new JSONObject(response.getStringWriter().toString());
+            Assert.assertEquals("application/json", response.getContentType());
             Assert.assertEquals(interfaceComponentPath, obj.getString("id"));
             Assert.assertEquals("interfaceTESTValue", obj.getString("sampleValue"));
         } finally {
             if (resolver != null && resolver.isLive()) {
+                resolver.close();
+            }
+        }
+    }
+
+    @Test
+    public void testRequestServlets() throws Exception {
+        ResourceResolver resolver = null;
+        try {
+            resolver = rrFactory.getAdministrativeResourceResolver(null);
+            FakeResponse response = new FakeResponse();
+            slingRequestProcessor.processRequest(new FakeRequest(baseRequestComponentPath + ".model.json"), response, resolver);
+            JSONObject obj = new JSONObject(response.getStringWriter().toString());
+            Assert.assertEquals("application/json", response.getContentType());
+            Assert.assertEquals("BASETESTVALUE", obj.getString("UPPER"));
+            Assert.assertEquals(baseRequestComponentPath, obj.getString("id"));
+
+            response = new FakeResponse();
+            slingRequestProcessor.processRequest(new FakeRequest(extendedRequestComponentPath + ".model.json"), response, resolver);
+            obj = new JSONObject(response.getStringWriter().toString());
+            Assert.assertEquals("application/json", response.getContentType());
+            Assert.assertEquals(extendedRequestComponentPath, obj.getString("id"));
+            Assert.assertEquals(testDate.getTimeInMillis(), obj.getLong("date"));
+
+            response = new FakeResponse();
+            slingRequestProcessor.processRequest(new FakeRequest(interfaceRequestComponentPath + ".model.json"), response, resolver);
+            obj = new JSONObject(response.getStringWriter().toString());
+            Assert.assertEquals("application/json", response.getContentType());
+            Assert.assertEquals(interfaceRequestComponentPath, obj.getString("id"));
+            Assert.assertEquals("interfaceTESTValue", obj.getString("sampleValue"));
+        } finally {
+            if (resolver != null && resolver.isLive()) {
                 resolver.close();
             }
         }

Added: sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ExtendedRequestComponent.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ExtendedRequestComponent.java?rev=1772087&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ExtendedRequestComponent.java (added)
+++ sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/ExtendedRequestComponent.java Wed Nov 30 17:11:02 2016
@@ -0,0 +1,50 @@
+/*
+ * 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.it.exporter;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Exporter;
+import org.apache.sling.models.annotations.Model;
+import org.apache.sling.models.annotations.Via;
+
+import javax.inject.Inject;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+
+@Model(adaptables = { SlingHttpServletRequest.class }, resourceType = "sling/exp-request/extended")
+@Exporter(name = "jackson", extensions = "json")
+public class ExtendedRequestComponent extends BaseRequestComponent {
+
+    @Inject @Via("resource")
+    private Date date;
+
+    public ExtendedRequestComponent(SlingHttpServletRequest request) {
+        super(request);
+    }
+
+    public Calendar getDateByCalendar() {
+        Calendar cal = new GregorianCalendar();
+        cal.setTime(date);
+        return cal;
+    }
+
+    public Date getDate() {
+        return date;
+    }
+}

Added: sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/RequestComponentImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/RequestComponentImpl.java?rev=1772087&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/RequestComponentImpl.java (added)
+++ sling/trunk/bundles/extensions/models/integration-tests/src/main/java/org/apache/sling/models/it/exporter/RequestComponentImpl.java Wed Nov 30 17:11:02 2016
@@ -0,0 +1,51 @@
+/*
+ * 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.it.exporter;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.models.annotations.Exporter;
+import org.apache.sling.models.annotations.Model;
+import org.apache.sling.models.annotations.Via;
+import org.apache.sling.models.annotations.injectorspecific.SlingObject;
+
+import javax.inject.Inject;
+
+@Model(adaptables = { SlingHttpServletRequest.class }, adapters = Component.class, resourceType = "sling/exp-request/interface")
+@Exporter(name = "jackson", extensions = "json")
+public class RequestComponentImpl implements Component {
+
+    @Inject @SlingObject
+    private Resource resource;
+
+    @Inject @Via("resource")
+    private String sampleValue;
+
+    private final SlingHttpServletRequest request;
+
+    public RequestComponentImpl(SlingHttpServletRequest request) {
+        this.request = request;
+    }
+
+    public String getId() {
+        return this.resource.getPath();
+    }
+
+    public String getSampleValue() {
+        return sampleValue;
+    }
+}