This discussion is archived
6 Replies Latest reply: May 15, 2012 12:47 AM by EJP RSS

Certificate is empty

906182 Newbie
Currently Being Moderated
Good day, I'm working on Soap Web Service to call from my Web Service an other Web Service. But I have little problem, my inserted certificate is empty. I'm using latest NetBeans and Tomcat. Here is my little code, please help me with this:

Java Code:
/* 
 * To change this template, choose Tools | Templates 
 * and open the template in the editor. 
 */ 
package niis.janis; 
 
import java.io.*; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.security.cert.X509Certificate; 
import javax.servlet.ServletException; 
import javax.servlet.ServletInputStream; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import niis.wsclient.GetInfoBlock.CadastreInformationBlockRequestStructure; 
import niis.wsclient.GetInfoBlock.CadastreInformationBlockResponseStructure; 
import niis.wsclient.GetInfoBlock.GetInfoBlock; 
 
/** 
 * 
 * @author Owner 
 */
 
public class testWS extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    static final long serialVersionUID = 1L; 
   
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        CadastreInformationBlockRequestStructure req = new CadastreInformationBlockRequestStructure(); 
        req.username = "janis"; 
        req.password = "wtfn00b"; 
 
        InputStream is = null; 
        X509Certificate cert = null; 
        FileInputStream fos = null; 
        int ch; 
        StringBuilder strContent = new StringBuilder(""); 
        try { 
            File certificate = new File("C:/Users/Owner/Desktop/notartest.notary.lv.p12"); 
 
            fos = new FileInputStream(certificate); 
            while((ch = fos.read()) != -1){ 
                strContent.append((char)ch); 
            } 
            is = new ByteArrayInputStream(strContent.toString().getBytes()); 
            cert = X509Certificate.getInstance(is); // is 'cert' is null 
        } catch (Exception ex) { 
            Logger.getLogger(testWS.class.getName()).log(Level.SEVERE, null, ex); 
        } finally { 
            if (fos != null) {fos.close();} 
            if (is != null) {is.close();} 
        } 
 
        CadastreInformationBlockResponseStructure response2 = null; 
        try { 
            GetInfoBlock proxy = new GetInfoBlock(); 
            proxy.Add(cert); 
            response2 = proxy.CadastreInformationBlocks(req); 
        } catch (Exception ex){ 
            Logger.getLogger(testWS.class.getName()).log(Level.SEVERE, null, ex); 
        } 
 
         
        //response.getWriter().write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?><document xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xsi:type=\"xs:String\">"+ response2.getCadastreInformationData() +"</document>"); 
        //response.getWriter().flush(); 
    } 
 
    protected void doPost(HttpServletRequest request, CadastreInformationBlockResponseStructure response) throws ServletException, IOException { 
            ServletInputStream inStream = request.getInputStream(); 
            String xml_doc="<?xml version=\"1.0\" encoding=\"UTF-8\" ?><document xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xsi:type=\"xs:base64Binary\" content-type=\"application/xls\">Jānis Pekša</document>"; 
    } 
}
Best regards,
wtfn00b.

Edited by: EJP on 14/05/2012 16:23
Added {noformat}
{noformat} tags. Please use them.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
  • 1. Re: Certificate is empty
    EJP Guru
    Currently Being Moderated
    1. Certificates are binary data and you are saving it in a StringBuilder, which is already invalid.

    2. You are then getting the bytes out of that, wrapping a ByteArrayInputStream around that, and passing that to X509Certificate.getInstance(). You didn't need to do any of that.

    3. All you had to do was pass the FileInputStream which you already had directly to X509Certificate.getInstance().

    I will never understand why people make complex things out of simple things.
  • 2. Re: Certificate is empty
    906182 Newbie
    Currently Being Moderated
    Okey, I did like this:



    try {
    CadastreInformationBlockRequestStructure req = new CadastreInformationBlockRequestStructure();
    req.username = "login";
    req.password = "pass";

    InputStream is = null;
    X509Certificate cert = null;
    FileInputStream fos = null;
    try {
    File certificate = new File("C:/Users/Owner/Desktop/notartest.notary.lv.p12");

    fos = new FileInputStream(certificate);
    cert = X509Certificate.getInstance(fos); // still empty = null
    } catch (Exception ex) {
    ex.getStackTrace();
    } finally {
    if (fos != null) {fos.close();}
    }

    And it's still empty = null.

    Best regards,
    wtfn00b.
  • 3. Re: Certificate is empty
    EJP Guru
    Currently Being Moderated
    That method does not return null, and null does not mean 'empty'.

    I suggest you print the stack trace.
  • 4. Re: Certificate is empty
    906182 Newbie
    Currently Being Moderated
    Here you go:

    Maijs 14, 2012 4:24:13 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
    INFO: Root resource classes found:
    class niis.graph.GenericResource
    Maijs 14, 2012 4:24:13 PM com.sun.jersey.api.core.ScanningResourceConfig init
    INFO: No provider classes found.
    Maijs 14, 2012 4:24:13 PM com.sun.jersey.spi.spring.container.servlet.SpringServlet getContext
    INFO: Using default applicationContext
    Maijs 14, 2012 4:24:13 PM com.sun.jersey.spi.spring.container.servlet.SpringServlet initiate
    SEVERE: Exception occurred when intialization
    java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?
         at org.springframework.web.context.support.WebApplicationContextUtils.getRequiredWebApplicationContext(WebApplicationContextUtils.java:70)
         at com.sun.jersey.spi.spring.container.servlet.SpringServlet.getDefaultContext(SpringServlet.java:153)
         at com.sun.jersey.spi.spring.container.servlet.SpringServlet.getContext(SpringServlet.java:137)
         at com.sun.jersey.spi.spring.container.servlet.SpringServlet.initiate(SpringServlet.java:117)
         at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318)
         at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
         at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
         at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373)
         at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556)
         at javax.servlet.GenericServlet.init(GenericServlet.java:160)
         at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1228)
         at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1147)
         at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1043)
         at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4957)
         at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5284)
         at org.apache.catalina.core.StandardContext$3.call(StandardContext.java:5279)
         at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:334)
         at java.util.concurrent.FutureTask.run(FutureTask.java:166)
         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
         at java.lang.Thread.run(Thread.java:722)

    Maijs 14, 2012 4:24:13 PM org.apache.catalina.util.LifecycleBase start
    INFO: The start() method was called on component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[niis_services]] after start() had already been called. The second call will be ignored.
    log4j:WARN No appenders could be found for logger (org.springframework.web.context.request.RequestContextListener).
    log4j:WARN Please initialize the log4j system properly.
  • 5. Re: Certificate is empty
    906182 Newbie
    Currently Being Moderated
    Cert is null.
  • 6. Re: Certificate is empty
    EJP Guru
    Currently Being Moderated
    That exception is not thrown by that code.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points