BasicAuthenticationExample.java

package basicauthenticationexample;

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

public class BasicAuthenticationExample {
       
    static class BasicAuthenticator extends Authenticator {
        String baName;
        String baPassword;
        private BasicAuthenticator(String baName1, String baPassword1) {
            baName = baName1;
            baPassword = baPassword1;
        }
        @Override
            public PasswordAuthentication getPasswordAuthentication() {
                System.out.println("Authenticating...");
                return new PasswordAuthentication(baName, baPassword.toCharArray());
            }
    };
    
    public static void main(String[] args) throws IOException, JAXBException {
        //Set up authentication
        //Replace these strings with real values.
        //The double backslash is to escape the backslash character.
        //Ideally, these Strings would be populated through a secure dialog.
        String name = "domain\\name";
        String password = "password";
        Authenticator.setDefault(new BasicAuthenticator(name, password));
        
        //Create a request object
        //Using Event Calendar Event List data list BizOps endpoint for this
        //example.
        EventCalendarEventListService.GetRowsRequest listRequestObj = new EventCalendarEventListService.GetRowsRequest();
        EventCalendarEventListService.ObjectFactory objectFactoryEvCalEvLiSvc = new EventCalendarEventListService.ObjectFactory();
        listRequestObj = objectFactoryEvCalEvLiSvc.createGetRowsRequest();
        
        //JAXBContext for the request
        JAXBContext contextForRequest = 
            JAXBContext.newInstance
                (EventCalendarEventListService.GetRowsRequest.class);
        
        //Marshaller for the request
        Marshaller m = contextForRequest.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        
        //Writer for the request
        StringWriter st = new StringWriter(); 
        m.marshal(listRequestObj, st);
        String listRequestStr = st.toString();
        
        //Wrap the request message with SOAP elements and XML declaration.
        listRequestStr = MessageAdjustments.fixSOAPRequestMessage(listRequestStr);
        
        //String for the endpoint
        //Don't forget to adjust for the square brackets.
        //For this example, use the Event Calendar Event List BizOps endpoint
        //on an Infinity applciation that uses Basic authentication
        String urlString = "URL string";       
        //This will send the message and place the SOAP reply message in a 
        //String.
        String listReplyStr = 
                HttpURLConnectionTechnique.createHttpURLConnectionAndMakeRequest
                (listRequestStr, urlString);
        
        //Remove the SOAP wrapping and XML declaration from the reply message.
        listReplyStr = MessageAdjustments.stripSOAPReplyMessage(listReplyStr);
        
        //Create a reply object.
        EventCalendarEventListService.GetRowsReply listReplyObj =
                new EventCalendarEventListService.GetRowsReply();
        listReplyObj = objectFactoryEvCalEvLiSvc.createGetRowsReply();
        //JAXBContext for the the reply
        JAXBContext contextForReply = 
            JAXBContext.newInstance
                (EventCalendarEventListService.GetRowsReply.class);
        //Unmarshaller for the reply
        Unmarshaller u = contextForReply.createUnmarshaller();
        StringBuilder xmlStr = new StringBuilder(listReplyStr);
        
        //Unmarshal the reply string into the Java object for the reply.
        listReplyObj = 
                (EventCalendarEventListService.GetRowsReply) u.unmarshal
                    (new StreamSource( new StringReader(xmlStr.toString())));
        
        
        //Output window
        System.out.println(listRequestStr);
        System.out.println("\nReply message:\n");
        System.out.println(listReplyStr);
        //Print out information about the reply. Total rows chosen because
        //printing row information if no rows are returned would generate an
        //exception. This isn't an exception handling example.
        System.out.print("\nTotal rows in reply: ");
        System.out.println
                (listReplyObj.getTotalRowsInReply());     
    }
}