From 1b84b2039700a35af298144fb2ec02c2d3a68e67 Mon Sep 17 00:00:00 2001 From: Bernard Labno Date: Tue, 17 May 2011 15:25:21 +0000 Subject: [PATCH] Initial import of iplocator-api project. --- iplocator-api/pom.xml | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/Service.java | 26 ++++++++++++++++++++++++++ iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/rest/InfoUnmarshaller.java | 40 ++++++++++++++++++++++++++++++++++++++++ iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/rest/generated/IpInfo.java | 654 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/rest/generated/ObjectFactory.java | 47 +++++++++++++++++++++++++++++++++++++++++++++++ iplocator-api/src/main/xjb/bindings.xjb | 13 +++++++++++++ iplocator-api/src/main/xsd/response.xml | 30 ++++++++++++++++++++++++++++++ iplocator-api/src/main/xsd/response.xsd | 33 +++++++++++++++++++++++++++++++++ iplocator-api/src/test/java/pl/labno/bernard/geobytes/iplocator/ServiceTest.java | 24 ++++++++++++++++++++++++ 9 files changed, 1003 insertions(+), 0 deletions(-) create mode 100644 iplocator-api/pom.xml create mode 100644 iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/Service.java create mode 100644 iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/rest/InfoUnmarshaller.java create mode 100644 iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/rest/generated/IpInfo.java create mode 100644 iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/rest/generated/ObjectFactory.java create mode 100644 iplocator-api/src/main/xjb/bindings.xjb create mode 100644 iplocator-api/src/main/xsd/response.xml create mode 100644 iplocator-api/src/main/xsd/response.xsd create mode 100644 iplocator-api/src/test/java/pl/labno/bernard/geobytes/iplocator/ServiceTest.java diff --git a/iplocator-api/pom.xml b/iplocator-api/pom.xml new file mode 100644 index 0000000..c0b5ede --- /dev/null +++ b/iplocator-api/pom.xml @@ -0,0 +1,136 @@ + + + 4.0.0 + + pl.labno.bernard.geobytes + iplocator-api + 1.0 + + + + junit + junit + 4.5 + test + + + net.sourceforge.yarfraw + yarfraw + 0.92 + + + commons-codec + commons-codec + 1.4 + + + commons-logging + commons-logging-api + 1.1 + + + javax.xml.bind + jaxb-api + 2.1 + + + com.sun.xml.bind + jaxb-impl + 2.1 + test + + + + + + generate-jaxb-artifacts + + + + org.codehaus.mojo + jaxb2-maven-plugin + + + + xjc + + + + + pl.labno.bernard.geobytes.iplocator.rest.generated + ${build.sourceDirectory} + false + false + + + + + + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + + testOutputDirectory + ${build.testOutputDirectory} + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.5 + 1.5 + ${project.build.sourceEncoding} + + + + org.apache.maven.plugins + maven-source-plugin + 2.1.1 + + + + jar + + + + + + org.apache.maven.plugins + maven-resources-plugin + 2.2 + + ${project.build.sourceEncoding} + + + + + + + UTF-8 + + + + bernard.labno.pl + MyCo Internal Repository + http://bernard.labno.pl/artifactory/libs-release-local + + + + + + + + + + \ No newline at end of file diff --git a/iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/Service.java b/iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/Service.java new file mode 100644 index 0000000..8eccc03 --- /dev/null +++ b/iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/Service.java @@ -0,0 +1,26 @@ +package pl.labno.bernard.geobytes.iplocator; + +import pl.labno.bernard.geobytes.iplocator.rest.InfoUnmarshaller; +import pl.labno.bernard.geobytes.iplocator.rest.generated.IpInfo; +import yarfraw.core.datamodel.YarfrawException; + +import javax.xml.bind.JAXBException; +import java.io.IOException; +import java.net.InetAddress; + +public class Service { + private String serviceURL = "http://www.geobytes.com/IpLocator.htm?GetLocation&template=xml.txt&IpAddress="; + + public String getServiceURL() { + return serviceURL; + } + + public void setServiceURL(String serviceURL) { + this.serviceURL = serviceURL; + } + + public IpInfo getInfo(InetAddress address) throws IOException, YarfrawException, JAXBException { + return InfoUnmarshaller.unmarshal(serviceURL + address.getHostAddress()); + } + +} diff --git a/iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/rest/InfoUnmarshaller.java b/iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/rest/InfoUnmarshaller.java new file mode 100644 index 0000000..bcf4599 --- /dev/null +++ b/iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/rest/InfoUnmarshaller.java @@ -0,0 +1,40 @@ +package pl.labno.bernard.geobytes.iplocator.rest; + +import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import pl.labno.bernard.geobytes.iplocator.rest.generated.IpInfo; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.io.StringReader; +import java.net.URL; + +public class InfoUnmarshaller { + + private static final Log LOG = LogFactory.getLog(InfoUnmarshaller.class); + + public static IpInfo unmarshal(String url) throws JAXBException, IOException { + return unmarshal(new URL(url).openStream()); + } + + public static IpInfo unmarshal(InputStream stream) throws JAXBException, IOException { + return unmarshal(new InputStreamReader(stream)); + } + + public static IpInfo unmarshal(Reader reader) throws JAXBException, IOException { + String content = IOUtils.toString(reader); + try { + JAXBContext jaxbContext = JAXBContext.newInstance(IpInfo.class.getPackage().getName()); + return (IpInfo) jaxbContext.createUnmarshaller().unmarshal(new StringReader(content)); + } catch (JAXBException e) { + LOG.error("Cannot unmarshal input stream.\n" + content, e); + throw e; + } + } + +} diff --git a/iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/rest/generated/IpInfo.java b/iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/rest/generated/IpInfo.java new file mode 100644 index 0000000..ea9dca8 --- /dev/null +++ b/iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/rest/generated/IpInfo.java @@ -0,0 +1,654 @@ +// +// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-833 +// See http://java.sun.com/xml/jaxb +// Any modifications to this file will be lost upon recompilation of the source schema. +// Generated on: 2011.05.17 at 03:13:16 PM GMT +// + + +package pl.labno.bernard.geobytes.iplocator.rest.generated; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlType; + + +/** + *

