CatalogService.java [src/csip] Revision: 54532d51c31186ee4a25258c2c3c643366480666 Date: Mon Apr 10 12:02:14 MDT 2017
/*
* $Id$
*
* This file is part of the Cloud Services Integration Platform (CSIP),
* a Model-as-a-Service framework, API and application suite.
*
* 2012-2017, Olaf David and others, OMSLab, Colorado State University.
*
* OMSLab licenses this file to you under the MIT license.
* See the LICENSE file in the project root for more information.
*/
package csip;
import csip.Config.Registry;
import csip.utils.JSONUtils;
import csip.utils.Services;
import csip.utils.SimpleCache;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
/**
* Catalog service
*
* @author wlloyd,od
*/
@Path("")
public class CatalogService {
static final Logger LOG = Logger.getLogger(CatalogService.class.getName());
static SimpleCache<Registry, String> cat = new SimpleCache<>();
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getTextCatalog(@Context UriInfo uriInfo) {
return getJSONCatalog(uriInfo);
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getJSONCatalog(@Context UriInfo uriInfo) {
LOG.log(Level.INFO, "HTTP/GET {0}", uriInfo.getRequestUri().toString());
return cat.get(Config.registry(), (Registry r) -> {
JSONArray o = new JSONArray();
try {
String host = Services.toPublicURL(uriInfo.getRequestUri()).toString();
if (!host.endsWith("/")) {
host += "/";
}
for (Class<?> c : r.getServices()) {
JSONObject m = new JSONObject();
String url = host + r.getServicePath(c);
m.put(ModelDataService.KEY_NAME, r.getServiceName(c));
m.put(ModelDataService.KEY_DESC, r.getServiceDescription(c));
m.put(ModelDataService.KEY_URL, url);
o.put(m);
}
return o.toString(2).replace("\\/", "/");
} catch (JSONException ex) {
o.put(JSONUtils.error(ex.getMessage()));
Logger.getLogger(CatalogService.class.getName()).log(Level.SEVERE, null, ex);
}
return o.toString().replace("\\/", "/");
});
// JSONArray o = new JSONArray();
// try {
// Registry r = Config.registry();
// String host = Services.toPublicURL(uriInfo.getRequestUri()).toString();
// if (!host.endsWith("/")) {
// host += "/";
// }
// for (Class<?> c : r.getServices()) {
// JSONObject m = new JSONObject();
// String url = host + r.getServicePath(c);
// m.put(ModelDataService.KEY_NAME, r.getServiceName(c));
// m.put(ModelDataService.KEY_DESC, r.getServiceDescription(c));
// m.put(ModelDataService.KEY_URL, url);
// o.put(m);
// }
// return o.toString(2).replace("\\/", "/");
// } catch (JSONException ex) {
// o.put(JSONUtils.error(ex.getMessage()));
// }
// return o.toString().replace("\\/", "/");
}
}