[jboss-svn-commits] JBL Code SVN: r25393 - in labs/jbossrules/trunk/drools-process/drools-workitems/src: main/java/org/drools/process/workitem/rest and 2 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Mon Feb 23 13:49:15 EST 2009
Author: salaboy21
Date: 2009-02-23 13:49:14 -0500 (Mon, 23 Feb 2009)
New Revision: 25393
Added:
labs/jbossrules/trunk/drools-process/drools-workitems/src/main/java/org/drools/process/workitem/rest/
labs/jbossrules/trunk/drools-process/drools-workitems/src/main/java/org/drools/process/workitem/rest/RestGeoCodeApiCallWorkItemHandler.java
labs/jbossrules/trunk/drools-process/drools-workitems/src/main/java/org/drools/process/workitem/rest/ResultGeoCodeApi.java
labs/jbossrules/trunk/drools-process/drools-workitems/src/test/java/org/drools/process/workitem/rest/
labs/jbossrules/trunk/drools-process/drools-workitems/src/test/java/org/drools/process/workitem/rest/RestGeoCodeApiCallWorkItemHandlerTest.java
Log:
Rest WorkItem example with YahooGeoCodeAPI, fix test
Added: labs/jbossrules/trunk/drools-process/drools-workitems/src/main/java/org/drools/process/workitem/rest/RestGeoCodeApiCallWorkItemHandler.java
===================================================================
--- labs/jbossrules/trunk/drools-process/drools-workitems/src/main/java/org/drools/process/workitem/rest/RestGeoCodeApiCallWorkItemHandler.java (rev 0)
+++ labs/jbossrules/trunk/drools-process/drools-workitems/src/main/java/org/drools/process/workitem/rest/RestGeoCodeApiCallWorkItemHandler.java 2009-02-23 18:49:14 UTC (rev 25393)
@@ -0,0 +1,170 @@
+package org.drools.process.workitem.rest;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.OutputStream;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import org.drools.process.instance.WorkItemHandler;
+import org.drools.runtime.process.WorkItem;
+import org.drools.runtime.process.WorkItemManager;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
+import org.xml.sax.XMLReader;
+
+public class RestGeoCodeApiCallWorkItemHandler implements WorkItemHandler {
+
+ private List<ResultGeoCodeApi> results;
+ private int httpResponseCode;
+ public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
+ try {
+ //APPID from yahoo TIpNDenV34Fwcw_x32k1eX6AlQzq4wajFEFvG501Pwc6w9jKEfy2vGnkIn.r5qSQqVvyhPPaTFo-
+ String URL = (String) workItem.getParameter("URL");
+ workItem.getParameters().remove("URL");
+ URL = URL + (String) workItem.getParameter("Service");
+ workItem.getParameters().remove("Service");
+ URL = URL + (String) workItem.getParameter("Method");
+ workItem.getParameters().remove("Method");
+
+ Set<String> keys = workItem.getParameters().keySet();
+ for (String parameter : keys) {
+ URL = URL + parameter + "=" + workItem.getParameter(parameter) + "&";
+ }
+
+
+ HttpURLConnection connection;
+ System.out.println("*** GET Created Customer **");
+ URL getUrl = new URL(URL);
+ connection = (HttpURLConnection) getUrl.openConnection();
+ connection.setRequestMethod("GET");
+ System.out.println("Content-Type: " + connection.getContentType());
+
+ BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
+
+ String line = reader.readLine();
+ while (line != null) {
+ System.out.println(line);
+ line = reader.readLine();
+ }
+ setHttpResponseCode(connection.getResponseCode());
+
+ //
+ this.results = parseResults(line);
+
+ System.out.println("" + line);
+ connection.disconnect();
+
+ } catch (MalformedURLException ex) {
+ Logger.getLogger(RestGeoCodeApiCallWorkItemHandler.class.getName()).log(Level.SEVERE, null, ex);
+ } catch (IOException ex) {
+ Logger.getLogger(RestGeoCodeApiCallWorkItemHandler.class.getName()).log(Level.SEVERE, null, ex);
+ }
+
+
+
+
+
+ }
+
+ private List<ResultGeoCodeApi> parseResults(String xml){
+ List<ResultGeoCodeApi> results = new ArrayList<ResultGeoCodeApi>();
+ try {
+
+ DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
+ DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
+ Document doc = docBuilder.parse(new ByteArrayInputStream(xml.getBytes()));
+ // normalize text representation
+ doc.getDocumentElement ().normalize ();
+ NodeList listOfResults = doc.getElementsByTagName("Result");
+ for(int i= 0; i< listOfResults.getLength(); i++){
+ ResultGeoCodeApi result = new ResultGeoCodeApi();
+ Node nodeResult = listOfResults.item(i);
+ if(nodeResult.getNodeType() == Node.ELEMENT_NODE){
+ Element elementResult = (Element)nodeResult;
+ result.setPrecision(elementResult.getAttribute("precision"));
+
+
+ NodeList latitudes = elementResult.getElementsByTagName("Latitude");
+ Element latitudeElement = (Element)latitudes.item(0);
+ result.setLatitude(latitudeElement.getNodeValue().trim());
+
+ NodeList longitudes = elementResult.getElementsByTagName("Longitude");
+ Element longitudeElement = (Element)longitudes.item(0);
+ result.setLongitude(longitudeElement.getNodeValue().trim());
+
+ NodeList addresses = elementResult.getElementsByTagName("Address");
+ Element addressElement = (Element)addresses.item(0);
+ result.setAddress(addressElement.getNodeValue().trim());
+
+ NodeList cities = elementResult.getElementsByTagName("City");
+ Element cityElement = (Element)cities.item(0);
+ result.setCity(cityElement.getNodeValue().trim());
+
+ NodeList states = elementResult.getElementsByTagName("State");
+ Element stateElement = (Element)states.item(0);
+ result.setState(stateElement.getNodeValue().trim());
+
+ NodeList zips = elementResult.getElementsByTagName("Zip");
+ Element zipElement = (Element)zips.item(0);
+ result.setZip(zipElement.getNodeValue().trim());
+
+ NodeList countries = elementResult.getElementsByTagName("Country");
+ Element countryElement = (Element)countries.item(0);
+ result.setCountry(countryElement.getNodeValue().trim());
+
+ results.add(result);
+ }
+
+ }
+
+ } catch (SAXException ex) {
+ Logger.getLogger(RestGeoCodeApiCallWorkItemHandler.class.getName()).log(Level.SEVERE, null, ex);
+ } catch (IOException ex) {
+ Logger.getLogger(RestGeoCodeApiCallWorkItemHandler.class.getName()).log(Level.SEVERE, null, ex);
+ } catch (ParserConfigurationException ex) {
+ Logger.getLogger(RestGeoCodeApiCallWorkItemHandler.class.getName()).log(Level.SEVERE, null, ex);
+ }
+
+ return results;
+
+ }
+
+ public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
+ // Do nothing, this work item cannot be aborted
+ }
+
+ /**
+ * @return the results
+ */
+ public List<ResultGeoCodeApi> getResults() {
+ return results;
+ }
+
+ /**
+ * @return the httpResponseCode
+ */
+ public int getHttpResponseCode() {
+ return httpResponseCode;
+ }
+
+ /**
+ * @param httpResponseCode the httpResponseCode to set
+ */
+ public void setHttpResponseCode(int httpResponseCode) {
+ this.httpResponseCode = httpResponseCode;
+ }
+}
Added: labs/jbossrules/trunk/drools-process/drools-workitems/src/main/java/org/drools/process/workitem/rest/ResultGeoCodeApi.java
===================================================================
--- labs/jbossrules/trunk/drools-process/drools-workitems/src/main/java/org/drools/process/workitem/rest/ResultGeoCodeApi.java (rev 0)
+++ labs/jbossrules/trunk/drools-process/drools-workitems/src/main/java/org/drools/process/workitem/rest/ResultGeoCodeApi.java 2009-02-23 18:49:14 UTC (rev 25393)
@@ -0,0 +1,146 @@
+/*
+ * Copyright 2009 salaboy.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * under the License.
+ */
+package org.drools.process.workitem.rest;
+
+/**
+ *
+ * @author salaboy
+ */
+public class ResultGeoCodeApi {
+ //http://developer.yahoo.com/maps/rest/V1/geocode.html
+
+ private String precision;
+ private String latitude;
+ private String longitude;
+ private String address;
+ private String city;
+ private String state;
+ private String zip;
+ private String country;
+
+ /**
+ * @return the latitude
+ */
+ public String getLatitude() {
+ return latitude;
+ }
+
+ /**
+ * @param latitude the latitude to set
+ */
+ public void setLatitude(String latitude) {
+ this.latitude = latitude;
+ }
+
+ /**
+ * @return the longitude
+ */
+ public String getLongitude() {
+ return longitude;
+ }
+
+ /**
+ * @param longitude the longitude to set
+ */
+ public void setLongitude(String longitude) {
+ this.longitude = longitude;
+ }
+
+ /**
+ * @return the address
+ */
+ public String getAddress() {
+ return address;
+ }
+
+ /**
+ * @param address the address to set
+ */
+ public void setAddress(String address) {
+ this.address = address;
+ }
+
+ /**
+ * @return the city
+ */
+ public String getCity() {
+ return city;
+ }
+
+ /**
+ * @param city the city to set
+ */
+ public void setCity(String city) {
+ this.city = city;
+ }
+
+ /**
+ * @return the state
+ */
+ public String getState() {
+ return state;
+ }
+
+ /**
+ * @param state the state to set
+ */
+ public void setState(String state) {
+ this.state = state;
+ }
+
+ /**
+ * @return the zip
+ */
+ public String getZip() {
+ return zip;
+ }
+
+ /**
+ * @param zip the zip to set
+ */
+ public void setZip(String zip) {
+ this.zip = zip;
+ }
+
+ /**
+ * @return the country
+ */
+ public String getCountry() {
+ return country;
+ }
+
+ /**
+ * @param country the country to set
+ */
+ public void setCountry(String country) {
+ this.country = country;
+ }
+
+ /**
+ * @return the precision
+ */
+ public String getPrecision() {
+ return precision;
+ }
+
+ /**
+ * @param precision the precision to set
+ */
+ public void setPrecision(String precision) {
+ this.precision = precision;
+ }
+}
Added: labs/jbossrules/trunk/drools-process/drools-workitems/src/test/java/org/drools/process/workitem/rest/RestGeoCodeApiCallWorkItemHandlerTest.java
===================================================================
--- labs/jbossrules/trunk/drools-process/drools-workitems/src/test/java/org/drools/process/workitem/rest/RestGeoCodeApiCallWorkItemHandlerTest.java (rev 0)
+++ labs/jbossrules/trunk/drools-process/drools-workitems/src/test/java/org/drools/process/workitem/rest/RestGeoCodeApiCallWorkItemHandlerTest.java 2009-02-23 18:49:14 UTC (rev 25393)
@@ -0,0 +1,52 @@
+package org.drools.process.workitem.rest;
+
+
+
+import org.drools.process.workitem.rest.*;
+import java.net.HttpURLConnection;
+import java.util.HashMap;
+import java.util.Map;
+import junit.framework.TestCase;
+import org.drools.process.instance.WorkItemManager;
+import org.drools.process.instance.impl.DefaultWorkItemManager;
+import org.drools.process.instance.impl.WorkItemImpl;
+
+
+
+
+public class RestGeoCodeApiCallWorkItemHandlerTest extends TestCase {
+
+
+
+ public void testEmpty(){
+
+ }
+ public void FIXMEtestYahooGeoCode() throws Exception {
+ RestGeoCodeApiCallWorkItemHandler handler = new RestGeoCodeApiCallWorkItemHandler();
+ Map<String, Object> queryParams = new HashMap<String, Object>();
+ queryParams.put( "URL","http://local.yahooapis.com/" );
+ queryParams.put("Service", "MapsService/V1/");
+ queryParams.put("Method", "geocode?");
+ queryParams.put("appid","TIpNDenV34Fwcw_x32k1eX6AlQzq4wajFEFvG501Pwc6w9jKEfy2vGnkIn.r5qSQqVvyhPPaTFo-");
+ //Real parameters
+ queryParams.put("street", "701+First+Ave");
+ queryParams.put("city", "Sunnyvale");
+ queryParams.put("state", "CA");
+
+
+ WorkItemImpl workItem = new WorkItemImpl();
+
+
+ workItem.setParameters(queryParams);
+
+
+ WorkItemManager manager = new DefaultWorkItemManager(null);
+ handler.executeWorkItem( workItem, manager );
+ assertEquals(HttpURLConnection.HTTP_OK, handler.getHttpResponseCode());
+ assertEquals( 1, handler.getResults().size() );
+ assertEquals("US", ((ResultGeoCodeApi)handler.getResults().get(0)).getCountry());
+
+ }
+
+
+}
More information about the jboss-svn-commits
mailing list