Java class for info element declaration. + * + *

The following schema fragment specifies the expected content contained within this class. + * + *

+ * <element name="info">
+ *   <complexType>
+ *     <complexContent>
+ *       <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *         <sequence>
+ *           <element name="IP" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *           <element name="countryid" type="{http://www.w3.org/2001/XMLSchema}short"/>
+ *           <element name="country" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *           <element name="fips" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *           <element name="iso2" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *           <element name="iso3" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *           <element name="ison" type="{http://www.w3.org/2001/XMLSchema}short"/>
+ *           <element name="internet" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *           <element name="comment" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *           <element name="regionid" type="{http://www.w3.org/2001/XMLSchema}short"/>
+ *           <element name="region" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *           <element name="code" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *           <element name="adm" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *           <element name="cityid" type="{http://www.w3.org/2001/XMLSchema}short"/>
+ *           <element name="city" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *           <element name="latitude" type="{http://www.w3.org/2001/XMLSchema}float"/>
+ *           <element name="longitude" type="{http://www.w3.org/2001/XMLSchema}float"/>
+ *           <element name="timezone" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *           <element name="dmaid" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *           <element name="dma" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *           <element name="market" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *           <element name="certainty" type="{http://www.w3.org/2001/XMLSchema}byte"/>
+ *           <element name="locationcode" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *           <element name="ipaddress" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *         </sequence>
+ *       </restriction>
+ *     </complexContent>
+ *   </complexType>
+ * </element>
+ * 
+ * + * + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "", propOrder = { + "ip", + "countryid", + "country", + "fips", + "iso2", + "iso3", + "ison", + "internet", + "comment", + "regionid", + "region", + "code", + "adm", + "cityid", + "city", + "latitude", + "longitude", + "timezone", + "dmaid", + "dma", + "market", + "certainty", + "locationcode", + "ipaddress" +}) +@XmlRootElement(name = "info") +public class IpInfo { + + @XmlElement(name = "IP", required = true) + protected String ip; + protected short countryid; + @XmlElement(required = true) + protected String country; + @XmlElement(required = true) + protected String fips; + @XmlElement(required = true) + protected String iso2; + @XmlElement(required = true) + protected String iso3; + protected short ison; + @XmlElement(required = true) + protected String internet; + @XmlElement(required = true) + protected String comment; + protected short regionid; + @XmlElement(required = true) + protected String region; + @XmlElement(required = true) + protected String code; + @XmlElement(required = true) + protected String adm; + protected short cityid; + @XmlElement(required = true) + protected String city; + protected float latitude; + protected float longitude; + @XmlElement(required = true) + protected String timezone; + @XmlElement(required = true) + protected String dmaid; + @XmlElement(required = true) + protected String dma; + @XmlElement(required = true) + protected String market; + protected byte certainty; + @XmlElement(required = true) + protected String locationcode; + @XmlElement(required = true) + protected String ipaddress; + + /** + * Gets the value of the ip property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getIP() { + return ip; + } + + /** + * Sets the value of the ip property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setIP(String value) { + this.ip = value; + } + + /** + * Gets the value of the countryid property. + * + */ + public short getCountryid() { + return countryid; + } + + /** + * Sets the value of the countryid property. + * + */ + public void setCountryid(short value) { + this.countryid = value; + } + + /** + * Gets the value of the country property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getCountry() { + return country; + } + + /** + * Sets the value of the country property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setCountry(String value) { + this.country = value; + } + + /** + * Gets the value of the fips property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getFips() { + return fips; + } + + /** + * Sets the value of the fips property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setFips(String value) { + this.fips = value; + } + + /** + * Gets the value of the iso2 property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getIso2() { + return iso2; + } + + /** + * Sets the value of the iso2 property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setIso2(String value) { + this.iso2 = value; + } + + /** + * Gets the value of the iso3 property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getIso3() { + return iso3; + } + + /** + * Sets the value of the iso3 property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setIso3(String value) { + this.iso3 = value; + } + + /** + * Gets the value of the ison property. + * + */ + public short getIson() { + return ison; + } + + /** + * Sets the value of the ison property. + * + */ + public void setIson(short value) { + this.ison = value; + } + + /** + * Gets the value of the internet property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getInternet() { + return internet; + } + + /** + * Sets the value of the internet property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setInternet(String value) { + this.internet = value; + } + + /** + * Gets the value of the comment property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getComment() { + return comment; + } + + /** + * Sets the value of the comment property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setComment(String value) { + this.comment = value; + } + + /** + * Gets the value of the regionid property. + * + */ + public short getRegionid() { + return regionid; + } + + /** + * Sets the value of the regionid property. + * + */ + public void setRegionid(short value) { + this.regionid = value; + } + + /** + * Gets the value of the region property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getRegion() { + return region; + } + + /** + * Sets the value of the region property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setRegion(String value) { + this.region = value; + } + + /** + * Gets the value of the code property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getCode() { + return code; + } + + /** + * Sets the value of the code property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setCode(String value) { + this.code = value; + } + + /** + * Gets the value of the adm property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getAdm() { + return adm; + } + + /** + * Sets the value of the adm property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setAdm(String value) { + this.adm = value; + } + + /** + * Gets the value of the cityid property. + * + */ + public short getCityid() { + return cityid; + } + + /** + * Sets the value of the cityid property. + * + */ + public void setCityid(short value) { + this.cityid = value; + } + + /** + * Gets the value of the city property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getCity() { + return city; + } + + /** + * Sets the value of the city property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setCity(String value) { + this.city = value; + } + + /** + * Gets the value of the latitude property. + * + */ + public float getLatitude() { + return latitude; + } + + /** + * Sets the value of the latitude property. + * + */ + public void setLatitude(float value) { + this.latitude = value; + } + + /** + * Gets the value of the longitude property. + * + */ + public float getLongitude() { + return longitude; + } + + /** + * Sets the value of the longitude property. + * + */ + public void setLongitude(float value) { + this.longitude = value; + } + + /** + * Gets the value of the timezone property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getTimezone() { + return timezone; + } + + /** + * Sets the value of the timezone property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setTimezone(String value) { + this.timezone = value; + } + + /** + * Gets the value of the dmaid property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getDmaid() { + return dmaid; + } + + /** + * Sets the value of the dmaid property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setDmaid(String value) { + this.dmaid = value; + } + + /** + * Gets the value of the dma property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getDma() { + return dma; + } + + /** + * Sets the value of the dma property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setDma(String value) { + this.dma = value; + } + + /** + * Gets the value of the market property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getMarket() { + return market; + } + + /** + * Sets the value of the market property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setMarket(String value) { + this.market = value; + } + + /** + * Gets the value of the certainty property. + * + */ + public byte getCertainty() { + return certainty; + } + + /** + * Sets the value of the certainty property. + * + */ + public void setCertainty(byte value) { + this.certainty = value; + } + + /** + * Gets the value of the locationcode property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getLocationcode() { + return locationcode; + } + + /** + * Sets the value of the locationcode property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setLocationcode(String value) { + this.locationcode = value; + } + + /** + * Gets the value of the ipaddress property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getIpaddress() { + return ipaddress; + } + + /** + * Sets the value of the ipaddress property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setIpaddress(String value) { + this.ipaddress = value; + } + +} diff --git a/iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/rest/generated/ObjectFactory.java b/iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/rest/generated/ObjectFactory.java new file mode 100644 index 0000000..0d1c657 --- /dev/null +++ b/iplocator-api/src/main/java/pl/labno/bernard/geobytes/iplocator/rest/generated/ObjectFactory.java @@ -0,0 +1,47 @@ +// +// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.1-833 +// See http://java.sun.com/xml/jaxb +// Any modifications to this file will be lost upon recompilation of the source schema. +// Generated on: 2011.05.17 at 03:13:16 PM GMT +// + + +package pl.labno.bernard.geobytes.iplocator.rest.generated; + +import javax.xml.bind.annotation.XmlRegistry; + + +/** + * This object contains factory methods for each + * Java content interface and Java element interface + * generated in the pl.labno.bernard.geobytes.iplocator.rest.generated package. + *

An ObjectFactory allows you to programatically + * construct new instances of the Java representation + * for XML content. The Java representation of XML + * content can consist of schema derived interfaces + * and classes representing the binding of schema + * type definitions, element declarations and model + * groups. Factory methods for each of these are + * provided in this class. + * + */ +@XmlRegistry +public class ObjectFactory { + + + /** + * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: pl.labno.bernard.geobytes.iplocator.rest.generated + * + */ + public ObjectFactory() { + } + + /** + * Create an instance of {@link IpInfo } + * + */ + public IpInfo createIpInfo() { + return new IpInfo(); + } + +} diff --git a/iplocator-api/src/main/xjb/bindings.xjb b/iplocator-api/src/main/xjb/bindings.xjb new file mode 100644 index 0000000..5daf00d --- /dev/null +++ b/iplocator-api/src/main/xjb/bindings.xjb @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/iplocator-api/src/main/xsd/response.xml b/iplocator-api/src/main/xsd/response.xml new file mode 100644 index 0000000..41f55a4 --- /dev/null +++ b/iplocator-api/src/main/xsd/response.xml @@ -0,0 +1,30 @@ + + + 87.207.25.131 + 197 + Poland + PL + PL + POL + 616 + PL + + + 3275 + Kujawsko-Pomorskie + KP + PL73 + 12699 + Bydgoszcz + 53.1500 + 18.0000 + + +01:00 + + + + 90 + PLKPBYDG + 87.207.25.131 + + diff --git a/iplocator-api/src/main/xsd/response.xsd b/iplocator-api/src/main/xsd/response.xsd new file mode 100644 index 0000000..f4bc5c3 --- /dev/null +++ b/iplocator-api/src/main/xsd/response.xsd @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/iplocator-api/src/test/java/pl/labno/bernard/geobytes/iplocator/ServiceTest.java b/iplocator-api/src/test/java/pl/labno/bernard/geobytes/iplocator/ServiceTest.java new file mode 100644 index 0000000..8e194ad --- /dev/null +++ b/iplocator-api/src/test/java/pl/labno/bernard/geobytes/iplocator/ServiceTest.java @@ -0,0 +1,24 @@ +package pl.labno.bernard.geobytes.iplocator; + +import org.junit.Assert; +import org.junit.Test; +import pl.labno.bernard.geobytes.iplocator.rest.generated.IpInfo; +import yarfraw.core.datamodel.YarfrawException; + +import javax.xml.bind.JAXBException; +import java.io.IOException; +import java.net.Inet4Address; + +public class ServiceTest { + + @Test + public void testGetRecentExchangeTable() throws YarfrawException, IOException, JAXBException { + final Service service = new Service(); + IpInfo info = service.getInfo(Inet4Address.getByAddress(new byte[]{87, (byte) 207, 25, (byte) 131})); + Assert.assertNotNull(info); + Assert.assertEquals(197, info.getCountryid()); + info = service.getInfo(Inet4Address.getByAddress(new byte[]{(byte) 165, 69, 10, 27})); + Assert.assertNotNull(info); + Assert.assertEquals(14, info.getCountryid()); + } +} -- libgit2 0.24.0