1818import java .util .Collection ;
1919import java .util .HashMap ;
2020import java .util .Map .Entry ;
21+ import java .util .logging .Level ;
22+ import java .util .logging .Logger ;
2123import java .util .regex .Matcher ;
2224import java .util .regex .Pattern ;
2325import javax .servlet .ServletConfig ;
3133import org .apache .jena .riot .RDFLanguages ;
3234import org .linkeddatafragments .config .ConfigReader ;
3335import org .linkeddatafragments .datasource .DataSourceFactory ;
36+ import org .linkeddatafragments .datasource .HdtDataSourceType ;
3437import org .linkeddatafragments .datasource .IDataSource ;
3538import org .linkeddatafragments .datasource .IndexDataSource ;
39+ import org .linkeddatafragments .datasource .JenaTDBDataSourceType ;
3640import org .linkeddatafragments .datasource .TriplePatternFragment ;
3741import org .linkeddatafragments .exceptions .DataSourceException ;
42+ import org .linkeddatafragments .exceptions .DataSourceNotFoundException ;
3843import org .linkeddatafragments .util .CommonResources ;
3944import org .linkeddatafragments .util .MIMEParse ;
4045
@@ -64,6 +69,11 @@ public class TriplePatternFragmentServlet extends HttpServlet {
6469 private final HashMap <String , IDataSource > dataSources = new HashMap <>();
6570 private final Collection <String > mimeTypes = new ArrayList <>();
6671
72+ public TriplePatternFragmentServlet () {
73+ HdtDataSourceType .register ();
74+ JenaTDBDataSourceType .register ();
75+ }
76+
6777 private File getConfigFile (ServletConfig config ) throws IOException {
6878 String path = config .getServletContext ().getRealPath ("/" );
6979 if (path == null ) {
@@ -104,14 +114,27 @@ public void init(ServletConfig servletConfig) throws ServletException {
104114 }
105115 }
106116
117+ @ Override
118+ public void destroy ()
119+ {
120+ for ( IDataSource dataSource : dataSources .values () ) {
121+ try {
122+ dataSource .close ();
123+ }
124+ catch ( Exception e ) {
125+ // ignore
126+ }
127+ }
128+ }
129+
107130 /**
108131 * Get the datasource
109132 *
110133 * @param request
111134 * @return
112135 * @throws IOException
113136 */
114- private IDataSource getDataSource (HttpServletRequest request ) throws IOException {
137+ private IDataSource getDataSource (HttpServletRequest request ) throws DataSourceNotFoundException {
115138 String contextPath = request .getContextPath ();
116139 String requestURI = request .getRequestURI ();
117140
@@ -126,7 +149,7 @@ private IDataSource getDataSource(HttpServletRequest request) throws IOException
126149 String dataSourceName = path .substring (1 );
127150 IDataSource dataSource = dataSources .get (dataSourceName );
128151 if (dataSource == null ) {
129- throw new IOException ( "Data source not found." );
152+ throw new DataSourceNotFoundException ( dataSourceName );
130153 }
131154 return dataSource ;
132155 }
@@ -292,8 +315,15 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro
292315
293316 RDFDataMgr .write (response .getOutputStream (), output , contentType );
294317 } catch (IOException | URISyntaxException e ) {
295- e .printStackTrace ();
296318 throw new ServletException (e );
319+ } catch (DataSourceNotFoundException ex ) {
320+ try {
321+ response .setStatus (404 );
322+ response .getOutputStream ().println (ex .getMessage ());
323+ response .getOutputStream ().close ();
324+ } catch (IOException ex1 ) {
325+ throw new ServletException (ex1 );
326+ }
297327 }
298328 }
299329
0 commit comments