Posts

Showing posts from 2014

Generate Random file name in Java

You can create a random string for a file name using following code snippet. public static String getRandomString(String fileName) { char fileNameArray[] = fileName.toCharArray(); int fileNameLen = fileNameArray.length; int c = 'A'; int r1 = 0, z=0; StringBuilder pw = new StringBuilder(""); for (int j = 0; j < 25; j++) { r1 = (int) (Math.random() * 4); switch (r1) { case 0: c = '0' + (int) (Math.random() * 9); if (c>'9'){ z = reset(z,fileNameLen); c = fileNameArray[z]; } break; case 1: c = 'a' + (int) (Math.random() * 25); if (c<'a' && c>'z'){ z = reset(z,fileNameLen); c = fileNameArray[z]; } break; case 2: c = 'A' + (int) (Math.random() * 25); if (c<'A' && c>'Z'){ z = reset(z,fileNameLen); c = fileNameArray[z]; } break; } pw.ap...

Tomcat 7: Set context path for your application in tomcat 7

Set context path for your application in tomcat 7 If you want to change context path for your application, you can do by changing {catalina_base}/conf/server.xml file. Suppose, you have deployed your application in tomcat 7,               http://localhost:8080/MyApp You want to set context path to newMyApp, Modify, host element in server.xml under tomcat/conf <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="false"> .... ... . <Context path="/ newMyApp "  docBase="/ MyApp " reloadable="true">  </Context> …  </Host> For detailed information, visit  http://tomcat.apache.org/tomcat-7.0-doc/deployer-howto.html#A_word_on_Contexts

Maven :: Include servlet-api only at local maven install not during building war

Include  servlet-api  only at local maven install not during building war Add scope element with value as ‘Provided’. Now maven will not include this jar in lib whiling building war file so that you can deploy it on tomcat.  <dependency>  <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> <scope>provided</scope> </dependency> For all scopes in maven, please visit  http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

JAXB:GlobalBinding :: cvc-complex-type.2.4.b: The content of element 'jaxb:globalBindings' is not complete

Error ::  cvc-complex-type.2.4.b: The content of element 'jaxb:globalBindings' is not complete. One of '{"http://  java.sun.com/xml/ns/jaxb":javaType, "http://java.sun.com/xml/ns/jaxb":serializable, WC[##other:"http://  java.sun.com/xml/ns/jaxb"]}' is expected. Solution :   Check  <jaxb:globalBindings> tag in your bindings.xml, you may be missing some required tag (If you are using latest eclipse like eclipse-Luna or latest).  Add this tag if missing, <jaxb:globalBindings>             <jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime"  parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"  printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />             <xjc:noValidator />     </jaxb:globalBindings>  

How to match date filed in Google Datastore

How to match date filed in Google Datastore Suppose you have an 'Employee' entity with updatedOn field (Date or timestamp type). You can use Date function to parse date string and filter your data. In GQL, you can use:   SELECT * FROM EmployeeObj where updatedOn > Date ('2014-09-25') Similarly in objectify, you can use filter           Objectify obfy = OfyService.ofy();           List<EmployeeObj> resultList = obfy.load().type(EmployeeObj.class) .filter("updatedOn > ", Date('2014-09-25'))                                                  .list();

Upload csv file at Google Cloud Storage using java

First do all required pre requiste as per google doc https://cloud.google.com/appengine/docs/java/googlestorage/ Now see the sample code for a csv file uploader using latest api public String uploadFileUsingGCSClient(String csvDate, String fileName, String dirName,String bucketName) throws IOException {  // init the bucket access     GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());     GcsFilename filename = new GcsFilename(bucketName+"/"+dirName, fileName);     GcsFileOptions fileOptions = new GcsFileOptions.Builder()    .mimeType("application/CSV")       .acl("public-read")    //.addUserMetadata("myfield1", "my field value")    .build();     GcsOutputChannel outputChannel = gcsService.createOrReplace(filename, fileOptions);          // write file using this st...

Struts 2 - struts tag with boolean property

Struts 2 - struts tag <s:if test= "booleanVaue" > with boolean property Always remember two points -- 1) Check your bean (.java) file for getter, setter for boolean type value   private boolean isTest;   private boolean hasCheck;    public boolean isTest() {         return isTest;     }     public void setTest(boolean isTest) {         this.isTest= isTest;     }    public boolean isHasCheck() {         return hasCheck;     }     public void setHasCheck(boolean hasCheck) {         this.hasCheck= hasCheck;     } ------------------------------------------------------------------------- On a jsp page if you are using these two variables in a <s:if> conditions <s:if test=" isTest "> // This will not work, will always ret...

Can not connect to git using ssh (Permissions 0644 for '/root/.ssh/id_rsa' are too open.

Permissions 0644 for '/root/.ssh/id_rsa' are too open. It is required that your private key files are NOT accessible by others. ------------------------ root@media-Vostro-1540:~/.ssh# ssh -T git@github.com @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @         WARNING: UNPROTECTED PRIVATE KEY FILE!          @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Permissions 0644 for '/root/.ssh/id_rsa' are too open. It is required that your private key files are NOT accessible by others. This private key will be ignored. bad permissions: ignore key: /root/.ssh/id_rsa Permission denied (publickey). Solution : First check the permission on id_rsa directory root@media-Vostro-1540:~/.ssh# ls -lrt total 12 -rw-r--r-- 1 root root 2160 Jun  4 15:05 known_hosts -rw-r--r-- 1 root root 1766 Jun  4 15:12 id_rsa -rw-r--r-- 1 root root  415 Jun  4 15:12 id_rsa.pub Ch...

appengine:devserver_stop not working in Windows 8/eclipse

If you have added appengine:devserver_stop in eclipse as a debug goal in maven goals and still you are not able to stop the server then you can manually close the server on given port. Let's take an example that your server is running on 8080 that you want to stop. 1) Open command line prompt:    netstat -a -n -o You will get following output......  Proto  Local Address          Foreign Address        State           PID  TCP    0.0.0.0:135            0.0.0.0:0              LISTENING       700  TCP    0.0.0.0:445            0.0.0.0:0         ...

DFP API works fine at localhost but failed Google App engine (access denied ("java.lang.RuntimePermission" "getClassLoader")

DFP API works fine at localhost but failed Google App engine (access denied ("java.lang.RuntimePermission" "getClassLoader") --------------------------------------------------------------------------  com.google.api.ads.common.lib.exception.ServiceException: Unexpected Exception.     com.google.api.ads.common.lib.soap.jaxws.JaxWsHandler.createSoapClient(JaxWsHandler.java:210)     com.google.api.ads.common.lib.soap.jaxws.JaxWsHandler.createSoapClient(JaxWsHandler.java:1)     com.google.api.ads.common.lib.factory.helper.BaseAdsServiceClientFactoryHelper.createSoapClient(BaseAdsServiceClientFactoryHelper.java:106)     com.google.api.ads.common.lib.factory.helper.BaseAdsServiceClientFactoryHelper.createAdsServiceClient(BaseAdsServiceClientFactoryHelper.java:71)     com.google.api.ads.common.lib.factory.AdsServiceClientFactory.getServiceClient(AdsServiceClientFactory.java:73)     com.google.api...

Writing a zip file on Google App engine

package com.lin.web.servlet; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.List; import java.util.logging.Logger; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; import com.google.api.client.http.ByteArrayContent; import com.google.api.client.http.GenericUrl; import com.google.api.client.http.HttpRequest; import com.google.api.client.http.HttpRequestFactory; import com.google.api.client.http.HttpResponse; import com.google.api.client.http.HttpTransport; import com.google.api.cl...