Search code examples
javarestx509certificatejersey-1.0media-type

How to return x509 certificate in Jersey ws


I am able to generate a certificate correctly but I am not able to return back to the client.

I am getting the following error in returning back to the client from my REST resource:

SEVERE: A message body writer for Java class sun.security.x509.X509CertImpl, and Java type class java.security.cert.X509Certificate, and MIME media type application/x-x509-user-cert was not found

The client code is correct since it works with other services.

REST resource:

@POST
    @XmlElement(name = "data")
    @Path("/,system/newCert")
    @Consumes({ "application/x-www-form-urlencoded" })
    @Produces({ "application/x-x509-user-cert" })
    public X509Certificate newCert(@FormParam("username") String uname,
            @FormParam("name") String CommonName,
            @FormParam("email") String email,
            @FormParam("pictureURL") String pURL,
            @FormParam("spkac") String spkacData) {

        String webId = "https://" + uname + "/profile/card#me";

        BouncyKeygenService keygen = new BouncyKeygenService();

        try {

            keygen.initialize();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Certificate cert = null;

        if (!spkacData.isEmpty()) {
            cert = keygen.createFromSpkac(spkacData);
        }

        cert.setSubjectCommonName(CommonName);
        cert.setSubjectWebID(webId);
        cert.addDurationInDays("36135"); // valid for 99 years
        cert.startEarlier("12"); 

        CertSerialisation certByte = null;
        X509Certificate x509 = null;
        try {
            certByte = cert.getSerialisation();

            ByteArrayOutputStream bout = new ByteArrayOutputStream(
                    certByte.getLength());
            certByte.writeTo(bout);
            CertificateFactory cf = CertificateFactory.getInstance("X.509");
            x509 = (X509Certificate) cf
                    .generateCertificate(new ByteArrayInputStream(bout
                            .toByteArray()));
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Date notAfter = x509.getNotAfter();

        return x509;
    }

The thing is that there is no MediaType x509 Certificate in Java, what is suppose to be returned?

Any help would be appreciated.

Thanks.


Solution

  • Solved!

    For future references; the return type cannot be a certificate, but wrap it in a MessageBodyWriter as Suggested from the comment on the question.

    This are the additional fixes to code in the question:

    public InputStream newCert(@FormParam("username") String uname,
            @FormParam("name") String CommonName,
            @FormParam("email") String email,
            @FormParam("pictureURL") String pURL,
            @FormParam("spkac") String spkacData) {
    
      ...
    
      ByteArrayOutputStream bout = null;
      InputStream is = null;
    
      ...
    
      try {...}
      catch {...}
    
      is = new ByteArrayInputStream (bout.toByteArray());   
      return is;
    
    }