My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* DSM - Distributed Search Manager
* Developed by Milspec Research International Pty Ltd
* $Author: skahl $
* $Revision: 1.7 $
* $Date: 2007/12/04 04:41:22 $
* (c)Copyright 2004
* education.au limited
* DEST
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the names education.au limited, DEST nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
* To the extent permitted by law, the copyright owners of this software and
* its contributors:
* (i) exclude all warranties in relation to the software; and
* (ii) exclude liability for all losses, costs, expenses and damages arising
* in any way from the use of the software whether arising from or in
* relation to breach of contract, negligence or any other tort, in equity
* or otherwise. If the software is in breach of a warranty which is
* implied by law, the copyright owners and contributors liability is
* limited to the replacement of the software.
*
* @author gsingh
*/

package au.edu.educationau.opensource.dsm.adapters;

import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.MessageFormat;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import au.edu.educationau.opensource.dsm.DeusExMachina;
import au.edu.educationau.opensource.dsm.adapters.contenthandler.OpenSearchContentHandler;
import au.edu.educationau.opensource.dsm.cache.DiskCacheEntry;
import au.edu.educationau.opensource.dsm.obj.SearchCriteria;
import au.edu.educationau.opensource.dsm.util.StringUtils;
import au.edu.educationau.opensource.dsm.worker.ResultSetUnifier;

/**
* Generic OpenSearchAdapter. Should handle the parameters that are common to
* both dsm and open search.
*
*/

public class OpenSearchAdapterImpl extends SearchAdapter {

static Logger logger = Logger.getLogger(OpenSearchAdapterImpl.class.getName());

private int startValue = 1;

private String newURL = "";

/** Parent constructor */
public OpenSearchAdapterImpl() {
super();
}

/**
* See the SearchAdapter abstract method doPrime
*
* @exception Throwable
*/
public void doPrime() throws Throwable {
SearchCriteria criteria = super.getSearchCriteria();
String url = this.getProperties().getSearchURL();
int batchSize = this.getProperties().getBatchSize();
String query = criteria.getQuery().replaceAll("\\s", " ");
String phraseDelimiter = this.getProperties().getExtraProperty("opensearch.phrase.delimiter");
String conjunctionDelimiter = this.getProperties().getExtraProperty("opensearch.conjunction.delimiter");
String disjunctionDelimiter = this.getProperties().getExtraProperty("opensearch.disjunction.delimiter");

if ("phrase".equals(criteria.getKeywordConstraint()) && phraseDelimiter != null) {
// add delimiters for phrase search
query = phraseDelimiter + query + phraseDelimiter;
} else if ("all".equals(criteria.getKeywordConstraint()) && conjunctionDelimiter != null) {
// add delimiters for AND search
query = StringUtils.arrayToString(criteria.getQueryArray(), " " + conjunctionDelimiter + " ");
} else if ("any".equals(criteria.getKeywordConstraint()) && disjunctionDelimiter != null) {
// add delimiters for OR search
query = StringUtils.arrayToString(criteria.getQueryArray(), " " + disjunctionDelimiter + " ");
}
// format url according to configuration
this.newURL = getFormattedUrl(url, query, batchSize);

clearURLBuffer();

}

protected String getFormattedUrl(String url, String query, int batchSize) {
try {
query = URLEncoder.encode(query, "UTF-8");
} catch (UnsupportedEncodingException e) {
logger.warn("Failed to encode query param.", e);
}
return MessageFormat.format(url, new Object[] { query, new Integer(batchSize) });
}

/**
* See the SearchAdapter abstract method doPerform
*
* @exception Throwable
*/
public ArrayList doPerform() throws Throwable {
OpenSearchContentHandler eCHandler = newContentHandler();
eCHandler.setSearchAdapterProperties(super.getProperties());
eCHandler.setSearchCriteria(super.getSearchCriteria());
DiskCacheEntry entry = DeusExMachina._DiskCache().getEntry(this.newURL, "UTF8");

if (null != entry) {
Reader reader = entry.getReader("UTF8");

if (null != reader) {
eCHandler = (OpenSearchContentHandler) ResultSetUnifier.processXML(reader, eCHandler, "UTF-8");
}
}

ArrayList batch = eCHandler.getResults();
super.setTheoreticalAvailableResults(eCHandler.getFound());
super.setDiagnosticCode(eCHandler.getDiagnosticCode());
this.newURL = "";
return batch;
}

protected OpenSearchContentHandler newContentHandler() {
return new OpenSearchContentHandler();
}

/**
* See the SearchAdapter abstract method doNextBatch
*
* @exception Throwable
*/
public boolean doNextBatch() throws Throwable {
return false;
}

/**
* See the SearchAdapter abstract method doStopNice
*
* @exception Throwable
*/
public void doStopNice() throws Throwable {
// does nothing here
}

/**
* See the SearchAdapter abstract method doStopForce
*
* @exception Throwable
*/
public void doStopForce() throws Throwable {
// does nothing here
}

/** Class display string */
public String toString() {
return this.getClass().getName();
}

public String getNewURL() {
return newURL;
}
} // -- EOF
Show details Hide details

Change log

r46 by simon.kahl on May 28, 2008   Diff
http://code.google.com/p/distributed-
search-manager/issues/detail?id=4
URL-encode query param when issuing search
Go to: 
Project members, sign in to write a code review

Older revisions

r15 by nick.lothian on Jan 09, 2008   Diff
Initial import.
All revisions of this file

File info

Size: 6064 bytes, 175 lines
Hosted by Google Code