Java: Get response from Solr in XML format - java

I am a newbie to Solr. I want to use Java to connect to my solr core and get the results back in XML format. By referring the official document, I am able to get the results in binary form. Below is my code:
public static void main(String[] args) throws SolrServerException, IOException {
String urlString = "http://localhost:8983/solr/index1/";
SolrClient solr = new HttpSolrClient.Builder(urlString).build();
SolrQuery query = new SolrQuery();
query.setQuery("*:*");
QueryResponse response = solr.query(query);
System.out.println(response.toString());
}
I also tried to research on how to get response. I found this link which says "If you want to get the raw xml response, just pick up any java HTTP Client, build the request and send it to Solr. You'll get a nice XML String.." solr response in xml format
I coded the below code, but it is not giving me response
public static void main(String[] args) throws ClientProtocolException, IOException
{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://localhost:8983/solr/index1/select?q=*:*&wt=xml");
CloseableHttpResponse response1 = httpclient.execute(httpGet);
System.out.println(response1);
}
}
Output:
HttpResponseProxy{HTTP/1.1 200 OK [Content-Type: application/xml; charset=UTF-8, Transfer-Encoding: chunked] ResponseEntityProxy{[Content-Type: application/xml; charset=UTF-8,Chunked: true]}}
On the official site https://lucene.apache.org/solr/guide/6_6/using-solrj.html, it is mentioned to use
solr.setParser(new XMLResponseParser());
to get XML response, but I am not sure how to use it since any example is not given. Any help is appreciated.
Edit:
As mentioned in John's comment, I have modified my code as:
System.out.println(EntityUtils.toString(response1.getEntity()));
But in the output, I can see some javascript which is followed by the XML output:
In Solr, the output in XML looks like this:

Not tried but it should work,
You need to initialize org.apache.solr.client.solrj.SolrClient which represents the Solr instance you want to use as follows.
import org.apache.solr.client.solrj.impl.XMLResponseParser;
String serverURL = "http://localhost:8983/solr/<core_name>";
SolrClient solr = new HttpSolrClient.Builder(serverURL).build();
solr.setParser(new XMLResponseParser());

Related

Consuming a StreamingResponseBody with Spring

I've got a simple web-service that stream a file using a StreamingResponseBody.
The definition looks like this:
#GetMapping("/files/{filename}")
public ResponseEntity<StreamingResponseBody> download(#PathVariable String filename) {
...
StreamingResponseBody responseBody = out -> {
...
}
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentLength(byteArray.length);
return new ResponseEntity(responseBody, httpHeaders, HttpStatus.OK);
}
It works well, but now, I need to consume it in a client application.
I'm using spring to consume it, but I can't find a way to read the stream and write it to a file as it flows...
I tryied using feign but it seems it doesn't support it.
I tryied using restTemplate but I can't make it work...
Does spring support streaming client side ?
Does anybody know how to do this ?
Perhaps using pure java API ?
Thanks a lot for your help !
You can use Apache Http Client (org.apache.httpcomponents:httpclient:4.5.12):
URI uri = new URIBuilder()
.setScheme(scheme)
.setHost(host)
.setPort(port)
.setPath(url)
.build();
HttpUriRequest request = RequestBuilder.get(uri).build();
try (CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse httpResponse = httpClient.execute(request);
InputStream inputStream = httpResponse.getEntity().getContent()) {
// Do with stream whatever you want, for example put it to File using FileOutputStream and 'inputStream' above.
}

Programmatically generate and retrieve BIRT Report from Web-Viewer

I've installed BIRT Web-Viewer on my server and am able to build the report with this URL:
http://hostname:port/birt/run?__report=test.rptdesign
Now I need to programmatically call this URL from my Java Code and retrieve the result as stream or file.
Is there any API for the Web-Viewer?
If not, could I just call the URL like this and extract the PDF?:
HttpClient httpClient = HttpClients.createDefault();
HttpGet postRequest = new HttpPost("http://hostname:port/birt/run");
List<NameValuePair> formData = new ArrayList<>();
formData.add(new BasicNameValuePair("__report", "test.rptdesign"));
HttpEntity entity = new UrlEncodedFormEntity(formData);
HttpResponse response = httpClient.execute(postRequest);
I found out, if I use the __format parameter with the value pdf, the response to the request is the PDF content, which is exactly what I wanted.
The standard response is a HTML, which will be returned with a second request. I'm pretty sure that response has to be retrieved with sessions.
Edit:
As requested I will post my request code. I modified it a bit, because I used some custom classes to hold configuration and the report.
public InputStream getReport() throws Exception {
StringBuilder urlBuilder = new StringBuilder()
.append("http://example.com:9080/contextRoot/run")
.append("?__report=ReportDesign.rptdesign&__format=pdf");
if (reportParameters != null) {
for (Map.Entry<String, String> parameter : reportParameters.entrySet()) {
String key = StringEscapeUtils.escapeHtml(parameter.getKey());
String value = StringEscapeUtils.escapeHtml(parameter.getValue());
urlBuilder.append('&')
.append(key);
.append('=');
.append(value);
}
}
URL requestUrl = new URL(burlBuilder.toString());
HttpURLConnection connection = (HttpURLConnection) requestUrl.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.connect();
return connection.getInputStream();
}
I also had another method write the used data as XML to the file system before I called requestUrl.openConnection(), but I think this is only necessary if you use very dynamic data like I did.

