Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add FeatureService to v2 APIs #4808

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// Copyright Red Hat, Inc.
//
// SPDX-License-Identifier: GPL-2.0-or-later
//
package org.dogtagpki.server.ca.rest.v2;

import javax.servlet.annotation.WebServlet;

import org.dogtagpki.server.rest.v2.FeatureServlet;

/**
* @author Marco Fargetta {@literal <[email protected]>}
*/
@WebServlet(
name = "caFeature",
urlPatterns = "/v2/config/features/*")
public class CAFeatureServlet extends FeatureServlet {
private static final long serialVersionUID = 1L;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import org.dogtagpki.server.rest.v2.filters.ACLFilter;

@WebFilter(servletNames = {"caInfo", "caCert", "caCertRequest", "caJobs"})
@WebFilter(servletNames = {"caInfo", "caCert", "caCertRequest", "caJobs", "caFeature"})
public class EmptyACL extends ACLFilter {

private static final long serialVersionUID = 1L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import org.dogtagpki.server.rest.v2.filters.AuthMethodFilter;

@WebFilter(servletNames = {"caInfo", "caCert", "caCertRequest", "caJobs"})
@WebFilter(servletNames = {"caInfo", "caCert", "caCertRequest", "caJobs", "caFeature"})
public class EmptyAuthMethod extends AuthMethodFilter {

private static final long serialVersionUID = 1L;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//
// Copyright Red Hat, Inc.
//
// SPDX-License-Identifier: GPL-2.0-or-later
//
package org.dogtagpki.server.rest.v2;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.netscape.certsrv.base.ResourceNotFoundException;
import com.netscape.certsrv.base.WebAction;
import com.netscape.certsrv.system.Feature;
import com.netscape.cmscore.apps.CMSEngine;
import com.netscape.cmscore.apps.EngineConfig;
import com.netscape.cmscore.base.ConfigStore;

/**
* @author Marco Fargetta {@literal <[email protected]>}
* @author alee
*/
public class FeatureServlet extends PKIServlet {
private static final long serialVersionUID = 1L;
public static final Logger logger = LoggerFactory.getLogger(FeatureServlet.class);

@WebAction(method = HttpMethod.GET, paths = {""})
public void listFeatures(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("FeatureServlet.listFeatures(): session: {}", session.getId());

CMSEngine engine = getEngine();
EngineConfig config = engine.getConfig();

ConfigStore cs = config.getSubStore("features", ConfigStore.class);
ArrayList<Feature> features = new ArrayList<>();
Enumeration<String> tags = cs.getSubStoreNames().elements();
while (tags.hasMoreElements()) {
String tag = tags.nextElement();
Feature feature = createFeature(cs, tag);
features.add(feature);
}
ObjectMapper mapper = new ObjectMapper();
PrintWriter out = response.getWriter();
out.println(mapper.writeValueAsString(features));
}

@WebAction(method = HttpMethod.GET, paths = {"{}"})
public void getFeature(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpSession session = request.getSession();
logger.debug("FeatureServlet.getFeature(): session: {}", session.getId());
String[] pathElement = request.getPathInfo().substring(1).split("/");
String featureId = pathElement[0];

CMSEngine engine = getEngine();
EngineConfig config = engine.getConfig();
ConfigStore cs = config.getSubStore("features", ConfigStore.class);
Enumeration<String> tags = cs.getSubStoreNames().elements();
while(tags.hasMoreElements()) {
String tag = tags.nextElement();
if (tag.equals(featureId)) {
Feature feature = createFeature(cs, tag);
PrintWriter out = response.getWriter();
out.println(feature.toJSON());
return;
}
}
throw new ResourceNotFoundException("Feature " + featureId + " not supported");
}

private Feature createFeature(ConfigStore cs, String tag) {
Map<String, String> props = cs.getSubStore(tag).getProperties();
Feature feature = new Feature();
feature.setId(tag);
feature.setEnabled(Boolean.parseBoolean(props.get("enabled")));
feature.setDescription(props.get("description"));
feature.setVersion(props.get("version"));
return feature;
}
}
Loading