ingo@828: package de.intevation.flys.client.server; ingo@828: ingo@828: import org.w3c.dom.Document; ingo@828: import org.w3c.dom.Element; 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: 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; ingo@828: 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: ingo@828: import de.intevation.flys.client.client.services.MapInfoService; 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@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: 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: ingo@828: public MapInfo getMapInfo(String url, String locale, String river) ingo@828: throws ServerException ingo@828: { ingo@828: System.out.println("MapInfoServiceImpl.getMapInfo"); ingo@828: ingo@828: Document request = getRequestDocument(river);; ingo@828: ingo@828: HttpClient client = new HttpClientImpl(url, locale); ingo@828: ingo@828: try { ingo@828: System.out.println("MapInfoServiceImpl.callService"); ingo@828: Document result = client.callService(url, "mapinfo", request); ingo@828: ingo@828: if (result == null) { ingo@828: System.err.println("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@828: System.err.println(ce.getLocalizedMessage()); 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@828: System.out.println("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@828: System.out.println("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: ingo@828: String riverWMS = XMLUtils.xpathString(result, XPATH_RIVER_WMS, 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: ingo@828: return new MapInfo(river, srid, bbox, riverWMS, wmsURL, wmsLayers); ingo@828: } ingo@828: } ingo@828: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :