Base class for various servlets. @author BaseX Team, BSD License @author Christian Gruen
| 25 | * @author Christian Gruen |
| 26 | */ |
| 27 | public abstract class BaseXServlet extends HttpServlet { |
| 28 | /** Servlet-specific user. */ |
| 29 | private String username; |
| 30 | /** Servlet-specific authentication method. */ |
| 31 | private AuthMethod auth; |
| 32 | |
| 33 | @Override |
| 34 | public void init(final ServletConfig config) throws ServletException { |
| 35 | super.init(config); |
| 36 | |
| 37 | final HTTPContext hc = HTTPContext.get(); |
| 38 | try { |
| 39 | hc.init(config.getServletContext()); |
| 40 | } catch(final IOException ex) { |
| 41 | throw new ServletException(ex); |
| 42 | } |
| 43 | |
| 44 | // parse servlet-specific user and authentication method |
| 45 | for(final String n : Collections.list(config.getInitParameterNames())) { |
| 46 | String name = n; |
| 47 | final String value = config.getInitParameter(name); |
| 48 | if(name.startsWith(Prop.DBPREFIX)) { |
| 49 | name = name.substring(Prop.DBPREFIX.length()); |
| 50 | if(name.equalsIgnoreCase(StaticOptions.USER.name())) { |
| 51 | username = value; |
| 52 | } else if(name.equalsIgnoreCase(StaticOptions.AUTHMETHOD.name())) { |
| 53 | auth = AuthMethod.valueOf(value); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | final Context ctx = hc.context(); |
| 58 | if(ctx.soptions.get(StaticOptions.LOGTRACE)) ctx.setExternal(ctx.log); |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public final void service(final HttpServletRequest request, final HttpServletResponse response) |
| 63 | throws IOException { |
| 64 | |
| 65 | final HTTPConnection conn = new HTTPConnection(request, response, auth); |
| 66 | try { |
| 67 | conn.authenticate(username); |
| 68 | run(conn); |
| 69 | } catch(final HTTPException ex) { |
| 70 | conn.error(ex.getStatus(), Util.message(ex)); |
| 71 | } catch(final LoginException ex) { |
| 72 | conn.error(SC_UNAUTHORIZED, Util.message(ex)); |
| 73 | } catch(final QueryException ex) { |
| 74 | int code = SC_INTERNAL_SERVER_ERROR; |
| 75 | boolean full = conn.context.soptions.get(StaticOptions.RESTXQERRORS); |
| 76 | final QNm qname = ex.qname(); |
| 77 | if(Token.eq(qname.uri(), QueryText.REST_URI)) { |
| 78 | final Value value = ex.value(); |
| 79 | if(value instanceof final ANum num) code = (int) num.itr(); |
| 80 | full = false; |
| 81 | } |
| 82 | conn.error(code, full ? Util.message(ex) : ex.getLocalizedMessage()); |
| 83 | } catch(final IOException ex) { |
| 84 | final boolean full = conn.context.soptions.get(StaticOptions.RESTXQERRORS); |
nothing calls this directly
no outgoing calls
no test coverage detected