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 API endpoint to retrieve configuration properties #385

Closed
wants to merge 1 commit into from
Closed
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
Expand Up @@ -7,9 +7,13 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openas2.cert.AliasedCertificateFactory;
import org.openas2.cmd.CommandResult;
import org.openas2.cmd.processor.RestCommandProcessor;
import org.openas2.util.Properties;

import javax.annotation.security.RolesAllowed;
import jakarta.ws.rs.Consumes;
Expand Down Expand Up @@ -38,12 +42,7 @@
import java.io.ByteArrayInputStream;
import java.security.cert.Certificate;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -53,6 +52,7 @@
@Path("api")
public class ApiResource {

private final Log logger = LogFactory.getLog(ApiResource.class.getSimpleName());
/**
* @return the processor
*/
Expand All @@ -73,9 +73,9 @@ public static void setProcessor(RestCommandProcessor aProcessor) {
@Context
Request request;
private final ObjectMapper mapper;

public ApiResource() {

mapper = new ObjectMapper();
// enable pretty printing
mapper.enable(SerializationFeature.INDENT_OUTPUT);
Expand Down Expand Up @@ -249,4 +249,25 @@ private CommandResult importCertificateByStream(String itemId, MultivaluedMap<St
}
}

@GET
@RolesAllowed({"ADMIN"})
@Path("/configuration/properties")
@Produces(MediaType.APPLICATION_JSON)
public Response getPropertyList() {
Map<String, String> result = new HashMap<>();
result = (Map<String, String>) Properties.getProperties();

ObjectMapper om = new ObjectMapper();
try {
String js = om.writeValueAsString(result);
return Response.ok(js, MediaType.APPLICATION_JSON).build();
} catch (JsonProcessingException e) {
logger.error(e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("error")
.type(MediaType.APPLICATION_JSON)
.build();
}
}

}
58 changes: 58 additions & 0 deletions Server/src/test/java/org/openas2/ApiResourceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.openas2;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.MockedStatic;
import org.openas2.cmd.processor.restapi.ApiResource;
import org.openas2.logging.Logger;
import org.openas2.util.Properties;

import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;

public class ApiResourceTest {

@Mock
private Logger logger;

@Mock
private ObjectMapper objectMapper;

@InjectMocks
private ApiResource apiResource;

private Client client;

@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);

// Set up the client with basic authentication
HttpAuthenticationFeature authFeature = HttpAuthenticationFeature.basicBuilder()
.credentials("userID", "pWd")
.build();

client = ClientBuilder.newClient().register(authFeature);
}

@Test
public void testGetProperties() throws Exception {
Response response = apiResource.getPropertyList();
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());

}
}
Loading