net.sf.aguacate.function.FunctionContextServlet Maven / Gradle / Ivy
package net.sf.aguacate.function;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import net.sf.aguacate.configuration.Configuration;
import net.sf.aguacate.field.format.FieldFormat;
import net.sf.aguacate.http.HttpBodyBuilerCoupling;
import net.sf.aguacate.util.config.database.DatabaseBridge;
import net.sf.aguacate.util.config.database.DatabaseCoupling;
import net.sf.aguacate.util.config.database.DatabaseInterface;
import net.sf.aguacate.util.environment.EnvironmentCoupling;
import net.sf.aguacate.util.type.Num;
public class FunctionContextServlet implements FunctionContext {
private static final Logger LOGGER = LogManager.getLogger(FunctionContextServlet.class);
private final String method;
private final String datasource;
private DatabaseBridge databaseBridge;
private final HttpServletRequest request;
private final HttpServletResponse response;
private final Map outputFields;
private Connection connection;
private Map paginationData;
public FunctionContextServlet(Configuration configuration, String method, HttpServletRequest request,
HttpServletResponse response) {
this.method = method;
this.datasource = configuration.getDatasource();
this.request = request;
this.response = response;
this.outputFields = configuration.getOutputFields(method);
}
@Override
public Connection acquireConnection() throws SQLException {
if (connection == null) {
connection = getDatabaseBridge().getDataSource().getConnection();
}
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("using url: {}", connection.getMetaData().getURL());
}
return connection;
}
@Override
public String getMethod() {
return method;
}
@Override
public DatabaseInterface databaseInterface() {
return getDatabaseBridge().getDatabaseInterface();
}
@Override
@SuppressWarnings("unchecked")
public Map get(String name) {
switch (name) {
case "metadata":
return (Map) request.getAttribute(name);
case "environment":
return EnvironmentCoupling.get(name);
case "pagination":
return pagination(name, request);
default:
throw new IllegalArgumentException(name);
}
}
@Override
public void rollback() {
if (connection != null) {
try {
connection.rollback();
} catch (SQLException e) {
throw new IllegalStateException(e);
}
}
}
@Override
public void close() {
if (connection != null) {
Connection temp = connection;
connection = null;
try {
temp.close();
} catch (SQLException e) {
LOGGER.warn("On close connection", e);
}
}
}
@Override
public void handleEmpty() {
response.setStatus(HttpServletResponse.SC_OK);
}
@Override
public void handleNotFound() {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
@Override
public void handleOk(Object object) {
try {
response.setStatus(HttpServletResponse.SC_OK);
HttpBodyBuilerCoupling.defaultInstance().build(response, object, outputFields);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
DatabaseBridge getDatabaseBridge() {
if (databaseBridge == null) {
databaseBridge = DatabaseCoupling.getDatabaseBridge(datasource);
}
return databaseBridge;
}
Map pagination(String method, HttpServletRequest request) {
if (paginationData == null) {
Map data = new LinkedHashMap<>();
Long page = Num.parseLongPositive(request.getParameter("page"));
if (page == null) {
page = Long.valueOf(0);
}
data.put("page", page);
Long items = Num.parseLongPositive(request.getParameter("size"));
if (items == null) {
// TODO: Default value?
items = Long.valueOf(10);
}
data.put("size", items);
data.put("count", items);
Long begin = page * items;
data.put("offset", begin);
data.put("begin", begin);
data.put("end", begin + items);
paginationData = data;
}
return paginationData;
}
}