ingo@4218: package de.intevation.flys.client.server; ingo@4218: ingo@4218: import java.io.IOException; ingo@4218: import java.io.InputStream; ingo@4218: import java.io.OutputStream; ingo@4218: import java.text.DateFormat; ingo@4218: import java.util.Date; ingo@4218: import java.util.Locale; ingo@4218: ingo@4218: import javax.servlet.http.HttpServlet; ingo@4218: import javax.servlet.http.HttpServletRequest; ingo@4218: import javax.servlet.http.HttpServletResponse; ingo@4218: ingo@4218: import org.apache.log4j.Logger; ingo@4218: import org.w3c.dom.Document; ingo@4218: import org.w3c.dom.Element; ingo@4218: ingo@4218: import de.intevation.artifacts.common.utils.XMLUtils; ingo@4218: import de.intevation.artifacts.httpclient.http.HttpClient; ingo@4218: import de.intevation.artifacts.httpclient.http.HttpClientImpl; ingo@4218: import de.intevation.artifacts.httpclient.http.response.StreamResponseHandler; ingo@4218: ingo@4218: ingo@4218: public class DischargeTablesServiceImpl extends HttpServlet { ingo@4218: ingo@4218: private static final Logger logger = Logger ingo@4218: .getLogger(DischargeInfoServiceImpl.class); ingo@4218: ingo@4218: private static final DateFormat DATE_FORMAT = DateFormat.getDateInstance( ingo@4218: DateFormat.MEDIUM, Locale.GERMANY); ingo@4218: ingo@4218: public void doGet(HttpServletRequest req, HttpServletResponse resp) { ingo@4218: logger.info("ChartOutputServiceImpl.doGet"); ingo@4218: ingo@4218: String url = getServletContext().getInitParameter("server-url"); ingo@4218: String locale = req.getParameter("locale"); ingo@4218: ingo@4218: prepareHeader(req, resp); ingo@4218: ingo@4218: Document requestDoc = createRequestDoc(req); ingo@4218: ingo@4218: HttpClient client = new HttpClientImpl(url, locale); ingo@4218: ingo@4218: try { ingo@4218: OutputStream out = resp.getOutputStream(); ingo@4218: InputStream stream = (InputStream) client.callService(url, ingo@4218: "dischargetablesoverview", requestDoc, ingo@4218: new StreamResponseHandler()); ingo@4218: ingo@4218: byte[] b = new byte[4096]; ingo@4218: try { ingo@4218: int i; ingo@4218: while ((i = stream.read(b)) >= 0) { ingo@4218: out.write(b, 0, i); ingo@4218: } ingo@4218: } ingo@4218: finally { ingo@4218: stream.close(); ingo@4218: } ingo@4218: } ingo@4218: catch (IOException ioe) { ingo@4218: logger.error("Error while fetching discharge tables chart!", ioe); ingo@4218: } ingo@4218: } ingo@4218: ingo@4218: protected void prepareHeader(HttpServletRequest req, ingo@4218: HttpServletResponse resp) { ingo@4218: resp.setHeader("Content-Type", "image/png"); ingo@4218: } ingo@4218: ingo@4218: protected Document createRequestDoc(HttpServletRequest req) { ingo@4218: Document request = XMLUtils.newDocument(); ingo@4218: XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(request, ingo@4218: null, null); ingo@4218: ingo@4218: Element service = creator.create("service"); ingo@4218: Element gauge = creator.create("gauge"); ingo@4218: Element extent = creator.create("extent"); ingo@4218: Element format = creator.create("format"); ingo@4218: Element timerange = creator.create("timerange"); ingo@4218: ingo@4218: creator.addAttr(gauge, "name", extractRequestGauge(req)); ingo@4218: creator.addAttr(extent, "width", extractRequestWidth(req)); ingo@4218: creator.addAttr(extent, "height", extractRequestHeight(req)); ingo@4218: creator.addAttr(format, "type", extractRequestFormat(req)); ingo@4218: creator.addAttr(timerange, "lower", extractRequestLowerTime(req)); ingo@4218: creator.addAttr(timerange, "upper", extractRequestUpperTime(req)); ingo@4218: ingo@4218: request.appendChild(service); ingo@4218: service.appendChild(gauge); ingo@4218: service.appendChild(extent); ingo@4218: service.appendChild(format); ingo@4218: service.appendChild(timerange); ingo@4218: ingo@4218: return request; ingo@4218: } ingo@4218: ingo@4218: protected String extractRequestGauge(HttpServletRequest req) { ingo@4218: return req.getParameter("gauge"); ingo@4218: } ingo@4218: ingo@4218: protected String extractRequestWidth(HttpServletRequest req) { ingo@4218: return req.getParameter("width"); ingo@4218: } ingo@4218: ingo@4218: protected String extractRequestHeight(HttpServletRequest req) { ingo@4218: return req.getParameter("height"); ingo@4218: } ingo@4218: ingo@4218: protected String extractRequestFormat(HttpServletRequest req) { ingo@4218: return req.getParameter("format"); ingo@4218: } ingo@4218: ingo@4218: protected String extractRequestLowerTime(HttpServletRequest req) { ingo@4218: String lowerStr = req.getParameter("lower"); ingo@4218: try { ingo@4218: long lowerMillis = Long.parseLong(lowerStr); ingo@4218: Date lower = new Date(lowerMillis); ingo@4218: ingo@4218: return DATE_FORMAT.format(lower); ingo@4218: } ingo@4218: catch (NumberFormatException nfe) { ingo@4218: logger.warn("Cannot parse time millies.", nfe); ingo@4218: } ingo@4218: ingo@4218: return null; ingo@4218: } ingo@4218: ingo@4218: protected String extractRequestUpperTime(HttpServletRequest req) { ingo@4218: String upperStr = req.getParameter("upper"); ingo@4218: try { ingo@4218: long upperMillis = Long.parseLong(upperStr); ingo@4218: Date upper = new Date(upperMillis); ingo@4218: ingo@4218: return DATE_FORMAT.format(upper); ingo@4218: } ingo@4218: catch (NumberFormatException nfe) { ingo@4218: logger.warn("Cannot parse time millies.", nfe); ingo@4218: } ingo@4218: ingo@4218: return null; ingo@4218: } ingo@4218: }