Created
June 4, 2014 13:21
-
-
Save christophstrobl/a0f8e32dcefb74b4c0d5 to your computer and use it in GitHub Desktop.
Using json content format with solrJ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2014 the original author or authors. | |
* | |
* Licensed 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 com.example.solr; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.Reader; | |
import java.nio.charset.Charset; | |
import javax.activation.MimeType; | |
import javax.activation.MimeTypeParseException; | |
import org.apache.solr.client.solrj.ResponseParser; | |
import org.apache.solr.common.util.NamedList; | |
import org.springframework.dao.InvalidDataAccessResourceUsageException; | |
import org.springframework.util.StreamUtils; | |
/** | |
* @author Christoph Strobl | |
*/ | |
public class MappingJacksonResponseParser extends ResponseParser { | |
private static final String WRITER = "json"; | |
private final MimeType responseType; | |
public MappingJacksonResponseParser() { | |
this(defaultMimeType()); | |
} | |
public MappingJacksonResponseParser(MimeType responseType) { | |
this.responseType = responseType != null ? responseType : defaultMimeType(); | |
} | |
@Override | |
public String getWriterType() { | |
return WRITER; | |
} | |
@Override | |
public String getContentType() { | |
return responseType.toString(); | |
} | |
@Override | |
public String getVersion() { | |
return super.getVersion(); | |
} | |
@Override | |
public NamedList<Object> processResponse(InputStream body, String encoding) { | |
NamedList<Object> result = new NamedList<Object>(); | |
try { | |
result.add("json", StreamUtils.copyToString(body, Charset.forName(encoding))); | |
} catch (IOException e) { | |
throw new InvalidDataAccessResourceUsageException("Unable to read json from stream.", e); | |
} | |
return result; | |
} | |
@Override | |
public NamedList<Object> processResponse(Reader reader) { | |
throw new UnsupportedOperationException(); | |
} | |
private static MimeType defaultMimeType() { | |
try { | |
//solr uses text/pain as content type (see: SOLR-5272) | |
return new MimeType("text", "plain"); | |
} catch (MimeTypeParseException o_O) { | |
throw new IllegalArgumentException(o_O); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2014 the original author or authors. | |
* | |
* Licensed 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 com.example.solr; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Collections; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
import org.apache.solr.client.solrj.SolrRequest; | |
import org.apache.solr.client.solrj.SolrServer; | |
import org.apache.solr.client.solrj.SolrServerException; | |
import org.apache.solr.common.params.ModifiableSolrParams; | |
import org.apache.solr.common.params.SolrParams; | |
import org.apache.solr.common.util.ContentStream; | |
import org.apache.solr.common.util.ContentStreamBase.StringStream; | |
import com.fasterxml.jackson.databind.JsonNode; | |
/** | |
* @author Christoph Strobl | |
*/ | |
public class SolrJsonRequest extends SolrRequest { | |
private static final long serialVersionUID = 5786008418321490550L; | |
private ModifiableSolrParams params = new ModifiableSolrParams(); | |
private List<StringStream> contentStream = null; | |
public SolrJsonRequest(METHOD method, String path) { | |
super(method, path); | |
} | |
@Override | |
public SolrParams getParams() { | |
return this.params; | |
} | |
@Override | |
public Collection<ContentStream> getContentStreams() throws IOException { | |
return contentStream != null ? Collections.<ContentStream> unmodifiableCollection(contentStream) : null; | |
} | |
@Override | |
public SolrJsonResponse process(SolrServer server) throws SolrServerException, IOException { | |
long startTime = TimeUnit.MILLISECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS); | |
SolrJsonResponse res = new SolrJsonResponse(); | |
this.setResponseParser(new MappingJacksonResponseParser()); | |
res.setResponse(server.request(this)); | |
long endTime = TimeUnit.MILLISECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS); | |
res.setElapsedTime(endTime - startTime); | |
return res; | |
} | |
public void addContentToStream(JsonNode content) { | |
if (contentStream == null) { | |
this.contentStream = new ArrayList<StringStream>(); | |
} | |
contentStream.add(new StringStream(content.toString())); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2014 the original author or authors. | |
* | |
* Licensed 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 com.example.solr; | |
import org.apache.solr.client.solrj.response.SolrResponseBase; | |
import org.apache.solr.common.util.NamedList; | |
import org.springframework.dao.InvalidDataAccessResourceUsageException; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
/** | |
* @author Christoph Strobl | |
*/ | |
public class SolrJsonResponse extends SolrResponseBase { | |
private static final long serialVersionUID = 5727953031460362404L; | |
private JsonNode root; | |
@Override | |
public void setResponse(NamedList<Object> response) { | |
super.setResponse(response); | |
try { | |
root = new ObjectMapper().readTree((String) getResponse().get("json")); | |
} catch (Exception e) { | |
throw new InvalidDataAccessResourceUsageException("Unable to parse json from response.", e); | |
} | |
} | |
public String getJsonResponse() { | |
return (String) getResponse().get("json"); | |
} | |
public JsonNode getNode(String name) { | |
return root.findValue(name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment