Skip to content

Commit

Permalink
Some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
menzowindhouwer committed Nov 28, 2018
1 parent 9ab51f4 commit b98e3c7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 27 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ $ curl -XPOST -H 'Content-Type:application/graphql' -d 'query { terms(match:"Ab
* [x] docker setup
* [ ] keep the languages
* [ ] query multiple datasets and merge the results
* [ ] fuller support for the NDE API design
* [ ] ...
38 changes: 19 additions & 19 deletions src/main/java/nl/knaw/huc/di/nde/Registry.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public class Registry {
private final GraphQL.Builder builder;

final static public Map<String,String> NAMESPACES = new LinkedHashMap<>();

static {
NAMESPACES.put("dc", "http://purl.org/dc/elements/1.1/");
NAMESPACES.put("dcterms", "http://purl.org/dc/terms/");
Expand Down Expand Up @@ -85,7 +84,8 @@ public List<TermDTO> get(DataFetchingEnvironment environment) {
builder = GraphQL.newGraphQL(schemaGenerator.makeExecutableSchema(typeRegistry, runtime));
}

private List<TermDTO> fetchMatchingTerms(String match,String dataset) {
private List<TermDTO> fetchMatchingTerms(String match,String dataset) {
List<TermDTO> res = new ArrayList<>();
try {
System.err.println("DBG: config["+System.getProperty("nde.config")+"]");
System.err.println("DBG: match["+match+"]");
Expand All @@ -94,23 +94,30 @@ private List<TermDTO> fetchMatchingTerms(String match,String dataset) {
Map vars = new HashMap();
vars.put("dataset", new XdmAtomicValue(dataset));
XdmItem dsConfig = Saxon.xpathSingle(config, "//nde:dataset[@id=$dataset]", vars, NAMESPACES);
String recipe = Saxon.xpath2string(dsConfig, "@recipe", null, NAMESPACES);
Class<RecipeInterface> clazz = (Class<RecipeInterface>) Class.forName(recipe);
RecipeInterface recipeImpl = clazz.newInstance();
return recipeImpl.fetchMatchingTerms(dsConfig, match);
if (dsConfig != null) {
String recipe = Saxon.xpath2string(dsConfig, "@recipe", null, NAMESPACES);
if (!recipe.isEmpty()) {
Class<RecipeInterface> clazz = (Class<RecipeInterface>) Class.forName(recipe);
RecipeInterface recipeImpl = clazz.newInstance();
res = recipeImpl.fetchMatchingTerms(dsConfig, match);
} else
Logger.getLogger(Registry.class.getName()).log(Level.SEVERE,"Recipe of dataset["+dataset+"] is unknown!");
} else
Logger.getLogger(Registry.class.getName()).log(Level.SEVERE,"Unknown dataset["+dataset+"]!");
} catch (Exception ex) {
Logger.getLogger(Registry.class.getName()).log(Level.SEVERE, null, ex);
}
return new ArrayList<TermDTO>();
return res;
}

// GraphQL

@POST
@Consumes("application/json")
public Response postJson(JsonNode body, @QueryParam("query") String query,
@HeaderParam("accept") String acceptHeader,
@QueryParam("accept") String acceptParam,
@HeaderParam("Authorization") String authHeader) {
System.err.println("DBG: postJson");
final String queryFromBody;
if (body.has("query")) {
queryFromBody = body.get("query").asText();
Expand Down Expand Up @@ -140,15 +147,13 @@ public Response postGraphql(String query, @QueryParam("query") String queryParam
@HeaderParam("accept") String acceptHeader,
@QueryParam("accept") String acceptParam,
@HeaderParam("Authorization") String authHeader) {
System.err.println("DBG: postGraphgl");
return executeGraphql(queryParam, acceptHeader, acceptParam, query, null, null, authHeader);
}

@GET
public Response get(@QueryParam("query") String query, @HeaderParam("accept") String acceptHeader,
@QueryParam("accept") String acceptParam,
@HeaderParam("Authorization") String authHeader) {
System.err.println("DBG: get");
return executeGraphql(null, acceptHeader, acceptParam, query, null, null, authHeader);
}

Expand Down Expand Up @@ -179,7 +184,6 @@ public Response executeGraphql(String query, String acceptHeader, String acceptP
"E.g. {query: \\\"{\\n persons {\\n ... \\\"}\"]}")
.build();
}


GraphQL graphQl = builder.build();

Expand All @@ -190,22 +194,18 @@ public Response executeGraphql(String query, String acceptHeader, String acceptP
.operationName(operationName)
.variables(variables == null ? Collections.emptyMap() : variables)
.build());
System.err.println("DBG: result["+result+"]");
return Response
.ok()
.type(MediaType.APPLICATION_JSON_TYPE)
.entity(result.toSpecification())
.build();
} catch (GraphQLException e) {
System.err.println("ERR: "+e.getMessage());
return Response.status(500).entity(e.getMessage()).build();
// throw e;
}
}

}

public boolean unSpecifiedAcceptHeader(@HeaderParam("accept") String acceptHeader) {
return acceptHeader == null || acceptHeader.isEmpty() || "*/*".equals(acceptHeader);
}

public boolean unSpecifiedAcceptHeader(@HeaderParam("accept") String acceptHeader) {
return acceptHeader == null || acceptHeader.isEmpty() || "*/*".equals(acceptHeader);
}
}
4 changes: 0 additions & 4 deletions src/main/java/nl/knaw/huc/di/nde/recipe/OpenSKOS.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
import nl.knaw.huc.di.nde.TermDTO;
import nl.mpi.tla.util.Saxon;

/**
*
* @author menzowi
*/
public class OpenSKOS implements RecipeInterface {

final static public Map<String,String> NAMESPACES = new LinkedHashMap<>();
Expand Down
4 changes: 0 additions & 4 deletions src/main/java/nl/knaw/huc/di/nde/recipe/RecipeInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
import net.sf.saxon.s9api.XdmItem;
import nl.knaw.huc.di.nde.TermDTO;

/**
*
* @author menzowi
*/
public interface RecipeInterface {

List<TermDTO> fetchMatchingTerms(XdmItem config, String match);
Expand Down

0 comments on commit b98e3c7

Please sign in to comment.