FileDocCategorySizeDatePackage
HandlerBookServiceServant.javaAPI DocExample3981Tue Dec 17 23:14:52 GMT 2002ora.jwsnut.chapter6.handlerbookservice

HandlerBookServiceServant.java

package ora.jwsnut.chapter6.handlerbookservice;

import java.util.Date;
import java.util.GregorianCalendar;
import javax.servlet.ServletContext;
import javax.xml.rpc.handler.MessageContext;
import javax.xml.rpc.holders.StringHolder;
import javax.xml.rpc.ServiceException;
import javax.xml.rpc.server.ServletEndpointContext;
import javax.xml.rpc.server.ServiceLifecycle;

/**
 * Implementation class for the small book web service.
 */
public class HandlerBookServiceServant implements HandlerBookQuery, ServiceLifecycle {
    
    // ServletEndpointContext object
    private ServletEndpointContext endpointContext;
    
    // Name of an authorized user.
    private String userName;
        
    // Password for the authorized user.
    private String password;

    
    /* -- Implementation of the ServiceLifeCycle interface -- */
    public void init(Object context) throws ServiceException {
        endpointContext = (ServletEndpointContext)context;
        
        // Get the authorized user name and password from the init parameters
        ServletContext servletContext = endpointContext.getServletContext();
        userName = servletContext.getInitParameter("UserName");
        password = servletContext.getInitParameter("Password");
    }
    
    public void destroy() {
        // Nothing to do
    }
    
    /* -- Implementation of the HandlerBookQuery interface -- */    
    /**
     * Gets the number of books known to the service
     * @return the number of books known to the service.
     */
    public int getBookCount() {
        String[] titles = HandlerBookServiceServantData.getBookTitles();
        return titles == null || !checkAccess() ? 0 : titles.length;
    }

    /**
     * Gets the title of a given book.
     * @param index the index of the book whose title is required
     * @return the title of the given book, or <code>null</code> if
     * the index is not valid.
     */
    public String getBookTitle(int index) {
        String[] bookTitles = HandlerBookServiceServantData.getBookTitles(); 
        if (bookTitles == null || index < 0 || index >= bookTitles.length || !checkAccess()) {
            return null;
        }
        return bookTitles[index];
    }
    
    /**
     * Gets the author for a books with a given title
     * @param title the titles of the book
     * @param author an output parameter that will be set to the author of the given book
     * @throws HandlerBookServiceException if the book title is unknown
     */
    public void getBookAuthor(String title, StringHolder author) throws HandlerBookServiceException {
        String authorName = HandlerBookServiceServantData.getBookAuthor(title);
        if (authorName == null || !checkAccess()) {
            throw new HandlerBookServiceException("Unknown author: " + title);
        }
        author.value = authorName;
    }  
    
    /**
     * Makes a log entry.
     * @param value the value to be logged.
     */
    public void log(String value) {
        if (checkAccess()) {
            endpointContext.getServletContext().log(new Date() + ": " + value);
        }
    }
       
    /**
     * Check whether the calling user is authenticated.
     */
    private boolean checkAccess() {
        boolean allowed = false;
        
        // Get the username and password from the MessageContext
        MessageContext context = endpointContext.getMessageContext();
        String callingUser = (String)context.getProperty(HandlerBookServiceConstants.USERNAME_PROPERTY);
        String callingPwd = (String)context.getProperty(HandlerBookServiceConstants.PASSWORD_PROPERTY);
        
        if (userName != null && password != null) {
            // Authentication is configured.
            return userName.equals(callingUser) &&
                    password.equals(callingPwd);
        }
        return allowed;            
    }
}