Thursday, September 29, 2011

Escaping Special Characters And White Space in SOLR in java

Escape Special characters in SOLR before Query
here is the function to escape the culprits

public static String escapeQueryCulprits(String s)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
// These characters are part of the query syntax and must be escaped
if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':'
|| c == '^' || c == '[' || c == ']' || c == '\"' || c == '{' || c == '}' || c == '~'
|| c == '*' || c == '?' || c == '|' || c == '&' || c == ';'
)
{
sb.append('\\');
}
if(Character.isWhitespace(c))
{
sb.append(" \\ ");
}
sb.append(c);
}
return sb.toString();
}

Wednesday, September 21, 2011

A partial block (3 of 4 bytes) was dropped in Base64 String


When you face such a issue just use

Flex :
encodeURIComponent(String) in flex

Java :
URIUtil.encodeQuery(String)

Before posting the string in Browser as "=" and "&" in the String create issue while decoding String.

Thursday, June 30, 2011

Response Header with MVC interceptors in Spring

Response Header's in Spring can be easily set using org.springframework.web.servlet.support.WebContentGenerator as this is a abstract class so we use a direct known sub class for the same which is org.springframework.web.servlet.mvc.WebContentInterceptor , this Interceptor checks and prepares request and response. Checks for supported methods and a required session, and applies the specified number of cache seconds.

Here is the Example, this should be under beans tag in your server-config.xml file

<mvc:annotation-driven />
<mvc:interceptors>
<bean id="webContentInterceptor" class="org.springframework.web.servlet.mvc.WebContentInterceptor">
<property name="cacheSeconds" value="120"/>
<property name="useExpiresHeader" value="true"/>
<property name="useCacheControlHeader" value="true"/>
<property name="requireSession" value="false"/>
<property name="useCacheControlNoStore" value="true" />
<property name="cacheMappings">
<props>
<prop key="/**/*.html">2000</prop>
<prop key="/**/*.css">500000</prop>
<prop key="/**/*.js">2592000</prop>
</props>
</property>
</bean>
</mvc:interceptors>

In CacheMapping attribute we can specify the cache time for different file types, as this increases the preformance of application.

Gzip in Apache Tomcat - Faster Responses with Compression


Today's Browser have capability to support gzip content and uncompressed the content to plain text. The data comes from server to client in a compressed form with increases the performance many times as less data get transfered on network.

Just go to Tomcat/conf/Server.xml file and replace the default node

<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" >

with follwing

<Connector port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true"
compression="on"
compressionMinSize="2048"
noCompressionUserAgents="gozilla, traviata"
compressableMimeType="text/html,text/xml,application/json">


Make sure you add the MIME Type which you want to add compression as follows
compressableMimeType="text/html,text/xml,application/json"

# Tomcat handles the compression for the supporting brorwers and do not compress response in case the browser is from monolithic age :)

- Varun Rathore

Tuesday, June 21, 2011

PJson in Spring using JacksonJson (Cross Domain Issues)

We can create pjson (json with padding) to achieve cross domain java-script call which is very important if data is coming from other domain.

Here is what i did to get pjson from object.
I created a class MappingJacksonJsonpView extended it by AbstractView, now here is the trick
I overrided a method renderMergedOutputModel as follows:

@Override
protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception
{
Object value = filterModel(model);
JsonGenerator generator = objectMapper.getJsonFactory().createJsonGenerator(response.getOutputStream(), encoding);
String callback = request.getParameter("jsoncallback");
prefixJson = false;
if (callback!=null)
{
prefixJson = true;
}
if (prefixJson)
{
generator.writeRaw(callback + "(");
}
objectMapper.writeValue(generator, value);
generator.flush();

if (prefixJson)
{
generator.writeRaw(");");
generator.flush();
}
}

and make sure you put entry in your servlet.xml file
<property name="defaultViews">
<list>
<bean class="com.views.utility.MappingJacksonJsonpView" />

Thursday, May 26, 2011

AS3 Signals - Faster Messages in AS3

AS3 signals are free , very fast and relaible messaging tool, i have been using them from last 6 months , below are some features of Signals which are more superior than Events

Dowload it from https://github.com/robertpenner/as3-signals/wiki/

Signal's Salient Features

Remove all event listeners : signal.removeAll();

Retrieve the number of listeners : signal.numListeners

Listeners can be added for a one-time call and removed automatically on dispatch:

signal.addOnce(theListener); // result: signal has one listener
signal.dispatch(theEvent); // result: theListener is called, signal now has no listeners

A Signal can be initialized with value classes that will validate value objects on dispatch (optional):

// A Signal that will dispatch a String and an integer:
progress = new Signal(String, int);
//later:
progress.dispatch(); // will throw ArgumentError
progress.dispatch('The Answer'); // will throw ArgumentError
progress.dispatch('The Answer', 42.5); // will throw ArgumentError
progress.dispatch('The Answer', 42); // will succeed


Varun Rathore

Wednesday, May 25, 2011

Adobe AIR - Opening Main Appllication from Child Window

Hi, I was trying to make the Main Application open and dock from a child window, here is the simple code to do this, it check if the Main application is not docked, if not if put that to front.

if(FlexGlobals.topLevelApplication.stage.nativeWindow.visible == false)
{
FlexGlobals.topLevelApplication.stage.nativeWindow.visible = true;
FlexGlobals.topLevelApplication.stage.nativeWindow.orderToFront();

}
FlexGlobals.topLevelApplication.stage.nativeWindow.orderToFront();


-Varun Rathore

Tuesday, May 17, 2011

Increasing Java Heap Size - Posting Bigger Files in Apache SOLR

In some cases we need to post bigger xml file in SOLR server for indexing, if you post the file directly you get OutOfMemoryExceptions to avoid such failure, we need to change the max memory size that the heap can reach for the JVM

Here is the Command by which we can increase the heap size
java -Xms128m -Xmx8192m - jar

So in order to post bigger files now we use
java -Xms128m -Xmx8192m - jar post.jar <*.xml>

The -Xmx argument defines the max memory size that the heap can reach for JVM.
The -Xms argument sets the initial heap memory size for the JVM.

-Varun Rathore

Apache SOLR , Deleting-Removing all data at one Go

If we need to remove/delete the indexed data from SOLR server here is a simple one line command which does the magic for you.

java -Ddata=args -jar post.jar "<delete><query>*:*</query></delete>"

-Varun Rathore

About Me