View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.lod;
20  
21  import java.io.IOException;
22  import java.io.StringReader;
23  import java.io.StringWriter;
24  import java.net.URI;
25  import java.util.Collections;
26  import java.util.List;
27  import java.util.stream.Stream;
28  
29  import org.apache.logging.log4j.LogManager;
30  import org.eclipse.rdf4j.rio.RDFFormat;
31  import org.eclipse.rdf4j.rio.RDFHandlerException;
32  import org.eclipse.rdf4j.rio.RDFParseException;
33  import org.eclipse.rdf4j.rio.RDFParser;
34  import org.eclipse.rdf4j.rio.RDFWriter;
35  import org.eclipse.rdf4j.rio.Rio;
36  import org.glassfish.jersey.server.ResourceConfig;
37  import org.glassfish.jersey.server.ServerProperties;
38  import org.mycore.common.config.MCRConfiguration2;
39  import org.mycore.frontend.jersey.access.MCRRequestScopeACLFilter;
40  import org.mycore.restapi.MCRCORSResponseFilter;
41  import org.mycore.restapi.MCRIgnoreClientAbortInterceptor;
42  import org.mycore.restapi.MCRSessionFilter;
43  import org.mycore.restapi.MCRTransactionFilter;
44  import org.mycore.restapi.converter.MCRWrappedXMLWriter;
45  
46  import io.swagger.v3.jaxrs2.integration.resources.OpenApiResource;
47  import jakarta.ws.rs.ApplicationPath;
48  import jakarta.ws.rs.core.Response;
49  
50  /**
51   * Basic configuration for the MyCoRe Linked Open Data Endpoint
52   * 
53   * @author Robert Stephan
54   */
55  @ApplicationPath("/open-data")
56  public class MCRJerseyLodApp extends ResourceConfig {
57  
58      //RDFXML is the default/fallback format an does not have to be on this list
59      private static List<RDFFormat> RDF_OUTPUT_FORMATS = List.of(RDFFormat.TURTLE, RDFFormat.JSONLD);
60  
61      /**
62       * Constructor
63       */
64      public MCRJerseyLodApp() {
65          super();
66          initAppName();
67          property(ServerProperties.APPLICATION_NAME, getApplicationName());
68          packages(getRestPackages());
69          property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, true);
70          register(MCRSessionFilter.class);
71          register(MCRTransactionFilter.class);
72          register(MCRLodFeature.class);
73          register(MCRCORSResponseFilter.class);
74          register(MCRRequestScopeACLFilter.class);
75          register(MCRIgnoreClientAbortInterceptor.class);
76      }
77  
78      /**
79       * read name for the Jersey App from properties or generate a default one
80       */
81      protected void initAppName() {
82          setApplicationName(MCRConfiguration2.getString("MCR.NameOfProject").orElse("MyCoRe") + " LOD-Endpoint");
83          LogManager.getLogger().info("Initiialize {}", getApplicationName());
84      }
85  
86      /**
87       * read packages with Rest controllers and configuration
88       * @return an array of package names
89       */
90      protected String[] getRestPackages() {
91          return Stream
92              .concat(
93                  Stream.of(MCRWrappedXMLWriter.class.getPackage().getName(),
94                      OpenApiResource.class.getPackage().getName()),
95                  MCRConfiguration2.getOrThrow("MCR.LOD.Resource.Packages", MCRConfiguration2::splitValue))
96              .toArray(String[]::new);
97      }
98  
99      /**
100      * create a Response object that contains the linked data in the given format
101      * 
102      * @param rdfxmlString - the linked data as String in RDFXML format
103      * @param uri - the base URI of the document
104      * @param mimeTypes - the mime types, sent with the request
105      * @return the Jersey Response with the requested Linked Data format
106      */
107     public static Response returnLinkedData(String rdfxmlString, URI uri, List<String> mimeTypes) {
108         try {
109             for (RDFFormat rdfOutFormat : RDF_OUTPUT_FORMATS) {
110                 if (!Collections.disjoint(mimeTypes, rdfOutFormat.getMIMETypes())) {
111                     RDFParser rdfParser = Rio.createParser(RDFFormat.RDFXML);
112                     StringWriter sw = new StringWriter();
113                     RDFWriter rdfWriter = Rio.createWriter(rdfOutFormat, sw);
114                     rdfParser.setRDFHandler(rdfWriter);
115                     rdfParser.parse(new StringReader(rdfxmlString), uri.toString());
116 
117                     return Response.ok(sw.toString()).type(rdfOutFormat.getDefaultMIMEType() + ";charset=UTF-8")
118                         .build();
119                 }
120             }
121         } catch (IOException | RDFParseException | RDFHandlerException e) {
122             // do nothing
123         }
124         //fallback, default: RDFFormat.RDFXML
125         return Response.ok(rdfxmlString, RDFFormat.RDFXML.getDefaultMIMEType() + ";charset=UTF-8")
126             .build();
127     }
128 }