Thursday, September 5, 2019

How to convert HttpResponse to String in Java

Following code snippet explains how to convert HttpResponse object to String in Java

Steps

  1. Call httpclient.execute()
  2. Get the InputStream from the response
  3. Convert the InputStream to ByteArrayOutputStream
  4. Convert the ByteArrayOutputStream to String
  5. Close the request and InputStream


private String executeAndGetResponse(HttpRequestBase request)
      throws IOException, ClientProtocolException, Exception, UnsupportedEncodingException {
    HttpResponse response = httpClient.execute(request);
    InputStream inputStream = response.getEntity().getContent();
    ByteArrayOutputStream result = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream.read(buffer)) != -1) {
      result.write(buffer, 0, length);
    }
    String strRes = result.toString(StandardCharsets.UTF_8.name());
   
    request.releaseConnection();
    inputStream.close();
    return strRes;
  }

Tuesday, September 3, 2019

Jackson Object Mapper to conver String to JsonNode

Following are some of the code snippet for Jackson Object Mapper
Convert String to JsonNode Object
String data;
JsonNode jsonNode = getObjectMapper().readTree(data);
Convert an Object to JSON String
String jsonString = getObjectMapper().writeValueAsString(object);
Convert InputStream to JsonNode Object
InputStream inputStream;
JsonNode jsonNode = getObjectMapper().readTree(inputStream);

Thursday, August 22, 2019

How to check what version of JQuery is loaded in browser

Sometimes we might want to know what version of jQuery a particular site is using. It is not always possible to know the version of jQuery we are using through the jQuery File name or reading the content of the files, specially when the jQuery is minimized.

Following snippet can tell us the version of jQuery loaded by a site.

if (typeof jQuery != 'undefined') {  
    // jQuery is loaded => print the version
    alert(jQuery.fn.jquery);
}

Simply go to the console of the browser, once the webpage is loaded, and then type. the above command on the console. It should display the version as an Alert.

Tuesday, August 20, 2019

How to generate sitemap for Blogs hosted on Blogger

Blogger comes with some goodies, like the atom feed URL, which can be used to generate sitemap for bloggers.
  1. Sign in to Google Search Console.
  2. Select the Blog from the left hand side.
  3. Click on Sitemaps link.
  4. At the top of the page with title "Add a new sitemap"
  5. Add the below code in the text field.
atom.xml?redirect=false&start-index=1&max-results=500
Google Search Console

Example : 

https://webdevpedia.blogspot.com/atom.xml?redirect=false&start-index=1&max-results=500
Press “Submit Sitemap” button.

Tuesday, August 13, 2019

Handle 409 conflict document in couchdb

Problem Use case:

 CouchDB Error 409 conflict document.

Exception Details [Truncated]:
CouchDB is throwing error 409 conflict while calling REST API to update data.
CouchDB will throw 409 conflict document exception if the payload is not proper. Following are some of the cases.
"_id" : This attribute is present, but the "_rev" attribute is not present or is wrong.
CouchDB uses both the "_id" and "_rev" attribute while updating a Document. Every time we update a document the "_rev" attribute value gets updated.

Solution:
If we want to update data, then make sure both "_id" and "_rev" attributes are carrying proper values respectively.


Referance Links related to CouchDB:
https://webdevpedia.blogspot.com/2019/07/installing-couchdb-on-gcp-compute-engine.html

CouchDB Error 412:Precondition Failed


Problem Use case:
 CouchDB Error 412:Precondition Failed

Exception Details [Truncated]:
CouchDB is throwing error while calling REST API to update data.

Solution:
CouchDB will throw 412:Precondition Failed exception if the payload does not contain the unique identifier "_id" attribute or its value is missing. Check the payload for PUT or POST to make sure the "_id" attribute is not missing.


Referance Links related to CouchDB:
https://webdevpedia.blogspot.com/2019/07/installing-couchdb-on-gcp-compute-engine.html

Tuesday, April 30, 2019

Starting Spring Boot Application in Debug mode

Start the Spring boot application with the following jvm argument.

mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"