avatar
custom OSGi configuration in Apache Felix console AEM

> Let configure a custom OSGi configuration and make an adjustment in Apache Felix Console

package com.flagtick.core.services;

import org.osgi.service.metatype.annotations.AttributeDefinition;
import org.osgi.service.metatype.annotations.AttributeType;
import org.osgi.service.metatype.annotations.ObjectClassDefinition;
import org.osgi.service.metatype.annotations.Option;

@ObjectClassDefinition(
        name = "Flagtick HTTP Configuration",
        description = "This configuration reads the values to make an HTTP call to a JSON Webservice"
)
public @interface FlagtickHttpConfiguration {
    /**
     * This is a checkbox property which will indicate of the configuration is
     * executed or not
     * @return {@link Boolean}
     */
    @AttributeDefinition(
            name = "Enable config",
            description = "This property indicates whether the configuration values will taken into account or not",
            type = AttributeType.BOOLEAN)
    public boolean enableConfig();

    /**
     * This method returns the protocol that is being used
     * @return Protocol
     */
    @AttributeDefinition(
            name = "Protocol",
            description = "Choose Protocol",
            options = {
                    @Option(label = "HTTP", value = "http"), @Option(label = "HTTPS", value = "https") })
    public String getProtocol();

    /**
     * Returns the server
     * @return {@link String}
     */
    @AttributeDefinition(
            name = "Server",
            description = "Enter the server name")
    public String getServer();

    /**
     * Returns the endpoint
     * @return {@link String}
     */
    @AttributeDefinition(
            name = "Endpoint",
            description = "Enter the endpoint")
    public String getEndpoint();
}

> Create custom service was that used configuration as above

package com.flagtick.core.services;

public interface FlagtickHttpService {
    public String generateBaseURL();
}

> Implement Services as in class bases and define all declaration

package com.flagtick.core.services;

import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.metatype.annotations.Designate;

@Component(service = FlagtickHttpService.class, immediate = true)
@Designate(ocd = FlagtickHttpConfiguration.class)
public class FlagtickHttpServiceImpl implements FlagtickHttpService {

    private FlagtickHttpConfiguration config;

    @Activate
    protected void activate(FlagtickHttpConfiguration configuration) {
        this.config = configuration;
    }

    @Override
    public String generateBaseURL() {
        try {
            boolean enable = config.enableConfig();
            String protocol = config.getProtocol();
            String server = config.getServer();
            String endpoint = config.getEndpoint();
            String url = protocol + "://" + server + "/" + endpoint;
            if (enable) {
                return url;
            } else {
                return "Something went wrong";
            }
        } catch (Exception e) {
            return "Something went wrong: " + e.getMessage();
        }
    }
}

> Implement FlagtickHttpServlet as below:

package com.flagtick.core.services;


import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.Servlet;

@Component(service = Servlet.class, property = { Constants.SERVICE_DESCRIPTION + "=HTTP servlet",
        "sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.paths=" + "/bin/flagtick/httpcall" })
public class FlagtickHttpServlet extends SlingAllMethodsServlet {

    private static final long serialVersionUID = -2014397651676211439L;
    private static final Logger log = LoggerFactory.getLogger(FlagtickHttpServlet.class);

    @Reference
    private FlagtickHttpService flagtickHttpService;

    @Override
    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
        try {
            String jsonResponse = flagtickHttpService.generateBaseURL();
            response.getWriter().println(jsonResponse);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}

> Go to http://localhost:4502/system/console/configMgr and search Flagtick HTTP

> Run URL: http://localhost:4502/bin/flagtick/httpcall

24
Creating your custom OSGi Configuration
You need to login to do this manipulation!