View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2022 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package io.wcm.siteapi.processor.impl.content;
21  
22  import static com.adobe.cq.export.json.ExporterConstants.SLING_MODEL_EXTENSION;
23  import static com.adobe.cq.export.json.ExporterConstants.SLING_MODEL_SELECTOR;
24  import static io.wcm.siteapi.processor.ProcessorConstants.PROCESSOR_CONTENT;
25  import static io.wcm.siteapi.processor.ProcessorConstants.PROPERTY_SUFFIX;
26  
27  import java.io.IOException;
28  
29  import javax.servlet.RequestDispatcher;
30  import javax.servlet.ServletException;
31  import javax.servlet.http.HttpServletResponse;
32  
33  import org.apache.sling.api.SlingHttpServletResponse;
34  import org.jetbrains.annotations.NotNull;
35  import org.osgi.service.component.annotations.Component;
36  import org.osgi.service.component.annotations.ConfigurationPolicy;
37  import org.osgi.service.component.propertytypes.ServiceRanking;
38  import org.osgi.service.metatype.annotations.AttributeDefinition;
39  import org.osgi.service.metatype.annotations.Designate;
40  import org.osgi.service.metatype.annotations.ObjectClassDefinition;
41  
42  import io.wcm.siteapi.processor.Processor;
43  import io.wcm.siteapi.processor.ProcessorRequestContext;
44  import io.wcm.siteapi.processor.SlingHttpServletProcessor;
45  
46  /**
47   * Page content.
48   * Forwards request to "model.json" version view of the page.
49   */
50  @Designate(ocd = ContentProcessor.Config.class)
51  @Component(service = Processor.class, configurationPolicy = ConfigurationPolicy.REQUIRE,
52      property = PROPERTY_SUFFIX + "=" + PROCESSOR_CONTENT)
53  @ServiceRanking(-500)
54  public class ContentProcessor implements SlingHttpServletProcessor {
55  
56    @ObjectClassDefinition(
57        name = "wcm.io Site API Content Processor",
58        description = "Provides model.json content of a page.")
59    @interface Config {
60  
61      @AttributeDefinition(
62          name = "Enabled",
63          description = "Processor is enabled.")
64      boolean enabled() default false;
65  
66    }
67  
68    @Override
69    public void process(@NotNull ProcessorRequestContext context, @NotNull SlingHttpServletResponse response)
70        throws ServletException, IOException {
71      String modelJsonUri = context.getPage().getPath() + "." + SLING_MODEL_SELECTOR + "." + SLING_MODEL_EXTENSION;
72      RequestDispatcher requestDispatcher = context.getRequest().getRequestDispatcher(modelJsonUri);
73      if (requestDispatcher != null) {
74        requestDispatcher.forward(context.getRequest(), response);
75      }
76      else {
77        response.sendError(HttpServletResponse.SC_NOT_FOUND);
78      }
79    }
80  
81  }