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.util.Validator;
025: import com.liferay.portal.kernel.webcache.WebCacheException;
026: import com.liferay.portal.kernel.webcache.WebCacheItem;
027: import com.liferay.portlet.cszsearch.model.CSZAddress;
028: import com.liferay.util.Html;
029: import com.liferay.util.Http;
030: import com.liferay.util.HttpUtil;
031: import com.liferay.util.Time;
032:
033: import java.io.BufferedReader;
034: import java.io.StringReader;
035:
036: import java.util.ArrayList;
037: import java.util.List;
038:
039: /**
040: * <a href="CityStateWebCacheItem.java.html"><b><i>View Source</i></b></a>
041: *
042: * @author Brian Wing Shun Chan
043: *
044: */
045: public class CityStateWebCacheItem implements WebCacheItem {
046:
047: public CityStateWebCacheItem(String cityAndState) {
048: _cityAndState = cityAndState;
049: }
050:
051: public Object convert(String key) throws WebCacheException {
052: List list = new ArrayList();
053:
054: String cityAndState = _cityAndState;
055: String city = null;
056: String state = null;
057:
058: try {
059: int pos = cityAndState.indexOf(StringPool.COMMA);
060:
061: city = cityAndState.substring(0, pos);
062: state = cityAndState.substring(pos + 1,
063: cityAndState.length()).trim();
064: } catch (Exception e) {
065: return list;
066: }
067:
068: try {
069: String text = Http
070: .URLtoString("http://zip4.usps.com/zip4/zcl_1_results.jsp?pagenumber=all"
071: + "&city="
072: + HttpUtil.encodeURL(city)
073: + "&state=" + HttpUtil.encodeURL(state));
074:
075: int x = text.indexOf("<!-- **");
076: int y = text.lastIndexOf("<!-- **");
077:
078: BufferedReader br = new BufferedReader(new StringReader(
079: Html.stripHtml(text.substring(x, y))));
080:
081: String line = null;
082:
083: while ((line = br.readLine()) != null) {
084: line = line.trim();
085:
086: if (!line.equals("")) {
087:
088: if (Validator.isNumber(line)) {
089: list
090: .add(new CSZAddress(null, city, state,
091: line));
092: }
093: }
094: }
095:
096: br.close();
097: } catch (Exception e) {
098: throw new WebCacheException(e);
099: }
100:
101: return list;
102: }
103:
104: public long getRefreshTime() {
105: return _REFRESH_TIME;
106: }
107:
108: private static final long _REFRESH_TIME = Time.DAY * 90;
109:
110: private String _cityAndState;
111:
112: }
|