SLING-6346 - fixing null return for sling models adapting from SlingHttpServletRequest
This closes #187
git-svn-id: https://svn.apache.org/repos/asf/sling/trunk@1772087 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index 025ee0f..c9017e9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -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>
diff --git a/src/main/java/org/apache/sling/models/it/exporter/BaseRequestComponent.java b/src/main/java/org/apache/sling/models/it/exporter/BaseRequestComponent.java
new file mode 100644
index 0000000..242dc69
--- /dev/null
+++ b/src/main/java/org/apache/sling/models/it/exporter/BaseRequestComponent.java
@@ -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;
+ }
+
+}
diff --git a/src/main/java/org/apache/sling/models/it/exporter/ExporterTest.java b/src/main/java/org/apache/sling/models/it/exporter/ExporterTest.java
index 5df67d4..4f04db1 100644
--- a/src/main/java/org/apache/sling/models/it/exporter/ExporterTest.java
+++ b/src/main/java/org/apache/sling/models/it/exporter/ExporterTest.java
@@ -57,6 +57,9 @@
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 @@
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 @@
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 @@
}
@Test
- public void testServlets() throws Exception {
+ public void testResourceServlets() throws Exception {
ResourceResolver resolver = null;
try {
resolver = rrFactory.getAdministrativeResourceResolver(null);
@@ -169,12 +184,14 @@
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 {
@@ -185,6 +202,38 @@
}
@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();
+ }
+ }
+ }
+
+ @Test
public void testFailedExport() throws Exception {
boolean thrown = false;
try {
diff --git a/src/main/java/org/apache/sling/models/it/exporter/ExtendedRequestComponent.java b/src/main/java/org/apache/sling/models/it/exporter/ExtendedRequestComponent.java
new file mode 100644
index 0000000..5a37678
--- /dev/null
+++ b/src/main/java/org/apache/sling/models/it/exporter/ExtendedRequestComponent.java
@@ -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;
+ }
+}
diff --git a/src/main/java/org/apache/sling/models/it/exporter/RequestComponentImpl.java b/src/main/java/org/apache/sling/models/it/exporter/RequestComponentImpl.java
new file mode 100644
index 0000000..7872b75
--- /dev/null
+++ b/src/main/java/org/apache/sling/models/it/exporter/RequestComponentImpl.java
@@ -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;
+ }
+}