src/main/java/de/uapcore/lightpit/AbstractLightPITServlet.java

changeset 159
86b5d8a1662f
parent 158
4f912cd42876
child 160
e2d09cf3fb96
equal deleted inserted replaced
158:4f912cd42876 159:86b5d8a1662f
26 * POSSIBILITY OF SUCH DAMAGE. 26 * POSSIBILITY OF SUCH DAMAGE.
27 * 27 *
28 */ 28 */
29 package de.uapcore.lightpit; 29 package de.uapcore.lightpit;
30 30
31 import de.uapcore.lightpit.dao.DataAccessObjects; 31 import de.uapcore.lightpit.dao.DaoProvider;
32 import de.uapcore.lightpit.dao.postgres.PGDataAccessObjects; 32 import de.uapcore.lightpit.dao.postgres.PGDaoProvider;
33 import org.slf4j.Logger; 33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory; 34 import org.slf4j.LoggerFactory;
35 35
36 import javax.servlet.ServletException; 36 import javax.servlet.ServletException;
37 import javax.servlet.http.HttpServlet; 37 import javax.servlet.http.HttpServlet;
98 * Creates a set of data access objects for the specified connection. 98 * Creates a set of data access objects for the specified connection.
99 * 99 *
100 * @param connection the SQL connection 100 * @param connection the SQL connection
101 * @return a set of data access objects 101 * @return a set of data access objects
102 */ 102 */
103 private DataAccessObjects createDataAccessObjects(Connection connection) throws SQLException { 103 private DaoProvider createDataAccessObjects(Connection connection) throws SQLException {
104 final var df = (DataSourceProvider) getServletContext().getAttribute(DataSourceProvider.Companion.getSC_ATTR_NAME()); 104 final var df = (DataSourceProvider) getServletContext().getAttribute(DataSourceProvider.Companion.getSC_ATTR_NAME());
105 if (df.getDialect() == DataSourceProvider.Dialect.Postgres) { 105 if (df.getDialect() == DataSourceProvider.Dialect.Postgres) {
106 return new PGDataAccessObjects(connection); 106 return new PGDaoProvider(connection);
107 } 107 }
108 throw new UnsupportedOperationException("Non-exhaustive if-else - this is a bug."); 108 throw new UnsupportedOperationException("Non-exhaustive if-else - this is a bug.");
109 } 109 }
110 110
111 private void invokeMapping(Map.Entry<PathPattern, Method> mapping, HttpServletRequest req, HttpServletResponse resp, DataAccessObjects dao) throws IOException { 111 private void invokeMapping(Map.Entry<PathPattern, Method> mapping, HttpServletRequest req, HttpServletResponse resp, DaoProvider dao) throws IOException {
112 final var pathPattern = mapping.getKey(); 112 final var pathPattern = mapping.getKey();
113 final var method = mapping.getValue(); 113 final var method = mapping.getValue();
114 try { 114 try {
115 LOG.trace("invoke {}#{}", method.getDeclaringClass().getName(), method.getName()); 115 LOG.trace("invoke {}#{}", method.getDeclaringClass().getName(), method.getName());
116 final var paramTypes = method.getParameterTypes(); 116 final var paramTypes = method.getParameterTypes();
119 if (paramTypes[i].isAssignableFrom(HttpServletRequest.class)) { 119 if (paramTypes[i].isAssignableFrom(HttpServletRequest.class)) {
120 paramValues[i] = req; 120 paramValues[i] = req;
121 } else if (paramTypes[i].isAssignableFrom(HttpServletResponse.class)) { 121 } else if (paramTypes[i].isAssignableFrom(HttpServletResponse.class)) {
122 paramValues[i] = resp; 122 paramValues[i] = resp;
123 } 123 }
124 if (paramTypes[i].isAssignableFrom(DataAccessObjects.class)) { 124 if (paramTypes[i].isAssignableFrom(DaoProvider.class)) {
125 paramValues[i] = dao; 125 paramValues[i] = dao;
126 } 126 }
127 if (paramTypes[i].isAssignableFrom(PathParameters.class)) { 127 if (paramTypes[i].isAssignableFrom(PathParameters.class)) {
128 paramValues[i] = pathPattern.obtainPathParameters(sanitizeRequestPath(req)); 128 paramValues[i] = pathPattern.obtainPathParameters(sanitizeRequestPath(req));
129 } 129 }
178 boolean paramsInjectible = true; 178 boolean paramsInjectible = true;
179 for (var param : method.getParameterTypes()) { 179 for (var param : method.getParameterTypes()) {
180 paramsInjectible &= HttpServletRequest.class.isAssignableFrom(param) 180 paramsInjectible &= HttpServletRequest.class.isAssignableFrom(param)
181 || HttpServletResponse.class.isAssignableFrom(param) 181 || HttpServletResponse.class.isAssignableFrom(param)
182 || PathParameters.class.isAssignableFrom(param) 182 || PathParameters.class.isAssignableFrom(param)
183 || DataAccessObjects.class.isAssignableFrom(param); 183 || DaoProvider.class.isAssignableFrom(param);
184 } 184 }
185 if (paramsInjectible) { 185 if (paramsInjectible) {
186 try { 186 try {
187 PathPattern pathPattern = new PathPattern(mapping.get().requestPath()); 187 PathPattern pathPattern = new PathPattern(mapping.get().requestPath());
188 188

mercurial