This project has retired. For details please refer to its Attic page.
UddiDigitalSignatureFile xref
View Javadoc
1   /*
2    * Copyright 2001-2013 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  package org.apache.juddi.samples;
18  
19  import java.io.FileInputStream;
20  import java.io.FileOutputStream;
21  import java.util.concurrent.atomic.AtomicReference;
22  import javax.xml.bind.JAXB;
23  
24  import org.apache.juddi.v3.client.config.UDDIClient;
25  import org.apache.juddi.v3.client.cryptor.DigSigUtil;
26  import org.apache.juddi.v3.client.cryptor.XmlUtils;
27  import org.uddi.api_v3.*;
28  
29  /**
30   * This class shows you how to digital sign a business and save to file
31   *
32   * @author <a href="mailto:alexoree@apache.org">Alex O'Ree</a>
33   */
34  public class UddiDigitalSignatureFile {
35  
36          /**
37           * This sets up the ws proxies using uddi.xml in META-INF
38           */
39          public UddiDigitalSignatureFile() {
40  
41          }
42  
43          public enum UddiType {
44  
45                  Business, Service, Binding, TModel, PublisherAssertion
46          }
47  
48          public void fire(String fileIn, String fileOut, UddiType type) {
49                  try {
50                          System.out.println("WARN - All previous signatures will be removed!");
51                          if (fileIn == null || fileOut == null || type == null) {
52                                  System.out.print("Input file: ");
53                                  fileIn = System.console().readLine();
54                                  System.out.print("Out file: ");
55                                  fileOut = System.console().readLine();
56                                  System.out.println();
57                                  for (int i = 0; i < UddiType.values().length; i++) {
58                                          System.out.println("[" + i + "] " + UddiType.values()[i].toString());
59                                  }
60                                  System.out.print("UDDI Type: ");
61                                  String t = System.console().readLine();
62                                  type = UddiType.values()[Integer.parseInt(t)];
63                          }
64  
65                          DigSigUtil ds = null;
66  
67                          //option 1), set everything manually
68                          ds = new DigSigUtil();
69                          ds.put(DigSigUtil.SIGNATURE_KEYSTORE_FILE, "keystore.jks");
70                          ds.put(DigSigUtil.SIGNATURE_KEYSTORE_FILETYPE, "JKS");
71                          ds.put(DigSigUtil.SIGNATURE_KEYSTORE_FILE_PASSWORD, "Test");
72                          ds.put(DigSigUtil.SIGNATURE_KEYSTORE_KEY_ALIAS, "Test");
73                          ds.put(DigSigUtil.SIGNATURE_OPTION_CERT_INCLUSION_BASE64, "true");
74  
75                          ds.put(DigSigUtil.SIGNATURE_OPTION_CERT_INCLUSION_SERIAL, "true");
76                          ds.put(DigSigUtil.SIGNATURE_OPTION_CERT_INCLUSION_SUBJECTDN, "true");
77                          ds.put(DigSigUtil.TRUSTSTORE_FILE, "truststore.jks");
78                          ds.put(DigSigUtil.TRUSTSTORE_FILETYPE, "JKS");
79                          ds.put(DigSigUtil.TRUSTSTORE_FILE_PASSWORD, "Test");
80  
81                          FileInputStream fis = new FileInputStream(fileIn);
82                          Class expectedType = null;
83                          switch (type) {
84                                  case Binding:
85                                          expectedType = BindingTemplate.class;
86                                          break;
87                                  case Business:
88                                          expectedType = BusinessEntity.class;
89                                          break;
90                                  case PublisherAssertion:
91                                          expectedType = PublisherAssertion.class;
92                                          break;
93                                  case Service:
94                                          expectedType = BusinessService.class;
95                                          break;
96                                  case TModel:
97                                          expectedType = TModel.class;
98                                          break;
99                          }
100                         Object be = XmlUtils.unmarshal(fis, expectedType);
101                         fis.close();
102                         fis = null;
103 
104                         switch (type) {
105                                 case Binding:
106                                         ((BindingTemplate) be).getSignature().clear();
107                                         break;
108                                 case Business:
109                                         ((BusinessEntity) be).getSignature().clear();
110                                         break;
111                                 case PublisherAssertion:
112                                         ((PublisherAssertion) be).getSignature().clear();
113                                         break;
114                                 case Service:
115                                         ((BusinessService) be).getSignature().clear();
116                                         break;
117                                 case TModel:
118                                         ((TModel) be).getSignature().clear();
119                                         break;
120                         }
121 
122                         System.out.println("signing");
123                         Object signUDDI_JAXBObject = ds.signUddiEntity(be);
124                         System.out.println("signed");
125                         DigSigUtil.JAXB_ToStdOut(signUDDI_JAXBObject);
126 
127                         System.out.println("verifing");
128                         AtomicReference<String> msg = new AtomicReference<String>();
129                         boolean verifySigned_UDDI_JAXB_Object = ds.verifySignedUddiEntity(signUDDI_JAXBObject, msg);
130                         if (verifySigned_UDDI_JAXB_Object) {
131                                 System.out.println("signature validation passed (expected)");
132                                 FileOutputStream fos = new FileOutputStream(fileOut);
133                                 JAXB.marshal(signUDDI_JAXBObject, fos);
134                                 fos.close();
135                         } else {
136                                 System.out.println("signature validation failed (not expected)");
137                         }
138                         System.out.println(msg.get());
139 
140                 } catch (Exception e) {
141                         e.printStackTrace();
142                 }
143         }
144 
145 }