Pokazywanie postów oznaczonych etykietą java. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą java. Pokaż wszystkie posty

poniedziałek, 31 października 2011

GTP prime library for Java (3GPP 32.295)

Recently I needed to have a client application for a GTP' external system. I did some research on the web but unfortunately did not find anything usable for me (found one java library on kenai.com but the implementation is in very early phase, the other was open-cgf written in Erlang for which I found two obstacles the language + crash dumps when it was starting up on my Ubuntu Natty 64-bit box). Having some development background from the past and having a look on the 3GPP 32.295 specification I decided that it is not a big amount of effort to implement it. I think this is a good opportunity to learn the protocol and additionally check the JBoss Netty framework - which seems to be a perfect match for such a solution. Indeed writing a client/server application using JBoss Netty is pretty straightforward (I will try to provide more details in another article) - it mainly about writing proper encoder/decoder and handlers.

The outcome can be found in the google code under the link: http://code.google.com/p/gtpprime/.

środa, 25 maja 2011

Encryption/decryption of data in Java

Recently I came to the problem that the one of my colleagues utilities had to store some sensitive data (password) in it's configuration file (property file). Storing it it plain text would be a serious security flaw and therefore we had to find a way to store the password in an encrypted form using some key. Fortunately it is not that difficult in java - the apache common codec library becomes useful here. It provides much more functionality but what we used is the base64 encoding/decoding.

Below I enclose the example logic for encrypting the password:
    public byte[] encryptPassword(String pass, String key) throws Exception {
        DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secKey = keyFactory.generateSecret(keySpec);
        byte[] cleartext = pass.getBytes("UTF8");     

        Cipher cipher = Cipher.getInstance("DES"); // cipher is not thread safe
        cipher.init(Cipher.ENCRYPT_MODE, secKey);
        return Base64.encodeBase64(cipher.doFinal(cleartext));
    }


And to decrypt the password one can use the following method:
    public byte[] decryptPassword(String encryptedPass, String key) throws Exception {
        DESKeySpec keySpec = new DESKeySpec(key.getBytes("UTF8"));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey secKey = keyFactory.generateSecret(keySpec);

        byte[] encrypedPwdBytes = Base64.decodeBase64(encryptedPass);

        Cipher cipher = Cipher.getInstance("DES");// cipher is not thread safe
        cipher.init(Cipher.DECRYPT_MODE, secKey);
        return (cipher.doFinal(encrypedPwdBytes));
       
    }


I hope that these code snippets would be helpful also for you.

czwartek, 26 sierpnia 2010

JEE : JBoss 5.1.0 GA and JDK 1.6

The recommended JDK for the standard downloadable JBoss 5.1.0 GA binaries is JDK 5. So if you have strange exception while invoking the SOAP WS (as I got - enclosed below) you probably forgot to check the releaseNotes and did not do the follow the guidelines from there:

from 5.0.0.GA
JBossAS 5.0.0.GA can be compiled with both Java5 & Java6. The Java5 compiled binary is our primary/recommended binary distribution. It has undergone rigorous testing and can run under both a Java 5 and a Java 6 runtime. When running under Java 6 you need to manually copy the following libraries from the JBOSS_HOME/client directory to the JBOSS_HOME/lib/endorsed directory, so that the JAX-WS 2.0 apis supported by JBossWS are used:
jbossws-native-saaj.jar
jbossws-native-jaxrpc.jar
jbossws-native-jaxws.jar
jbossws-native-jaxws-ext.jar


The files are locatedt in the common/lib/ subdirectory. After copying them to lib/endorsed and of course restarting JBoss the presented below exception was gone:

2010-08-26 23:11:34,073 ERROR [org.jboss.ws.core.jaxws.SOAPFaultHelperJAXWS] (http-127.0.0.1-8080-2) SOAP request exception
java.lang.UnsupportedOperationException: setProperty must be overridden by all subclasses of SOAPMessage

at javax.xml.soap.SOAPMessage.setProperty(Unknown Source)
at org.jboss.ws.core.soap.SOAPMessageImpl.(SOAPMessageImpl.java:87)
at org.jboss.ws.core.soap.MessageFactoryImpl.createMessage(MessageFactoryImpl.java:215)
at org.jboss.ws.core.soap.MessageFactoryImpl.createMessage(MessageFactoryImpl.java:193)
at org.jboss.wsf.stack.jbws.RequestHandlerImpl.processRequest(RequestHandlerImpl.java:455)
at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleRequest(RequestHandlerImpl.java:295)
at org.jboss.wsf.stack.jbws.RequestHandlerImpl.doPost(RequestHandlerImpl.java:205)
at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleHttpRequest(RequestHandlerImpl.java:131)
at org.jboss.wsf.common.servlet.AbstractEndpointServlet.service(AbstractEndpointServlet.java:85)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)


There is also a jdk6 version available for download on the sourceforge.

Java - JSR181 and jaxws-api maven problem

Today I tried to create a WS that would be used for a purpose of mass provisioning operations. The ideas was to transfer the file as the attachement to the normal SOAP message (to avoid xml tag overhead) - in my case I decided to check MTOM (Message Transmission Optimization Mechanism) and JBoss as the runtime environment (version 5.1.0 with Tomcat web container). I found a manual describing howto enable the MTOM for a WS in JBoss and decided to start the implementation. For that purpose I used EclipseIDE + maven for building the packages and here the first problem occured namely it seems to be a dependency problem between jsr181-api (WebService, SOAPBinding annotations) and jaxws-api (BindingType annotation). In my case I was able to use the first one in version 1.0 MR (MaintenanceRelease) while the jaxws-api in version 2.1-1 required the 1.0 version, which led to the following problem:

Missing artifact javax.jws:jsr181:jar:1.0:compile


The problem basically is that there is no packaging available for this artifact - probably due to some licensing restrictions.

1) Fortunately there is a workaround that helped in my case - to use the geronimo-jaxws_2.1_spec instead of jaxws-api :) And it solved my compilation problems.

2) I found another solution to the previously described problem:
- I added the http://download.java.net/maven/2 repository
- I increased the version of jaxws-api 2.2.1

Moreover the version 2.2.1 comes with JSR224 annotations, which contain the @MTOM annotation for web services supporting the MTOM.