teichmann@5861: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5861: * Software engineering by Intevation GmbH teichmann@5861: * teichmann@5993: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5861: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5993: * documentation coming with Dive4Elements River for details. teichmann@5861: */ teichmann@5861: teichmann@5835: package org.dive4elements.river.client.server; felix@851: felix@1518: import java.util.ArrayList; felix@1518: import java.util.List; felix@1518: felix@851: import org.w3c.dom.Document; felix@851: ingo@1367: import org.apache.log4j.Logger; ingo@1367: felix@851: import com.google.gwt.user.server.rpc.RemoteServiceServlet; felix@851: teichmann@5835: import org.dive4elements.artifacts.common.ArtifactNamespaceContext; teichmann@5835: import org.dive4elements.artifacts.common.utils.ClientProtocolUtils; teichmann@5835: import org.dive4elements.artifacts.common.utils.XMLUtils; felix@851: teichmann@5835: import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException; teichmann@5835: import org.dive4elements.artifacts.httpclient.http.HttpClient; teichmann@5835: import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; teichmann@5835: import org.dive4elements.artifacts.httpclient.http.response.DocumentResponseHandler; felix@851: teichmann@5835: import org.dive4elements.river.client.shared.exceptions.ServerException; teichmann@5835: import org.dive4elements.river.client.shared.model.Artifact; teichmann@5835: import org.dive4elements.river.client.shared.model.Data; teichmann@5835: import org.dive4elements.river.client.shared.model.DataItem; teichmann@5835: import org.dive4elements.river.client.client.services.FeedService; felix@851: felix@851: /** felix@851: * This interface provides a method that bundles the artifact specific felix@851: * operation FEED. felix@851: */ felix@851: public class FeedServiceImpl felix@851: extends RemoteServiceServlet felix@851: implements FeedService felix@851: { teichmann@8203: private static final Logger log = Logger.getLogger(FeedServiceImpl.class); ingo@1367: ingo@1367: felix@851: /** XPath that points to the result type of a feed or advance operation.*/ felix@851: public static final String XPATH_RESULT = "/art:result/@art:type"; felix@851: felix@851: /** XPath that points to the result type of a feed or advance operation.*/ felix@851: public static final String XPATH_RESULT_MSG = "/art:result/text()"; felix@851: felix@851: /** A constant that marks errors.*/ felix@851: public static final String OPERATION_FAILURE = "FAILURE"; felix@851: felix@851: /** The error message key that is thrown if an error occured while feeding felix@851: * new data.*/ felix@851: public static final String ERROR_FEED_DATA = "error_feed_data"; felix@851: felix@851: felix@851: /** felix@851: * This method triggers the FEED operation. felix@851: * felix@851: * @param artifact The artifact that needs to be fed. felix@851: * @param data An array of Data objects that contain the information that felix@851: * are used for the FEED operation. felix@851: * felix@851: * @return a new artifact parsed from the description of FEED. felix@851: */ felix@851: public Artifact feed( felix@851: String locale, felix@851: Artifact artifact, felix@851: Data[] data) felix@851: throws ServerException felix@851: { teichmann@8203: log.info("StepForwardServiceImpl.feed"); felix@851: raimund@1425: String url = getServletContext().getInitParameter("server-url"); raimund@1425: felix@851: Document feed = ClientProtocolUtils.newFeedDocument( felix@851: artifact.getUuid(), felix@851: artifact.getHash(), felix@851: createKVP(data)); felix@851: felix@851: HttpClient client = new HttpClientImpl(url, locale); felix@851: felix@851: try { felix@851: Document description = (Document) client.feed( teichmann@5835: new org.dive4elements.artifacts.httpclient.objects.Artifact( felix@851: artifact.getUuid(), felix@851: artifact.getHash()), felix@851: feed, felix@851: new DocumentResponseHandler()); felix@851: felix@851: if (description == null) { teichmann@8203: log.warn("StepForwardService.feed() - FAILED"); felix@851: throw new ServerException(ERROR_FEED_DATA); felix@851: } felix@851: felix@851: String result = XMLUtils.xpathString( felix@851: description, felix@851: XPATH_RESULT, felix@851: ArtifactNamespaceContext.INSTANCE); felix@851: felix@851: if (result == null || !result.equals(OPERATION_FAILURE)) { teichmann@8203: log.debug("StepForwardService.feed() - SUCCESS"); felix@851: return (Artifact) new FLYSArtifactCreator().create(description); felix@851: } felix@851: else if (result != null && result.equals(OPERATION_FAILURE)) { felix@851: String msg = XMLUtils.xpathString( felix@851: description, felix@851: XPATH_RESULT_MSG, felix@851: ArtifactNamespaceContext.INSTANCE); felix@851: throw new ServerException(msg); felix@851: } felix@851: } felix@851: catch (ConnectionException ce) { teichmann@8203: log.error(ce, ce); felix@851: } felix@851: teichmann@8203: log.warn("StepForwardService.feed() - FAILED"); felix@851: throw new ServerException(ERROR_FEED_DATA); felix@851: } felix@851: felix@851: felix@851: /** felix@1518: * Triggers FEED operation, many artifacts, same data item(s). felix@1518: * felix@1518: * @param artifacts Artifacts that shall be fed. felix@1518: * @param data An array of Data objects that contain the information that felix@1518: * are used for the FEED operation. felix@1518: * felix@1518: * @return a new artifact parsed from the description of FEED. felix@1518: */ felix@1518: public List feedMany( felix@1518: String locale, felix@1518: List artifacts, felix@1518: Data[] data) felix@1518: throws ServerException felix@1518: { teichmann@8203: log.info("StepForwardServiceImpl.feedMany"); felix@1518: felix@1518: String url = getServletContext().getInitParameter("server-url"); felix@1518: felix@1518: List resultArtifacts = new ArrayList(); felix@1518: felix@1518: for (Artifact artifact: artifacts) { teichmann@8203: log.info("feedMany: Relay to StepForwardServiceImpl.feed"); felix@1518: Artifact fedArtifact = feed(locale, artifact, data); felix@1518: resultArtifacts.add(fedArtifact); felix@1518: } felix@1518: felix@1518: return resultArtifacts; felix@1518: } felix@1518: felix@1518: felix@1518: /** felix@851: * This method creates an array of key/value pairs from an array of Data felix@851: * objects. The string array is used as parameter for the feed() operation. felix@851: * felix@851: * @param data The data that should be transformed into the string array. felix@851: * felix@851: * @return a string array that contains key/value pairs. felix@851: */ felix@851: protected String[][] createKVP(Data[] data) { felix@851: String[][] kvp = new String[data.length][]; felix@851: felix@851: int i = 0; felix@851: felix@851: for (Data d: data) { felix@851: DataItem[] items = d.getItems(); felix@851: String key = d.getLabel(); raimund@2535: String value = d.getStringValue(); felix@851: felix@851: kvp[i++] = new String[] { key, value }; felix@851: } felix@851: felix@851: return kvp; felix@851: } felix@851: } felix@851: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :