org.apache.solr.request.PHPSerializedResponseWriter Maven / Gradle / Ivy
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.request;
import java.io.Writer;
import java.io.IOException;
import java.util.*;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.util.UnicodeUtil;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.search.DocIterator;
import org.apache.solr.search.DocList;
import org.apache.solr.search.SolrIndexSearcher;
/**
* A description of the PHP serialization format can be found here:
* http://www.hurring.com/scott/code/perl/serialize/
*
*
* In order to support PHP Serialized strings with a proper byte count, This ResponseWriter
* must know if the Writers passed to it will result in an output of CESU-8 (UTF-8 w/o support
* for large code points outside of the BMP)
*
* Currently Solr assumes that all Jetty servlet containers (detected using the "jetty.home"
* system property) use CESU-8 instead of UTF-8 (verified to the current release of 6.1.20).
*
* In installations where Solr auto-detects incorrectly, the Solr Administrator should set the
* "solr.phps.cesu8" system property to either "true" or "false" accordingly.
*/
public class PHPSerializedResponseWriter implements QueryResponseWriter {
static String CONTENT_TYPE_PHP_UTF8="text/x-php-serialized;charset=UTF-8";
// Is this servlet container's UTF-8 encoding actually CESU-8 (i.e. lacks support for
// large characters outside the BMP).
boolean CESU8 = false;
public void init(NamedList n) {
String cesu8Setting = System.getProperty("solr.phps.cesu8");
if (cesu8Setting != null) {
CESU8="true".equals(cesu8Setting);
} else {
// guess at the setting.
// Jetty up until 6.1.20 at least (and probably versions after) uses CESU8
CESU8 = System.getProperty("jetty.home") != null;
}
}
public void write(Writer writer, SolrQueryRequest req, SolrQueryResponse rsp) throws IOException {
PHPSerializedWriter w = new PHPSerializedWriter(writer, req, rsp, CESU8);
try {
w.writeResponse();
} finally {
w.close();
}
}
public String getContentType(SolrQueryRequest request, SolrQueryResponse response) {
return CONTENT_TYPE_TEXT_UTF8;
}
}
class PHPSerializedWriter extends JSONWriter {
final private boolean CESU8;
final UnicodeUtil.UTF8Result utf8;
public PHPSerializedWriter(Writer writer, SolrQueryRequest req, SolrQueryResponse rsp, boolean CESU8) {
super(writer, req, rsp);
this.CESU8 = CESU8;
this.utf8 = CESU8 ? null : new UnicodeUtil.UTF8Result();
// never indent serialized PHP data
doIndent = false;
}
public void writeResponse() throws IOException {
Boolean omitHeader = req.getParams().getBool(CommonParams.OMIT_HEADER);
if(omitHeader != null && omitHeader) rsp.getValues().remove("responseHeader");
writeNamedList(null, rsp.getValues());
}
@Override
public void writeNamedList(String name, NamedList val) throws IOException {
writeNamedListAsMapMangled(name,val);
}
@Override
public void writeDoc(String name, Collection fields, Set returnFields, Map pseudoFields) throws IOException {
ArrayList single = new ArrayList();
HashMap multi = new HashMap();
for (Fieldable ff : fields) {
String fname = ff.name();
if (returnFields!=null && !returnFields.contains(fname)) {
continue;
}
// if the field is multivalued, it may have other values further on... so
// build up a list for each multi-valued field.
SchemaField sf = schema.getField(fname);
if (sf.multiValued()) {
MultiValueField mf = multi.get(fname);
if (mf==null) {
mf = new MultiValueField(sf, ff);
multi.put(fname, mf);
} else {
mf.fields.add(ff);
}
} else {
single.add(ff);
}
}
// obtain number of fields in doc
writeArrayOpener(single.size() + multi.size() + ((pseudoFields!=null) ? pseudoFields.size() : 0));
// output single value fields
for(Fieldable ff : single) {
SchemaField sf = schema.getField(ff.name());
writeKey(ff.name(),true);
sf.write(this, ff.name(), ff);
}
// output multi value fields
for(MultiValueField mvf : multi.values()) {
writeKey(mvf.sfield.getName(), true);
writeArrayOpener(mvf.fields.size());
int i = 0;
for (Fieldable ff : mvf.fields) {
writeKey(i++, false);
mvf.sfield.write(this, null, ff);
}
writeArrayCloser();
}
// output pseudo fields
if (pseudoFields !=null && pseudoFields.size()>0) {
writeMap(null,pseudoFields,true,false);
}
writeArrayCloser();
}
@Override
public void writeDocList(String name, DocList ids, Set fields, Map otherFields) throws IOException {
boolean includeScore=false;
if (fields!=null) {
includeScore = fields.contains("score");
if (fields.size()==0 || (fields.size()==1 && includeScore) || fields.contains("*")) {
fields=null; // null means return all stored fields
}
}
int sz=ids.size();
writeMapOpener(includeScore ? 4 : 3);
writeKey("numFound",false);
writeInt(null,ids.matches());
writeKey("start",false);
writeInt(null,ids.offset());
if (includeScore) {
writeKey("maxScore",false);
writeFloat(null,ids.maxScore());
}
writeKey("docs",false);
writeArrayOpener(sz);
SolrIndexSearcher searcher = req.getSearcher();
DocIterator iterator = ids.iterator();
for (int i=0; i