/**
* Copyright (c) Codice Foundation
* <p>
* This is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser
* General Public License as published by the Free Software Foundation, either version 3 of the
* License, or any later version.
* <p>
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. A copy of the GNU Lesser General Public License
* is distributed along with this program and can be found at
* <http://www.gnu.org/licenses/lgpl.html>.
*/
package org.codice.ddf.security.handler.guest.configuration;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import javax.activation.MimeType;
import javax.activation.MimeTypeParseException;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import org.codice.ddf.branding.BrandingRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;
/**
* Stores external configuration properties.
*
* @author ddf.isgs@lmco.com
*/
@Path("/")
public class Configuration {
private static final Logger LOGGER = LoggerFactory.getLogger(Configuration.class);
private static Configuration uniqueInstance;
private static MimeType mimeType = null;
private String header = "";
private String footer = "";
private String style = "";
private String textColor = "";
private Optional<BrandingRegistry> branding = Optional.empty();
private Configuration() {
}
/**
* @return a unique instance of {@link Configuration}
*/
public static synchronized Configuration getInstance() {
if (uniqueInstance == null) {
uniqueInstance = new Configuration();
}
return uniqueInstance;
}
@GET
@Path("/config")
public Response getDocument(@Context UriInfo uriInfo, @Context HttpServletRequest httpRequest) {
Response response;
JSONObject configObj = new JSONObject();
configObj.put("text", header);
configObj.put("footer", footer);
configObj.put("style", style);
configObj.put("textColor", textColor);
configObj.put("branding", getProductName());
String configString = JSONValue.toJSONString(configObj);
response =
Response.ok(new ByteArrayInputStream(configString.getBytes(StandardCharsets.UTF_8)),
mimeType.toString())
.build();
return response;
}
public String getHeader() {
return header;
}
public void setHeader(String header) {
this.header = header;
}
public String getFooter() {
return footer;
}
public void setFooter(String footer) {
this.footer = footer;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public String getTextColor() {
return textColor;
}
public void setTextColor(String textColor) {
this.textColor = textColor;
}
public String getProductName() {
return branding.map(BrandingRegistry::getProductName)
.orElse("");
}
public BrandingRegistry getBranding() {
return branding.orElse(null);
}
public void setBranding(BrandingRegistry branding) {
this.branding = Optional.ofNullable(branding);
}
public void setMimeType(String mimeType) {
MimeType mime = null;
try {
mime = new MimeType(mimeType);
} catch (MimeTypeParseException e) {
LOGGER.info("Failed to create mimetype: {}.", mimeType);
}
this.mimeType = mime;
}
}