001/*
002 *  Copyright (c) 2000-2003 Yale University. All rights reserved.
003 *
004 *  THIS SOFTWARE IS PROVIDED "AS IS," AND ANY EXPRESS OR IMPLIED
005 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
006 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE EXPRESSLY
007 *  DISCLAIMED. IN NO EVENT SHALL YALE UNIVERSITY OR ITS EMPLOYEES BE
008 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
009 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED, THE COSTS OF
010 *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA OR
011 *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
012 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
013 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
014 *  SOFTWARE, EVEN IF ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH
015 *  DAMAGE.
016 *
017 *  Redistribution and use of this software in source or binary forms,
018 *  with or without modification, are permitted, provided that the
019 *  following conditions are met:
020 *
021 *  1. Any redistribution must include the above copyright notice and
022 *  disclaimer and this list of conditions in any related documentation
023 *  and, if feasible, in the redistributed software.
024 *
025 *  2. Any redistribution must include the acknowledgment, "This product
026 *  includes software developed by Yale University," in any related
027 *  documentation and, if feasible, in the redistributed software.
028 *
029 *  3. The names "Yale" and "Yale University" must not be used to endorse
030 *  or promote products derived from this software.
031 */
032
033package edu.yale.its.tp.cas.client;
034
035import java.net.URLEncoder;
036
037import javax.servlet.ServletException;
038import javax.servlet.http.HttpServletRequest;
039
040/**
041 * Provides utility functions in support of CAS clients.
042 */
043public class Util {
044
045    /**
046     * Returns a service ID (URL) as a composite of the preconfigured server name and the runtime request.
047     */
048    public static String getService(HttpServletRequest request, String server) throws ServletException {
049        // ensure we have a server name
050        if (server == null)
051            throw new IllegalArgumentException("name of server is required");
052
053        // now, construct our best guess at the string
054        StringBuffer sb = new StringBuffer();
055        if (request.isSecure())
056            sb.append("https://");
057        else
058            sb.append("http://");
059        sb.append(server);
060        sb.append(request.getRequestURI());
061
062        if (request.getQueryString() != null) {
063            // first, see whether we've got a 'ticket' at all
064            int ticketLoc = request.getQueryString().indexOf("ticket=");
065
066            // if ticketLoc == 0, then it's the only parameter and we ignore
067            // the whole query string
068
069            // if no ticket is present, we use the query string wholesale
070            if (ticketLoc == -1)
071                sb.append("?" + request.getQueryString());
072            else if (ticketLoc > 0) {
073                ticketLoc = request.getQueryString().indexOf("&ticket=");
074                if (ticketLoc == -1) {
075                    // there was a 'ticket=' unrelated to a parameter named 'ticket'
076                    sb.append("?" + request.getQueryString());
077                } else if (ticketLoc > 0) {
078                    // otherwise, we use the query string up to "&ticket="
079                    sb.append("?" + request.getQueryString().substring(0, ticketLoc));
080                }
081            }
082        }
083        return URLEncoder.encode(sb.toString());
084    }
085}