Restlet Https requests

I have successfully wrote HTTP request sending using Restlet's ClientResource:
public static String get(String url) {
ClientResource clientResource = new ClientResource(url);
Representation responseRepresentation = clientResource.get();
String response = responseRepresentation.getText();
return response;
}
Now I'm trying to write HTTPS requests GET and POST with Restlet's ClientResource but I'm not sure about the exact syntax.
Any reference and/or examples will be appreciated.

Not able to retrieve Data from Mendeley by using OAuth2 and HTTP in Java

I had an aplication that worked fine with OAuth1 on Mendeley. Since OAth1 is no more supported I have to migrate my app to OAuth2 toget the Data.
I get the token response but I cannot request any content, the program throws a NullPointerException.
I'm testing around with this sourcecode here.
I also use Apache OLTU and the Apache HTTPClient
This is the code I try to run:
static OAuthClientRequest request;
static OAuthClient oAuthClient;
static OAuthJSONAccessTokenResponse tokenResponse ;
static String CATALOG_URL="https://api-oauth2.mendeley.com/oapi/documents/groups?items=10";
request = OAuthClientRequest
.tokenLocation("https://api-oauth2.mendeley.com/oauth/token")
.setClientId(Client_ID)
.setClientSecret(Secret)
.setGrantType(GrantType.CLIENT_CREDENTIALS)
.setScope("all")
.buildBodyMessage();
System.out.println("is set up");
oAuthClient = new OAuthClient(new URLConnectionClient());
tokenResponse = oAuthClient.accessToken( request, OAuthJSONAccessTokenResponse.class);
System.out.println("token is retrieved");
HttpGet httpGet = new HttpGet(CATALOG_URL);
httpGet.setHeader("Authorization", "Bearer " + tokenResponse.getAccessToken());
//this is where the Exception is thrown
HttpResponse httpResponse = apacheHttpClient.execute(httpGet);
//
System.out.println("this is it: "+httpResponse.toString());
String responseAsString = EntityUtils.toString(httpResponse.getEntity());
System.out.println(responseAsString);
The Exception I get is:
Exception in thread "main" java.lang.NullPointerException
I'm now asking myself why this is happening.
The CATALOG_URL is from the Mendeley webside and should return the first Page of the list that contains all public groups.
I also tried different URL from the Mendeley webside.
Could there be anything wrong with the HttpGet statement?
Does anyone have any hints?
You are receiving a NullPointerException because you are using a variable (apacheHttpClient) that has not been initialised. Try doing this first
apacheHttpClient = ApacheHttpTransport.newDefaultHttpClient();

How to handle JSON with XStream in Java?

I have created a Google custom search engine and initiated the query using the HTTP GET. Now Google is returning the result as JSON format. I was just wondering how to format this JSON output into a Human readable way.
For example:
Title: The matrix
htmlTitle: "The matrix.."
I have seen XStream is recommended in many forums. But not sure how can I get this to work.
Can someone please help me with this.
Just for reference, I am giving the HTTP GET code in here:
public static void main(String[] args) throws IOException {
HttpGet httpget = new HttpGet("https://www.googleapis.com/customsearch/v1?key=AIzaSyBp_5Upf6h0QSXR8UveLs4_c6lAmGW_7B8&cx=014783642332862910131:opc1zgsvfhi&q=matrix&alt=json");
System.out.println(httpget.getURI());
ResponseHandler<String> responseHandler = new BasicResponseHandler();
HttpClient httpClient = new DefaultHttpClient();
String responseBody = httpClient.execute(httpget, responseHandler);
System.out.println(responseBody);
First you need to create a Class that maps the Goolge response, then use a code like this (from the XStream tutorial):
String json = "{\"product\":{\"name\":\"Banana\",\"id\":\"123\""
+ ",\"price\":\"23.0\"}}";
XStream xstream = new XStream(new JettisonMappedXmlDriver());
xstream.alias("product", Product.class);
Product product = (Product)xstream.fromXML(json);
System.out.println(product.getName());

Categories