1414import java .io .FileReader ;
1515import java .io .IOException ;
1616import java .net .URISyntaxException ;
17- import java .net .URL ;
1817import java .util .ArrayList ;
1918import java .util .Collection ;
2019import java .util .HashMap ;
4645 * @author Bart Hanssens
4746 */
4847public class TriplePatternFragmentServlet extends HttpServlet {
48+
4949 private final static long serialVersionUID = 1L ;
5050
5151 // Parameters
@@ -55,17 +55,15 @@ public class TriplePatternFragmentServlet extends HttpServlet {
5555 public final static String OBJ = "object" ;
5656 public final static String PAGE = "page" ;
5757
58-
59- private final static Pattern STRINGPATTERN =
60- Pattern .compile ("^\" (.*)\" (?:@(.*)|\\ ^\\ ^<?([^<>]*)>?)?$" );
58+ private final static Pattern STRINGPATTERN
59+ = Pattern .compile ("^\" (.*)\" (?:@(.*)|\\ ^\\ ^<?([^<>]*)>?)?$" );
6160 private final static TypeMapper TYPES = TypeMapper .getInstance ();
6261 private final static long TRIPLESPERPAGE = 100 ;
6362
6463 private ConfigReader config ;
6564 private final HashMap <String , IDataSource > dataSources = new HashMap <>();
6665 private final Collection <String > mimeTypes = new ArrayList <>();
6766
68-
6967 private File getConfigFile (ServletConfig config ) throws IOException {
7068 String path = config .getServletContext ().getRealPath ("/" );
7169 if (path == null ) {
@@ -96,11 +94,11 @@ public void init(ServletConfig servletConfig) throws ServletException {
9694 dataSources .put (dataSource .getKey (), DataSourceFactory .create (dataSource .getValue ()));
9795 }
9896
99- // register content types
97+ // register content types
10098 mimeTypes .add (Lang .TTL .getHeaderString ());
10199 mimeTypes .add (Lang .JSONLD .getHeaderString ());
102100 mimeTypes .add (Lang .NTRIPLES .getHeaderString ());
103- mimeTypes .add (Lang .RDFXML .getHeaderString () );
101+ mimeTypes .add (Lang .RDFXML .getHeaderString ());
104102 } catch (IOException | DataSourceException e ) {
105103 throw new ServletException (e );
106104 }
@@ -118,9 +116,9 @@ private IDataSource getDataSource(HttpServletRequest request) throws IOException
118116 String requestURI = request .getRequestURI ();
119117
120118 String path = contextPath == null
121- ? requestURI
122- : requestURI .substring (contextPath .length ());
123-
119+ ? requestURI
120+ : requestURI .substring (contextPath .length ());
121+
124122 if (path .equals ("/" ) || path .isEmpty ()) {
125123 return new IndexDataSource (getBaseUrl (request ), dataSources );
126124 }
@@ -142,11 +140,17 @@ private IDataSource getDataSource(HttpServletRequest request) throws IOException
142140 private String getDatasetUrl (HttpServletRequest request ) {
143141 return getBaseUrl (request ) + request .getRequestURI ();
144142 }
145-
143+
144+ /**
145+ * Get Base url
146+ *
147+ * @param request
148+ * @return
149+ */
146150 private String getBaseUrl (HttpServletRequest request ) {
147- if (config .getBaseURL () != null )
151+ if (config .getBaseURL () != null ) {
148152 return config .getBaseURL ();
149- else if ((request .getServerPort () == 80 )
153+ } else if ((request .getServerPort () == 80 )
150154 || (request .getServerPort () == 443 )) {
151155 return request .getScheme () + "://"
152156 + request .getServerName ();
@@ -165,7 +169,7 @@ else if ((request.getServerPort() == 80)
165169 * @param limit
166170 */
167171 private void addMeta (Model output , Resource datasetId , Resource fragmentId ,
168- long total , long limit ) {
172+ long total , long limit ) {
169173 output .add (datasetId , CommonResources .RDF_TYPE , CommonResources .VOID_DATASET );
170174 output .add (datasetId , CommonResources .RDF_TYPE , CommonResources .HYDRA_COLLECTION );
171175 output .add (datasetId , CommonResources .VOID_SUBSET , fragmentId );
@@ -181,7 +185,6 @@ private void addMeta(Model output, Resource datasetId, Resource fragmentId,
181185 output .add (fragmentId , CommonResources .HYDRA_ITEMSPERPAGE , limitTyped );
182186 }
183187
184-
185188 /**
186189 * Add reference to first/previous/next page
187190 *
@@ -195,21 +198,21 @@ private void addMeta(Model output, Resource datasetId, Resource fragmentId,
195198 * @throws URISyntaxException
196199 */
197200 private void addPages (Model output , Resource fragmentId , String fragmentUrl ,
198- long total , long limit , long offset , long page ) throws URISyntaxException {
201+ long total , long limit , long offset , long page ) throws URISyntaxException {
199202 URIBuilder pagedUrl = new URIBuilder (fragmentUrl );
200203
201204 pagedUrl .setParameter (PAGE , "1" );
202205 output .add (fragmentId , CommonResources .HYDRA_FIRSTPAGE ,
203- output .createResource (pagedUrl .toString ()));
206+ output .createResource (pagedUrl .toString ()));
204207 if (offset > 0 ) {
205208 pagedUrl .setParameter (PAGE , Long .toString (page - 1 ));
206209 output .add (fragmentId , CommonResources .HYDRA_PREVIOUSPAGE ,
207- output .createResource (pagedUrl .toString ()));
210+ output .createResource (pagedUrl .toString ()));
208211 }
209212 if (offset + limit < total ) {
210213 pagedUrl .setParameter (PAGE , Long .toString (page + 1 ));
211214 output .add (fragmentId , CommonResources .HYDRA_NEXTPAGE ,
212- output .createResource (pagedUrl .toString ()));
215+ output .createResource (pagedUrl .toString ()));
213216 }
214217 }
215218
@@ -243,7 +246,6 @@ private void addControls(Model output, Resource datasetId, String datasetUrl) {
243246 output .add (objectMapping , CommonResources .HYDRA_PROPERTY , CommonResources .RDF_OBJECT );
244247 }
245248
246-
247249 @ Override
248250 public void doGet (HttpServletRequest request , HttpServletResponse response ) throws ServletException {
249251 try {
@@ -258,8 +260,8 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
258260 long limit = TRIPLESPERPAGE ;
259261 long offset = limit * (page - 1 );
260262
261- TriplePatternFragment fragment =
262- dataSource .getFragment (subject , predicate , object , offset , limit );
263+ TriplePatternFragment fragment
264+ = dataSource .getFragment (subject , predicate , object , offset , limit );
263265
264266 // fill the output model
265267 Model output = fragment .getTriples ();
@@ -278,7 +280,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
278280 addMeta (output , datasetId , fragmentId , total , limit );
279281 addPages (output , fragmentId , fragmentUrl , total , limit , offset , page );
280282 addControls (output , datasetId , datasetUrl );
281-
283+
282284 // do conneg
283285 String bestMatch = MIMEParse .bestMatch (mimeTypes , request .getHeader ("Accept" ));
284286 Lang contentType = RDFLanguages .contentTypeToLang (bestMatch );
@@ -287,7 +289,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
287289 response .setHeader ("Server" , "Linked Data Fragments Server" );
288290 response .setContentType (bestMatch );
289291 response .setCharacterEncoding ("utf-8" );
290-
292+
291293 RDFDataMgr .write (response .getOutputStream (), output , contentType );
292294 } catch (IOException | URISyntaxException e ) {
293295 e .printStackTrace ();
0 commit comments