net.sf.aguacate.function.spi.impl.FunctionResponseJson Maven / Gradle / Ivy
package net.sf.aguacate.function.spi.impl;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import net.sf.aguacate.function.FunctionContext;
import net.sf.aguacate.function.FunctionEvalResult;
import net.sf.aguacate.function.spi.AbstractFunction;
import net.sf.aguacate.http.HttpBodyBuilerCoupling;
import net.sf.aguacate.util.parameter.Parameter;
public class FunctionResponseJson extends AbstractFunction {
private final Parameter parameter;
public FunctionResponseJson(Collection methods, String name, Parameter parameter) {
super(methods, name);
this.parameter = parameter;
}
@Override
public FunctionEvalResult evaluate(FunctionContext functionContext, Map context) {
try {
String parameterName = parameter.getName();
Object object = parameter.calculateContext(context).get(parameterName);
if (object == null) {
functionContext.response().setStatus(HttpServletResponse.SC_NOT_FOUND);
} else {
HttpBodyBuilerCoupling.defaultInstance().build(functionContext.response(), object,
functionContext.getOutputFields());
}
return new FunctionEvalResult(null, null);
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
}