# MyCompanyClient.java Sample

This topic inludes the source code for the MyCompanyClient.java Sample.

## Sample Location

This sample is located in the following directory in your WebLogic Workshop installation:

BEA_HOME/weblogic81/samples/workshop/SamplesApp/ProxyClient/WSSE/clientCert/

## Sample Source Code

`001 package WSSE.clientCert; 002 003 import java.util.List; 004 import java.util.ArrayList; 005 import java.security.KeyStore; 006 import java.security.PrivateKey; 007 import java.security.cert.X509Certificate; 008 import java.security.cert.CertificateException; 009 import javax.xml.rpc.ServiceException; 010 import javax.xml.namespace.QName; 011 import javax.xml.rpc.handler.HandlerInfo; 012 import javax.xml.rpc.handler.HandlerRegistry; 013 import weblogic.Home; 014 import weblogic.webservice.context.WebServiceSession; 015 import weblogic.webservice.context.WebServiceContext; 016 import weblogic.webservice.core.handler.WSSEClientHandler; 017 import weblogic.webservice.WLMessageContext; 018 import weblogic.xml.security.wsse.Security; 019 import weblogic.xml.security.wsse.Token; 020 import weblogic.xml.security.wsse.BinarySecurityToken; 021 import weblogic.xml.security.wsse.SecurityElementFactory; 022 import weblogic.xml.security.specs.SignatureSpec; 023 import weblogic.jws.proxies.MyCompany; 024 import weblogic.jws.proxies.MyCompany_Impl; 025 import weblogic.jws.proxies.MyCompanySoap; 026 import weblogic.xml.security.UserInfo; 027 import weblogic.xml.security.specs.EncryptionSpec; 028 029 /* 030 * This client (1) adds a digital certificate to the invoking SOAP message, 031 * (2) encrypts the invoking SOAP message, 032 * and (3) adds a username/password token to the SOAP message. 033 */ 034 public class MyCompanyClient { 035 036 public static final String USERNAME="weblogic"; 037 public static final String USER_PASSWORD="weblogic"; 038 039 /* 040 * Home.getPath() returns the String value WL_HOME + "/server". 041 * Home.getPath().replaceAll("/server", "") extracts the value of WL_HOME. 042 */ 043 private static final String CLIENT_KEYSTORE = Home.getPath().replaceAll("/server", "") + "/samples/domains/workshop/samples_client.jks"; 044 private static final String KEYSTORE_PASS = "password"; 045 private static final String KEY_ALIAS = "client1"; 046 private static final String SERVER_KEY_ALIAS = "mycompany"; 047 private static final String KEY_PASSWORD = "password"; 048 049 public static void main(String[] args) { 050 051 try{ 052 /* 053 * Instantiate the main proxy class. The proxy class has the same name as the 054 * web service, with "_Impl" appended. 055 */ 056 MyCompany myservice = new MyCompany_Impl("http://localhost:7001/WebServices/security/wsse/reqResp/mycompany/MyCompany.jws?WSDL"); 057 058 WebServiceContext context = myservice.context(); 059 WebServiceSession session = context.getSession(); 060 061 /** 062 * Registers a handler for the SOAP message traffic. 063 HandlerRegistry registry = myservice.getHandlerRegistry(); 064 List list = new ArrayList(); 065 list.add(new HandlerInfo(WSSEClientHandler.class, null, null)); 066 registry.setHandlerChain(new QName("hello"), list); 067 */ 068 final KeyStore keystore = KeyUtil.loadKeystore(CLIENT_KEYSTORE, KEYSTORE_PASS); 069 070 // Add a client certificate 071 072 X509Certificate clientcert = KeyUtil.getCertificate(KEY_ALIAS, keystore); 073 074 PrivateKey clientprivate = KeyUtil.getPrivateKey(KEY_ALIAS, KEY_PASSWORD, keystore); 075 076 SecurityElementFactory factory = SecurityElementFactory.getDefaultFactory(); 077 078 Token x509token = factory.createToken(clientcert, clientprivate); 079 080 SignatureSpec sigSpec = SignatureSpec.getDefaultSpec(); 081 082 Security security = factory.createSecurity(null); 083 084 security.addSignature(x509token, sigSpec); 085 086 security.addToken(x509token); 087 088 // Encrypts the SOAP body 089 090 X509Certificate servercert = KeyUtil.getCertificate(SERVER_KEY_ALIAS, keystore); 091 092 EncryptionSpec encSpec = EncryptionSpec.getDefaultSpec(); 093 094 Token serverToken = factory.createToken(servercert, null); 095 096 security.addEncryption(serverToken, encSpec); 097 098 // Adds a username/password token 099 100 /** 101 * Set the username and password token for SOAP message sent from the client, through 102 * the proxy, to the web service. 103 */ 104 UserInfo ui = new UserInfo("weblogic", "weblogic"); 105 session.setAttribute(WSSEClientHandler.REQUEST_USERINFO, ui); 106 107 /** 108 * Adds the username / password token to the SOAP header. 109 */ 110 Security security2 = factory.createSecurity(null); 111 security.addToken(ui); 112 session.setAttribute(WSSEClientHandler.REQUEST_SECURITY, security); 113 114 /* 115 * Get the protocol-specific proxy class. 116 */ 117 MyCompanySoap msg = myservice.getMyCompanySoap(); 118 119 /* 120 * Add the security element to the request. 121 */ 122 context.getSession().setAttribute(WSSEClientHandler.REQUEST_SECURITY, security); 123 124 /* 125 * Set the client's private key to decrypt the response 126 */ 127 session.setAttribute(WSSEClientHandler.KEY_ATTRIBUTE, clientprivate); 128 129 /** 130 * Invoke the web service method hello() 131 */ 132 String result = msg.hello(); 133 134 System.out.println(); 135 System.out.println("Web Service Response:"); 136 System.out.println(result); 137 } 138 catch(Exception e){ 139 e.printStackTrace(); 140 } 141 } 142 }`