wttech / AEM-Rules-for-SonarQube

SonarQube plugin with set of rules detecting possible bugs and bad smells specific for AEM development.
Apache License 2.0
112 stars 51 forks source link

Make AEM-8 allow annotations from org.osgi.service.component.annotations #79

Closed toniedzwiedz closed 6 years ago

toniedzwiedz commented 6 years ago

AEM-8 pertains to the use of SCR annotations. When the @Component and @Property annotations are used to describe a Sling Servlet, the rule recommends the use od @SlingServlet.

In our case, the rule tells us to replace the new Declarative Services annotations with @SlingServlet, which we don't even keep on the classpath as we've migrated from the SCR annotations.

Violating code:

package com.foo.bar.core.components.page;

import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.wcm.api.NameConstants;
import com.google.common.net.MediaType;
import java.io.IOException;
import javax.servlet.Servlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.service.component.annotations.Component;

@Component(
  service = Servlet.class,
  property = {
    "sling.servlet.extensions=json",
    "sling.servlet.selectors=details",
    "sling.servlet.resourceTypes=" + NameConstants.NT_PAGE
  }
)
public class PageDetailsServlet extends SlingAllMethodsServlet {

  @Override
  protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
    throws IOException {
    final Resource pageContentResource = request.getResource().getChild(JcrConstants.JCR_CONTENT);
    final PageModel page = ResourceUtil.adaptTo(pageContentResource, PageModel.class);

    response.setContentType(MediaType.JSON_UTF_8.toString());
    response.getWriter().write(JsonUtils.toJson(page));
  }

} 
toniedzwiedz commented 6 years ago

This duplicates #72 I need to upgrade the plugin to the newest version on the customer's SonarQube server