package org.gcube.gcat.rest; import java.util.ArrayList; import java.util.List; import javax.cache.Cache; import javax.ws.rs.DefaultValue; import javax.ws.rs.GET; import javax.ws.rs.HeaderParam; import javax.ws.rs.InternalServerErrorException; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.datacatalogue.metadatadiscovery.DataCalogueMetadataFormatReader; import org.gcube.datacatalogue.metadatadiscovery.bean.MetadataProfile; import org.gcube.gcat.ResourceInitializer; import org.gcube.gcat.oldutils.CachesManager; import org.json.JSONObject; import org.json.XML; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; /** * @author Luca Frosini (ISTI - CNR) */ @Path(BaseREST.PROFILES) public class Profile extends BaseREST { public static final String PROFILE_NAME_PARAMETER = "PROFILE_NAME"; public static DataCalogueMetadataFormatReader getDataCalogueMetadataFormatReader() throws Exception { Cache readerCache = CachesManager.getReaderCache(); String context = ScopeProvider.instance.get(); DataCalogueMetadataFormatReader reader; if(readerCache.containsKey(context)) reader = (DataCalogueMetadataFormatReader) readerCache.get(context); else { reader = new DataCalogueMetadataFormatReader(); readerCache.put(context, reader); } return reader; } /** * Returns the names of the metadata profiles in a given context * @param context * @return * @throws Exception */ public static List getProfilesNames() throws Exception { DataCalogueMetadataFormatReader reader = getDataCalogueMetadataFormatReader(); List toReturn = new ArrayList(); List listProfiles = reader.getListOfMetadataProfiles(); if(listProfiles != null && !listProfiles.isEmpty()) { for(MetadataProfile profile : listProfiles) { toReturn.add(profile.getName()); } } return toReturn; } /** * Returns the source xml of the metadata profile (specified via name) in a given context * @param context * @return * @throws Exception */ public static String getProfileSource(String profileName) throws Exception { DataCalogueMetadataFormatReader reader = Profile.getDataCalogueMetadataFormatReader(); List listProfiles = reader.getListOfMetadataProfiles(); String xmlToReturn = null; if(listProfiles != null && !listProfiles.isEmpty()) { for(MetadataProfile profile : listProfiles) { if(profile.getName().equals(profileName)) { xmlToReturn = reader.getMetadataFormatForMetadataProfile(profile).getMetadataSource(); break; } } } return xmlToReturn; } @GET @Produces(ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8) public String list() { setCalledMethod("GET /" + BaseREST.PROFILES); ObjectMapper mapper = new ObjectMapper(); ArrayNode arrayNode = mapper.createArrayNode(); try { List names = getProfilesNames(); for(String name : names) { arrayNode.add(name); } return mapper.writeValueAsString(arrayNode); } catch(Exception e) { throw new InternalServerErrorException(e.getMessage()); } } public static int PRETTY_PRINT_INDENT_FACTOR = 4; @GET @Path("/{" + PROFILE_NAME_PARAMETER + "}") @Produces({MediaType.APPLICATION_XML, ResourceInitializer.APPLICATION_JSON_CHARSET_UTF_8}) public String read(@PathParam(PROFILE_NAME_PARAMETER) String id, @DefaultValue(MediaType.APPLICATION_JSON) @HeaderParam("Accept") String accept) { setCalledMethod("GET /" + BaseREST.PROFILES + "/{" + PROFILE_NAME_PARAMETER + "}"); try { String profile = Profile.getProfileSource(id); if(accept.startsWith(MediaType.APPLICATION_XML)){ return profile; }else { JSONObject xmlJSONObj = XML.toJSONObject(profile); String jsonString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR); return jsonString; } } catch(Exception e) { throw new InternalServerErrorException(e.getMessage()); } } }