diff --git a/webservice/Main.jws b/webservice/Main.jws new file mode 100644 index 0000000000000000000000000000000000000000..a44e7d781bf3066524a2062f96907ab727768581 --- /dev/null +++ b/webservice/Main.jws @@ -0,0 +1,178 @@ +/* + * Copyright (C) 2004-2011 Centro de Computacao Cientifica e Software Livre + * Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR + * + * This file is part of datasid + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, + * USA. + */ + +/* General libs */ +import java.io.*; +import java.util.*; +import java.text.*; + +/* File libs */ +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.StringWriter; + +/* DOM */ +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import org.w3c.dom.*; + +/* Regular Expressions */ +import java.util.regex.Pattern; +import java.util.regex.Matcher; + +public class Main { + + private static final String AGENT_VERSION = "1.0.0"; + private static final String AGENT_UPDATE_LINK = "http://localhost:8280/webservice/datasid-1.0.0-update.run"; + + // enum does not work as expected inside an axis web service + // using simple constants instead + private static final int ERROR = 0; + private static final int WARNING = 1; + private static final int INFO = 2; + private static final int DEBUG = 3; + + /** + * Returns the name of the log level + * + * @author Eduardo Luis Buratti + * @param level Log level + */ + private static String logLevelName(int level) { + if (level == ERROR) return "ERROR"; + else if (level == WARNING) return "WARNING"; + else if (level == INFO) return "INFO"; + else if (level == DEBUG) return "DEBUG"; + else return "NULL"; + } + + /** + * Write messages to the log + * + * @author Eduardo Luis Buratti + * @param level Log level + * @param msg Message + */ + private static void log(int level, String msg) { + System.out.println(getTimestamp() + " [DataSeedWS][" + logLevelName(level) + "]: " + msg); + } + + /** + * Returns a string representing the current date + * + * @author Eduardo Luis Buratti + * @return String + */ + private static String getDate() { + Calendar date = Calendar.getInstance(); // gets current date + + int year = date.get(Calendar.YEAR); + int month = date.get(Calendar.MONTH); + int day = date.get(Calendar.DATE); + + /* NOTE: Calendar.MONTH field value is 0-based. e.g: 0 is January */ + month++; + + return year + "-" + month + "-" + day; + } + + /** + * Returns current timestamp + * + * @author Eduardo Luis Buratti + * @return String + */ + private static String getTimestamp() { + // gets the current time + Date date = new Date(); + long milisecs = date.getTime(); + + // generates timestamp string + Timestamp timestamp = new Timestamp(milisecs); + return timestamp.toString(); + } + + + /** + * Parse a XML and returns the root DOM Element + * + * @author Eduardo Luis Buratti + * @param xml The xml string to be parsed + * @return Element + */ + private static Element parseXML(String xml) throws Exception { + // transform the xml string into a InputStream + InputStream is = new ByteArrayInputStream(xml.getBytes()); + + // creates a builder + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = dbf.newDocumentBuilder(); + + Document dom = builder.parse(is); + + return dom.getDocumentElement(); + } + + /** + * Receive an XML string which has the inventory data to be parsed and + * inserted into database. Return "Success" string if insertion operation + * is successful. Any other errors will throw exceptions. + * + * @author Eduardo Luis Buratti + * @param xmlData XML string of inventory + * @return String + */ + public static String setInventory(String xmlData) { + try { + Element root = parseXML(xmlData); + System.out.println("RECEIVED: " + root.getTagName()); + return "Success"; + } catch (Exception e) { + log(ERROR, e.getMessage() + " " + xmlData); + return "ERROR: " + e.getMessage(); + } + } + + /** + * Return a string with current agent version. + * + * @author Eduardo Luis Buratti + * @return String + */ + public static String getAgentVersion() { + return AGENT_VERSION; + } + + /** + * Return a string that contains a link to download the newest version of + * agent. + * + * @author Eduardo Luis Buratti + * @return String + */ + public static String getUpdateLink() + { + return AGENT_UPDATE_LINK; + } + +}