AnsweredAssumed Answered

custom action for secure timestamp - error using BouncyCastle

Question asked by campos on Jul 9, 2015
Hi all, I'm a newbie to developing to alfresco so here is my question.
I'm developing a custom action in alfresco wich goal is to add an aspect of a Secure Time Stamp of a document when a button is actioned on share.  Here's my code for "executeImpl" method

<code language="java" start="107">
@Override
    protected void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef)
    {
        Security.addProvider(new BouncyCastleProvider());


        if (nodeService.exists(actionedUponNodeRef))
        {

            FileInfo fileInfo = null;

            fileInfo = fileFolderService.getFileInfo(actionedUponNodeRef);
            String fileName = (String) nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_NAME); // == fileInfo.getName();

            // convert node content to file
            File file = new File(fileName);
            contentReader = contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT);

            contentReader.getContent(file);

            String tsaUrl = "http://ts.cartaodecidadao.pt/tsa/server";
            OutputStream out = null;
            HttpURLConnection con = null;
            MessageDigest hash;
            InputStream is = null;

            try
            {
                hash = MessageDigest.getInstance("SHA1");
                try
                {
                    is = new BufferedInputStream(new FileInputStream(file));
                    final byte[] buffer = new byte[1024];
                    for (int i = 0; is.read(buffer) != -1;)
                    {
                        hash.update(buffer, 0, i);
                    }

                } catch (FileNotFoundException e)
                {
                    e.printStackTrace();
                }

                TimeStampRequestGenerator timeStampRequestGenerator = new TimeStampRequestGenerator();

                /*FIXME */
                ASN1ObjectIdentifier algorithm = TSPAlgorithms.SHA1;
                TimeStampRequest timeStampRequest = timeStampRequestGenerator.generate(algorithm, hash.digest());
                byte request[] = timeStampRequest.getEncoded();

                URL url = new URL(tsaUrl);
                con = (HttpURLConnection) url.openConnection();
                con.setDoOutput(true);
                con.setDoInput(true);
                con.setRequestMethod("POST");
                con.setRequestProperty("Content-type", "application/timestamp-query");
                con.setRequestProperty("Content-length", String.valueOf(request.length));
                out = con.getOutputStream();
                out.write(request);
                out.flush();
                out.close();

                if (con.getResponseCode() != HttpURLConnection.HTTP_OK)
                {

                    throw new IOException("Received HTTP error: "
                            + con.getResponseCode() + " - "
                            + con.getResponseMessage());
                }

                InputStream in = con.getInputStream();
                TimeStampResp resp = TimeStampResp.getInstance(new ASN1InputStream(in).readObject());
                TimeStampResponse response = new TimeStampResponse(resp);

                if (response.getFailInfo() == null)
                {

                    Date date = response.getTimeStampToken()
                            .getTimeStampInfo().getGenTime();
                    String tsa = response.getTimeStampToken()
                            .getTimeStampInfo().getTsa().toString()
                            + "\n";
                    String serialNumber = response.getTimeStampToken()
                            .getTimeStampInfo().getSerialNumber()
                            .toString()
                            + "\n";
                    String policy = response.getTimeStampToken()
                            .getTimeStampInfo().getPolicy().getId()
                            + "\n";

                    if (date.equals(null) || serialNumber.equals(null))
                    {

                        return; // abort process because date and serial are
                        // mandatory elements
                    } else
                    {
                        Map<QName, Serializable> aspectValues = new HashMap<>();

                        aspectValues.put(TimeStampModel.PROP_GENTIME, date);


                        aspectValues.put(TimeStampModel.PROP_TSA, tsa);

                        aspectValues.put(TimeStampModel.PROP_SERIALNUMBER,
                                serialNumber);

                        aspectValues.put(TimeStampModel.PROP_POLICY, policy);

                        nodeService.addAspect(actionedUponNodeRef, TimeStampModel.ASPECT_STAMPED, aspectValues);

                    }
                } else
                {
                    System.out.println("———-return 1");
                    return;
                }

            } catch (Exception e)
            {
                System.out.println("———-exception");
                e.printStackTrace();
            }

        } else{
            System.out.println("———-return 2");
            return;
        }

    }
</code>

I can build the amp succesfully but when trying the action i get the following error on line 153 (it's the one presented on this code which represents 233 on my class). The log is on the attached file



Can anyone knows what's happening? I already serached for "java.lang.NoClassDefFoundError: Could not initialize class org.bouncycastle.tsp.TSPAlgorithms" but unsuccessfully.
I'm using version 1.52 of BouncyCastle libraries and already added the dependecies on my pom.xml file

    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-jdk15on</artifactId>
        <version>1.52</version>
    </dependency>

    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcpkix-jdk15on</artifactId>
        <version>1.52</version>
    </dependency>


Sorry about my english but it's not my native language.

Attachments

Outcomes