001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.cszsearch.util;
022:
023: import com.liferay.portal.kernel.util.StringPool;
024: import com.liferay.portal.kernel.webcache.WebCacheException;
025: import com.liferay.portal.kernel.webcache.WebCacheItem;
026: import com.liferay.portlet.cszsearch.model.CSZAddress;
027: import com.liferay.util.Http;
028: import com.liferay.util.HttpUtil;
029: import com.liferay.util.Time;
030:
031: import java.io.BufferedReader;
032: import java.io.IOException;
033: import java.io.StringReader;
034:
035: import java.util.ArrayList;
036: import java.util.List;
037:
038: /**
039: * <a href="Zip4WebCacheItem.java.html"><b><i>View Source</i></b></a>
040: *
041: * @author Brian Wing Shun Chan
042: *
043: */
044: public class Zip4WebCacheItem implements WebCacheItem {
045:
046: public Zip4WebCacheItem(String street, String cityAndState) {
047: _street = street;
048: _cityAndState = cityAndState;
049: }
050:
051: public Object convert(String key) throws WebCacheException {
052: List list = new ArrayList();
053:
054: String street = _street;
055: String city = "";
056: String state = "";
057: String zip = "";
058:
059: try {
060: String csz = _cityAndState;
061:
062: int pos = csz.indexOf(StringPool.COMMA);
063:
064: if (pos != -1) {
065: city = csz.substring(0, pos).trim();
066: state = csz.substring(pos + 1, csz.length()).trim();
067: } else {
068: zip = csz;
069: }
070: } catch (Exception e) {
071: return list;
072: }
073:
074: try {
075: String text = Http
076: .URLtoString("http://zip4.usps.com/zip4/zcl_0_results.jsp?visited=1&"
077: + "pagenumber=all&firmname=&urbanization=&address1=&"
078: + "address2="
079: + HttpUtil.encodeURL(street)
080: + "&city="
081: + HttpUtil.encodeURL(city)
082: + "&state=" + state + "&zip5=" + zip);
083:
084: if (text.indexOf("headers=\"full\"") > -1) {
085: int x = text.indexOf("<td headers=\"full\"");
086: x = text.indexOf(">", x) + 1;
087:
088: int y = text.indexOf("<br />", x);
089:
090: street = text.substring(x, y).trim();
091:
092: x = y + 6;
093:
094: y = text.indexOf(" ", x);
095:
096: city = text.substring(x, y).trim();
097:
098: x = y + 6;
099:
100: y = text.indexOf(" ", x);
101:
102: state = text.substring(x, y).trim();
103:
104: x = y + 12;
105:
106: y = text.indexOf("<br />", x);
107:
108: zip = text.substring(x, y).trim();
109:
110: list.add(new CSZAddress(street, city, state, zip));
111: } else if (text.indexOf("headers=\"units\"") > -1) {
112: int x = text.indexOf("<!-- **");
113: int y = text.lastIndexOf("<!-- **");
114:
115: BufferedReader br = new BufferedReader(
116: new StringReader(text.substring(x, y)));
117:
118: String line = null;
119:
120: while ((line = br.readLine()) != null) {
121: if (line.indexOf("headers=\"zip\"") > -1) {
122: zip = br.readLine().trim();
123:
124: list.add(new CSZAddress(street, city, state,
125: zip));
126: }
127: }
128:
129: br.close();
130: }
131: } catch (IOException ioe) {
132: throw new WebCacheException(ioe);
133: } catch (Exception e) {
134: }
135:
136: return list;
137: }
138:
139: public long getRefreshTime() {
140: return _REFRESH_TIME;
141: }
142:
143: private static final long _REFRESH_TIME = Time.DAY * 90;
144:
145: private String _street;
146: private String _cityAndState;
147:
148: }
|