ingo@828: package de.intevation.flys.client.server; ingo@828: ingo@828: import com.google.gwt.user.server.rpc.RemoteServiceServlet; ingo@828: ingo@828: import de.intevation.artifacts.common.utils.XMLUtils; ingo@828: import de.intevation.artifacts.common.utils.XMLUtils.ElementCreator; ingo@828: import de.intevation.artifacts.httpclient.exceptions.ConnectionException; ingo@828: import de.intevation.artifacts.httpclient.http.HttpClient; ingo@828: import de.intevation.artifacts.httpclient.http.HttpClientImpl; christian@4632: import de.intevation.flys.client.client.services.MapInfoService; ingo@828: import de.intevation.flys.client.shared.exceptions.ServerException; ingo@828: import de.intevation.flys.client.shared.model.BBox; ingo@828: import de.intevation.flys.client.shared.model.MapInfo; ingo@828: christian@4632: import org.apache.log4j.Logger; christian@4632: import org.w3c.dom.Document; christian@4632: import org.w3c.dom.Element; ingo@828: ingo@828: ingo@828: /** ingo@828: * This service fetches a document that contains meta information for a specific ingo@828: * chart. ingo@828: * ingo@828: * @author Ingo Weinzierl ingo@828: */ ingo@828: public class MapInfoServiceImpl ingo@828: extends RemoteServiceServlet ingo@828: implements MapInfoService ingo@828: { ingo@1367: private static final Logger logger = ingo@1367: Logger.getLogger(MapInfoServiceImpl.class); ingo@1367: ingo@1367: ingo@828: public static final String XPATH_RIVER = ingo@828: "/mapinfo/river/@name"; ingo@828: ingo@828: public static final String XPATH_SRID = ingo@828: "/mapinfo/river/srid/@value"; ingo@828: ingo@828: public static final String XPATH_BBOX = ingo@828: "/mapinfo/river/bbox/@value"; ingo@828: ingo@828: public static final String XPATH_RIVER_WMS = ingo@828: "/mapinfo/river/river-wms/@url"; ingo@828: christian@4632: public static final String XPATH_RIVER_WMS_LAYERS = christian@4632: "/mapinfo/river/river-wms/@layers"; christian@4632: ingo@828: public static final String XPATH_WMS_URL = ingo@828: "/mapinfo/river/background-wms/@url"; ingo@828: ingo@828: public static final String XPATH_WMS_LAYERS = ingo@828: "/mapinfo/river/background-wms/@layers"; ingo@828: ingo@828: public static final String ERROR_NO_MAPINFO_FOUND = ingo@828: "mapinfo_service_no_result"; ingo@828: ingo@828: christian@4632: @Override raimund@1425: public MapInfo getMapInfo(String locale, String river) ingo@828: throws ServerException ingo@828: { ingo@1367: logger.info("MapInfoServiceImpl.getMapInfo"); ingo@828: raimund@1425: String url = getServletContext().getInitParameter("server-url"); raimund@1425: ingo@828: Document request = getRequestDocument(river);; ingo@828: ingo@828: HttpClient client = new HttpClientImpl(url, locale); ingo@828: ingo@828: try { ingo@1367: logger.debug("MapInfoServiceImpl.callService"); ingo@828: Document result = client.callService(url, "mapinfo", request); ingo@828: ingo@828: if (result == null) { ingo@1367: logger.warn("MapInfo service returned no result."); ingo@828: throw new ServerException(ERROR_NO_MAPINFO_FOUND); ingo@828: } ingo@828: ingo@828: return getMapInfo(result); ingo@828: } ingo@828: catch (ConnectionException ce) { ingo@1367: logger.error(ce, ce); ingo@828: } ingo@828: ingo@828: throw new ServerException(ERROR_NO_MAPINFO_FOUND); ingo@828: } ingo@828: ingo@828: ingo@828: public static Document getRequestDocument(String rivername) { ingo@1367: logger.debug("MapInfoServiceImpl.getRequestDocument"); ingo@828: ingo@828: Document request = XMLUtils.newDocument(); ingo@828: ElementCreator cr = new ElementCreator(request, null, null); ingo@828: ingo@828: Element root = cr.create("mapinfo"); ingo@828: Element river = cr.create("river"); ingo@828: ingo@828: river.setTextContent(rivername); ingo@828: ingo@828: request.appendChild(root); ingo@828: root.appendChild(river); ingo@828: ingo@828: return request; ingo@828: } ingo@828: ingo@828: ingo@828: public static MapInfo getMapInfo(Document result) { ingo@1367: logger.debug("MapInfoServiceImpl.getMapInfo"); ingo@828: ingo@828: String river = XMLUtils.xpathString(result, XPATH_RIVER, null); ingo@828: String sridStr = XMLUtils.xpathString(result, XPATH_SRID, null); ingo@828: String bboxS = XMLUtils.xpathString(result, XPATH_BBOX, null); ingo@828: BBox bbox = BBox.getBBoxFromString(bboxS); ingo@828: christian@4632: String riverWMS = XMLUtils.xpathString(result, XPATH_RIVER_WMS, null); christian@4632: String riverLayers = XMLUtils.xpathString(result, XPATH_RIVER_WMS_LAYERS, null); ingo@828: String wmsURL = XMLUtils.xpathString(result, XPATH_WMS_URL, null); ingo@828: String wmsLayers = XMLUtils.xpathString(result, XPATH_WMS_LAYERS, null); ingo@828: ingo@828: int srid = 4326; ingo@828: ingo@828: try { ingo@828: srid = Integer.parseInt(sridStr); ingo@828: } ingo@828: catch (NumberFormatException nfe) { ingo@828: // do nothing ingo@828: } ingo@828: christian@4632: return new MapInfo(river, srid, bbox, riverWMS, riverLayers, wmsURL, wmsLayers); ingo@828: } ingo@828: } ingo@828: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :