| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package no.sesat.search.run; |
| 19 | |
|
| 20 | |
|
| 21 | |
import java.io.IOException; |
| 22 | |
import java.util.Enumeration; |
| 23 | |
import java.util.Hashtable; |
| 24 | |
import java.util.Map; |
| 25 | |
import java.util.Set; |
| 26 | |
import javax.servlet.http.HttpServletRequest; |
| 27 | |
import javax.servlet.http.HttpServletResponse; |
| 28 | |
import no.sesat.search.datamodel.DataModel; |
| 29 | |
import no.sesat.search.site.SiteKeyedFactoryInstantiationException; |
| 30 | |
import org.apache.commons.lang.StringEscapeUtils; |
| 31 | |
import org.apache.commons.lang.StringUtils; |
| 32 | |
import org.apache.log4j.Logger; |
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public final class RunningWebQuery extends RunningQueryImpl { |
| 39 | |
|
| 40 | 0 | private static final Logger LOG = Logger.getLogger(RunningWebQuery.class); |
| 41 | |
private static final String ERR_SEND_ERROR = "!!! Unable to sendError !!!"; |
| 42 | |
|
| 43 | |
private final HttpServletRequest request; |
| 44 | |
private final HttpServletResponse response; |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | 0 | private static final String[] ATTRS_TO_COPY = new String[] { |
| 52 | |
"REMOTE_ADDR" |
| 53 | |
}; |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | 0 | private static final String[] HEADERS_TO_COPY = new String[] { |
| 59 | |
"user-agent", |
| 60 | |
"x-forwarded-for" |
| 61 | |
}; |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
public RunningWebQuery(final Context cxt, |
| 72 | |
final String query, |
| 73 | |
final HttpServletRequest request, |
| 74 | |
final HttpServletResponse response) throws SiteKeyedFactoryInstantiationException { |
| 75 | |
|
| 76 | 0 | super(cxt, query); |
| 77 | |
|
| 78 | 0 | this.request = request; |
| 79 | 0 | this.response = response; |
| 80 | |
|
| 81 | 0 | if (LOG.isTraceEnabled()) { |
| 82 | 0 | LOG.trace("RunningWebQuery(mode, " + query + ", request, response)"); |
| 83 | |
} |
| 84 | |
|
| 85 | |
|
| 86 | 0 | final Map<String,Object> parameters = datamodel.getJunkYard().getValues(); |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | 0 | for (String key : datamodel.getParameters().getValues().keySet()) { |
| 92 | |
|
| 93 | 0 | final String value = datamodel.getParameters().getValue(key).getString(); |
| 94 | 0 | parameters.put(key, value); |
| 95 | |
|
| 96 | 0 | if (LOG.isTraceEnabled()) { |
| 97 | 0 | LOG.trace("Added " + key + ", value: " + value + ", "); |
| 98 | |
} |
| 99 | 0 | } |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | 0 | if (parameters.containsKey("nav_newspaperNames")) { |
| 104 | 0 | parameters.put("nav_newspaperNames", "newssourcenavigator"); |
| 105 | |
} |
| 106 | |
|
| 107 | 0 | if (parameters.containsKey("nav_videosourceNames")) { |
| 108 | 0 | parameters.put("nav_videosourceNames", "videosourcenavigator"); |
| 109 | |
} |
| 110 | |
|
| 111 | 0 | if (parameters.containsKey("ywpopnavn")) { |
| 112 | 0 | parameters.put("newssource", parameters.get("ywpopnavn")); |
| 113 | 0 | parameters.remove("ywpopnavn"); |
| 114 | |
} |
| 115 | |
|
| 116 | |
|
| 117 | 0 | for (Enumeration<String> e = (Enumeration<String>)request.getAttributeNames(); e.hasMoreElements();) { |
| 118 | |
|
| 119 | 0 | final String attrName = e.nextElement(); |
| 120 | |
|
| 121 | |
|
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | |
|
| 126 | |
|
| 127 | 0 | parameters.put(attrName, request.getAttribute(attrName)); |
| 128 | 0 | if (LOG.isTraceEnabled()) { |
| 129 | 0 | LOG.trace("Added " + attrName + ", value: " + request.getAttribute(attrName)); |
| 130 | |
} |
| 131 | 0 | } |
| 132 | |
|
| 133 | 0 | for (final String attrName : ATTRS_TO_COPY) { |
| 134 | 0 | if (request.getAttribute(attrName) != null) { |
| 135 | 0 | parameters.put(attrName, request.getAttribute(attrName)); |
| 136 | |
|
| 137 | 0 | if (LOG.isTraceEnabled()) { |
| 138 | 0 | LOG.trace("Added(Manually) " + attrName + ", value: " + request.getAttribute(attrName)); |
| 139 | |
} |
| 140 | |
} |
| 141 | |
} |
| 142 | |
|
| 143 | 0 | for (final String header : HEADERS_TO_COPY) { |
| 144 | 0 | if (request.getHeader(header) != null) { |
| 145 | 0 | parameters.put(header, request.getHeader(header)); |
| 146 | |
|
| 147 | 0 | if (LOG.isTraceEnabled()) { |
| 148 | 0 | LOG.trace("Added HTTP header " + header + ", value: " + request.getHeader(header)); |
| 149 | |
} |
| 150 | |
} |
| 151 | |
} |
| 152 | |
|
| 153 | 0 | parameters.put("request", request); |
| 154 | 0 | parameters.put("response", response); |
| 155 | |
|
| 156 | |
|
| 157 | 0 | } |
| 158 | |
|
| 159 | |
@Override |
| 160 | |
public void run() throws InterruptedException{ |
| 161 | |
|
| 162 | 0 | super.run(); |
| 163 | |
|
| 164 | 0 | if( allCancelled ){ |
| 165 | |
|
| 166 | |
try { |
| 167 | |
|
| 168 | 0 | response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE); |
| 169 | 0 | } catch (IOException ex) { |
| 170 | 0 | LOG.error(ERR_SEND_ERROR, ex); |
| 171 | 0 | } |
| 172 | |
|
| 173 | |
}else{ |
| 174 | |
|
| 175 | |
|
| 176 | 0 | for( Map.Entry<String,Object> entry : datamodel.getJunkYard().getValues().entrySet() ){ |
| 177 | |
|
| 178 | 0 | if( entry.getValue() instanceof String[] && ((String[])entry.getValue()).length ==1 ){ |
| 179 | 0 | request.setAttribute(entry.getKey(), ((String[])entry.getValue())[0]); |
| 180 | |
}else{ |
| 181 | 0 | request.setAttribute(entry.getKey(), entry.getValue()); |
| 182 | |
} |
| 183 | 0 | if (LOG.isTraceEnabled()) { |
| 184 | 0 | LOG.trace("Added " + entry.getKey() + ", value: " + request.getAttribute(entry.getKey())); |
| 185 | |
} |
| 186 | |
} |
| 187 | |
|
| 188 | |
|
| 189 | 0 | request.setAttribute("hits",getHits()); |
| 190 | |
} |
| 191 | 0 | } |
| 192 | |
} |