Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

unable to use OSGi @Component if I'm using import com.day.cq.wcm.api.components.Component

Avatar

Level 3

Hi All,

I'm migrating from felix to OSGI annotations. In one of my servlet I'm trying to use @Component (OSGI annoation) instead of @Slingservlet. But it's throwing an error The import com.day.cq.wcm.api.components.Component collides with another import statement.  In my servlet I have already used wcm api component , so now i'm unable to use OSGI @Component. Below is my sample code. Can any one help me to fix this? Appreciate your help.

import com.day.cq.wcm.api.components.Component;

import com.day.cq.wcm.api.components.ComponentManager;

import com.google.common.collect.ImmutableMap;

import com.google.gson.Gson;

import java.io.IOException;

import java.time.ZonedDateTime;

import java.time.format.DateTimeFormatter;

import java.util.Arrays;

import java.util.GregorianCalendar;

import java.util.Map;

import java.util.stream.Collectors;

import javax.annotation.Nonnull;

import javax.servlet.Servlet;

import javax.servlet.ServletException;

import org.apache.commons.lang3.StringUtils;

import org.apache.commons.lang3.tuple.Pair;

import org.apache.felix.scr.annotations.sling.SlingServlet;

import org.apache.sling.api.SlingHttpServletRequest;

import org.apache.sling.api.SlingHttpServletResponse;

import org.apache.sling.api.resource.ValueMap;

import org.apache.sling.api.servlets.SlingSafeMethodsServlet;

import org.osgi.service.component.annotations.Component;

@Component(service = {Servlet.class},

property = {

"sling.servlet.resourceTypes=" + "sling/servlet/default",

"sling.servlet.selectors=" + "referencepreview",

"sling.servlet.methods=" + "GET",

"sling.servlet.extensions=" + "json"

})

public class ReferencePreviewServlet extends SlingSafeMethodsServlet {

private static final DateTimeFormatter DATE_TIME_FORMATTER =

DateTimeFormatter.ofPattern("dd.MM.YYYY HH:mm");

@Override

protected void doGet(@Nonnull SlingHttpServletRequest request,

@Nonnull SlingHttpServletResponse response) throws ServletException, IOException {

response.setContentType(FileType.JSON.getMimeType());

ComponentManager componentManager =

request.getResourceResolver().adaptTo(ComponentManager.class);

@SuppressWarnings({"ComponentManager can not be null", "squid:S2259", "ConstantConditions"})

    Component component = componentManager.getComponentOfResource(request.getResource());

if (component == null) {

new Gson().toJson(ImmutableMap.of("renderable", false), response.getWriter());

return;

    }

ValueMap properties = request.getResource().getValueMap();

Map<String, String> propertyMap = properties.keySet().stream()

.filter(this::excludeSystemProperties).map(key -> Pair.of(key, properties.get(key)))

.map(this::getFormattedPair).collect(Collectors.toMap(Pair::getKey, Pair::getValue));

new Gson()

.toJson(

ImmutableMap.of("renderable", true, "name",

StringUtils.defaultString(component.getTitle()), "properties", propertyMap),

response.getWriter());

  }

Thanks,

Vijay

Feike Vissersmacdonald2008Arun Patidar

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

remove import com.day.cq.wcm.api.components.Component and change your line of code like below:

com.day.cq.wcm.api.components.Component component = componentManager.getComponentOfResource(request.getResource());

Thanks

Arun

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

Hi,

remove import com.day.cq.wcm.api.components.Component and change your line of code like below:

com.day.cq.wcm.api.components.Component component = componentManager.getComponentOfResource(request.getResource());

Thanks

Arun