Thursday, February 14, 2013

Java - String comparison

Does it give you a bellyache?
String gg1 = "goldengate";
String gg2 = "goldengate"; //s2 is stored in a string pool.
String gg3 = new String("goldengate");
System.out.println(gg1==gg2); //is true, because they share
//the same memory addr
System.out.println(gg1==gg3); //false, because of 'new'
view raw StringPool.java hosted with ❤ by GitHub
Never mind, you can compare them like this-

System.out.println(gg1.compareTo(gg3));

Saturday, February 9, 2013

Url encode an HTTP GET Solr request and parse json using gson java library

Make a request to the SOLR webserver and parse the json string to Java classes using the code snippets below.
The JSON to be parsed-
{
"responseHeader": {
"status": 0,
"QTime": 1,
"params": {
"q": "searchterm1 OR searchterm2",
"q.op": "AND",
"wt": "json",
"rows": "10000",
"version": "2.2"
}
},
"response": {
"numFound": 2,
"start": 0,
"docs": [
{
"name": "searchterm1",
"id": "1"
},
{
"name": "searchterm2",
"id": "2"
}
]
}
}
view raw Solr.json hosted with ❤ by GitHub
The corresponding Java classes-
public class Documents {
private String name;
private String id;
@Override
public String toString() {
return id+":"+name;
}
public String getName() {
return name;
}
public String getID() {
return id;
}
}
public class Results {
private String numFound;
private ArrayList<Documents> docs;
@Override
public String toString() {
return numFound +" - " + docs.toString();
}
public ArrayList<Documents> getDocs(){
return docs;
}
public String getNumFound() {
return numFound;
}
}
public class Response {
private Results response;
@Override
public String toString() {
return response.toString();
}
}
view raw Classes.java hosted with ❤ by GitHub
Code snippet to make a web server call and parse using gson-
String REQUEST_URL = "properties/select/?q=" + criteria
+ "&q.op=AND&rows=10&version=2.2&wt=json";
try {
URL url = new URL(webServer + "properties/select/?q="
+ URLEncoder.encode(criteria, "UTF-8")
+ "&q.op=AND&rows=10&version=2.2&wt=json");
BufferedReader in = new BufferedReader(
new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
jsonString = inputLine;
}
in.close();
//Create gson
Gson gson = new GsonBuilder().create();
Response r = gson.fromJson(jsonString, Response.class);
LOG.info("MyGSON:" + r.toString());
} catch (IOException e) {
e.getMessage();
e.printStackTrace();
} catch (Exception e) {
e.getMessage();
e.printStackTrace();
}
view raw ParseGson.java hosted with ❤ by GitHub


The Java classes must have the same variable names as the Json's keys. Notice how nested Json elements are handled.

Wednesday, February 6, 2013

Errors running builder 'Google WebApp Project Validator'

Error:

Problems occurred building the selected resources.
Errors running builder 'Java Builder' on project 'curve_app'.
com/google/gdt/eclipse/suite/preferences/GdtPreferences
Errors running builder 'Google WebApp Project Validator' on project 'curve_app'.
java.lang.NullPointerException

Workaround solution:

Close all projects, close eclipse.

Start eclipse using, "eclipse -clean" option.

You can try repeating the process 2-3 times,  if it does not work first time.

Not sure of the exact reason why this appears.


Tuesday, February 5, 2013

Highcharts Phantomjs Export - TypeError: 'undefined' is not an object

Generating a png file from config-

phantomjs-1.8.1/bin/phantomjs highcharts-convert.js -infile config.json  -outfile out1.png -width 300 -scale 2.5 -constr Chart -callback callback.js

Error-
TypeError: 'undefined' is not an object (evaluating 'svgElem.getAttribute')
phantomjs://webpage.evaluate():20
phantomjs://webpage.evaluate():23
phantomjs://webpage.evaluate():23
TypeError: 'null' is not an object (evaluating 'svg.width')
highcharts-convert.js:89
highcharts-convert.js:322
:/modules/webpage.js:277
view raw gistfile1.js hosted with ❤ by GitHub

Things to check-
1. The following 3 files should be there in same folder or you have to set the paths to these here-
var config = {
                /* define locations of mandatory javascript files */
                HIGHCHARTS: 'highstock.js',
                HIGHCHARTS_MORE: 'highcharts-more',
                JQUERY: 'jquery-1.8.2.min.js'
        },

2. The 'infile' parameter should have an extension of .json, because that is what it uses to decide between svg input and a configuration json.

Friday, February 1, 2013

GWT - Highchart export request - Read multipart/form-data parameters

Getting multipart-form-data request parameters in java servlet-

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException {
if (!ServletFileUpload.isMultipartContent(req)) {
throw new ServletException("Not a file upload request");
}
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iter;
iter = upload.getItemIterator(req);
while (iter.hasNext()) {
FileItemStream item = iter.next();
InputStream stream = item.openStream();
if (item.isFormField()) {
if (item.getFieldName().equalsIgnoreCase("svg")) {
svg = Streams.asString(stream);
}
...
...
}
}
}