Examples - using MTrustAttribute for Java™

For an explanation about MQePrivateRegistry and MQePublicRegistry (used in the following example) see Private registry serviceand Public registry service.

/*SIMPLE PROTECT FRAGMENT */

        MQeMsgHndl msgObj;
        MQeMTrustAttributeHndl attr;
    MQeMARSCryptorHndl mars;
    MQePrivateRegistryHndl sendreg;
    MQePublicRegistryHndl pr;
    MQeMsgObjectHndl msgObj;
        MQeInt64 confirmId;
    MQERETURN rc;
    MQeExceptBlock exceptBlock;

        rc = mqe_uniqueValue(&exceptBlock, &confirmId);
    /* create the cryptor */
        rc = mqeMARSCryptor_new(&exceptBlock, &mars);
         /* create an attribute using the cryptor */
        rc = mqeMTrustAttribute_new(&exceptBlock, &attr, 
                          NULL, mars, NULL);
         /* open the private registry belonging to the sender */
        rc = mqePrivateRegistry_new(&exceptBlock, &sendreg);
        rc = mqePrivateRegistry_activate(sendreg, &exceptBlock, 
                            MQeStrinh("Bruce1"), 
                                        MQeString(".//MQeNode_PrivateRegistry"),
                                        MQeString("12345678"), 
                                        MQeString("It_is_a_secret"), NULL, NULL);
         /* set the private registry in the attribute */
        rc = mqeMTrustAttribute_setPrivateRegistry(attr, &exceptBlock, 
                                    sendreg);
         /* set the target (recipient) name in the attribute */
        rc = mqeMTrustAttribute_setTarget(attr, &exceptBlock, 
                                MQeString("Bruce8"));
         /* open a public registry to get the target's certificate */
        rc = mqePublicRegistry_new(&exceptBlock, &pr);
         rc = mqePublicRegistry_activate(pr, &exceptBlock, 
                                         MQeString("MQeNode_PublicRegistry"), 
                                         MQeString(".//"));
         /* set the public registry in the attribute */
        rc = mqeMTrustAttribute_setPublicRegistry(attr, &exceptBlock, pr);
         /* set a home server, which is used to find the certificate*/
         /* if it is not already in the public registry */
        rc = mqeMTrustAttribute_setHomeServer(attr, &exceptBlock, 
                                               MQeString(MyHomeServer ":8082"));
         /* create the message */
        rc = mqeMsgObject_new(&exceptBlock, &msObj);
        rc = mqeFields_putAscii(msgObj, &exceptBlock, MQeString("MsgData"), 
                                 MQeString("0123456789abcdef..."));
         /* put the message using the attribute */
        rc = mqeQueueManager_putMessage(newQM, &exceptBlock, 
                            targetQMgrName, targetQName,
                                         msgObj, attr, confirmId ); 
 
        /*SIMPLE UNPROTECT FRAGMENT */

        MQeMsgHNDL msgObj2;
        MQeMTrustAttributeHndl attr2;
    MQeMARSCryptorHndl mars;
    MQePrivateRegistryHndl getreg;
     MQePublicRegistryHndl pr;
       MQeInt64 confirmId2;
    MQERETURN rc;
    MQeExceptBlock exceptBlock;

    rc = mqe_uniqueValue(&exceptBlock, &confirmId);
         /* create the cryptor */
        rc = mqeMARSCryptor_new(&exceptBlock, &mars);
         /* create an attribute using the cryptor */
        rc = mqeMTrustAttribute_new(&exceptBlock, &attr2, 
                          NULL, mars, NULL);
         /* open the private registry belonging to the target */
        rc = mqePrivateRegistry_new(&exceptBlock, &getreg);
        rc = mqePrivateRegistry_activate(getreg, &exceptBlock, 
                            MQeStrinh("Bruce8"), 
                                        MQeString(".//MQeNode_PrivateRegistry"),
                                        MQeString("12345678"), 
                                        MQeString("It_is_a_secret"), NULL, NULL);
         /* set the private registry in the attribute */
        rc = mqeMTrustAttribute_setPrivateRegistry(attr2, 
                                    &exceptBlock, 
                                    getreg);
         /* open a public registry to get the sender's certificate */
        rc = mqePublicRegistry_new(&exceptBlock, &pr);
        rc = mqePublicRegistry_activate(pr, &exceptBlock, 
                                         MQeString("MQeNode_PublicRegistry"), 
                                         MQeString(".//"));
         /* set the public registry in the attribute */
        rc = mqeMTrustAttribute_setPublicRegistry(attr2, 
                                    &exceptBlock, 
                                    pr);
         /* set a home server, which is used to find the certificate*/
         /* if it is not already in the public registry */
        rc = mqeMTrustAttribute_setHomeServer(attr2, &exceptBlock, 
                                              MQeString(MyHomeServer ":8082"));
         /* get the message using the attribute */
    rc = mqeQueueManager_getMessage(newQM, &exceptBlock, 
                          &msgObj2, targetQMgrName,
                                      targetQName, NULL, attr2, confirmId2);
    /*SIMPLE PROTECT FRAGMENT */
{
    MQeMsgObject msgObj = null;
    MQeMTrustAttribute attr = null;
    long confirmId = MQe.uniqueValue();
try {
    trace(">>>putMessage from Bruce1 intended for Bruce8" 
         + " to target Q using MQeMTrustAttribute 
          with MARSCryptor ");
     /* create the cryptor */
    MQeMARSCryptor mars = new MQeMARSCryptor();
     /* create an attribute using the cryptor */
    attr = new MQeMTrustAttribute(null, mars, null);
     /* open the private registry belonging to the sender */
    String EntityName = "Bruce1";
    String PIN = "12345678";
    Object Passwd = "It_is_a_secret";
    MQePrivateRegistry sendreg = new MQePrivateRegistry();
    sendreg.activate(EntityName, ".\\MQeNode_PrivateRegistry",
                     PIN, Passwd, null, null );
     /* set the private registry in the attribute */
    attr.setPrivateRegistry(sendreg );
     /* set the target (recipient) name in the attribute */
    attr.setTarget("Bruce8");
     /* open a public registry to get the target's certificate */
    MQePublicRegistry pr = new MQePublicRegistry();
    pr.activate("MQeNode_PublicRegistry", ".\\");
     /* set the public registry in the attribute */
    attr.setPublicRegistry(pr);
     /* set a home server, which is used to find the certificate*/
     /* if it is not already in the public registry */
    attr.setHomeServer(MyHomeServer +":8082");
     /* create the message */
    msgObj =new MQeMsgObject();
    msgObj.putAscii("MsgData","0123456789abcdef...");
     /* put the message using the attribute */
    newQM.putMessage(targetQMgrName, targetQName,
                     msgObj, attr, confirmId );
    trace(">>>MTrustAttribute protected msg put OK...");
    }
catch (Exception e)
    {
    trace(">>>on exception try resend exactly once...");
    msgObj.putBoolean(MQe.Msg_Resend, true);
    newQM.putMessage(targetQMgrName, targetQName,
                     msgObj, attr, confirmId );
    }
}


    /*SIMPLE UNPROTECT FRAGMENT */
{
    MQeMsgObject msgObj2 = null;
    MQeMTrustAttribute attr2 = null;
    long confirmId2 = MQe.uniqueValue();
try {
    trace(">>>getMessage from Bruce1 intended for Bruce8"
         + " from target Q using MQeMTrustAttribute with MARSCryptor ");
     /* create the cryptor */
    MQeMARSCryptor mars = new MQeMARSCryptor();
     /* create an attribute using the cryptor */
    attr2 = new MQeMTrustAttribute(null, mars, null);
     /* open the private registry belonging to the target */
    String EntityName =  "Bruce8";
    String PIN =  "12345678";
    Object Passwd =  "It_is_a_secret";
    MQePrivateRegistry getreg = new MQePrivateRegistry();
    getreg.activate(EntityName, ".\\MQeNode_PrivateRegistry",
                    PIN, Passwd, null, null );
     /* set the private registry in the attribute */
    attr2.setPrivateRegistry(getreg);
     /* open a public registry to get the sender's certificate */
    MQePublicRegistry pr = new MQePublicRegistry();
    pr.activate("MQeNode_PublicRegistry", ".\\");
     /* set the public registry in the attribute */
    attr2.setPublicRegistry(pr);
     /* set a home server, which is used to find the certificate*/
     /* if it is not already in the public registry */
    attr2.setHomeServer(MyHomeServer +":8082");
     /* get the message using the attribute */
    msgObj2 = newQM.getMessage(targetQMgrName,
                               targetQName, null, attr2, confirmId2 );
    trace(">>>MTrustAttribute protected msg = "
          + msgObj2.getAscii("MsgData"));
    }
catch (Exception e)
    {  
     /*exception may have left */
    newQM.undo(targetQMgrName,             /*message locked on queue */
              targetQName, confirmId2 );  /*undo just in case */
    e.printStackTrace();                  /*show exception reason */
    }
}

Terms of use | WebSphere software

(c) Copyright IBM Corporation 2004, 2005. All rights reserved.