/*
 * Copyright 2005 The JA-SIG Collaborative. All rights reserved. See license
 * distributed with this file and available online at
 * http://www.uportal.org/license.html
 */
package org.jasig.cas.advice;

import org.jasig.cas.authentication.principal.Service;
import org.jasig.cas.ticket.TicketValidationException;

/**
 * Exception indicating failure to validate a potentially otherwise valid ticket
 * granting ticket id because it has been overused.
 */
public class TgtOveruseException extends TicketValidationException {

    private static final long serialVersionUID = -1931457301226439078L;

    /**
     * Non-null String ticket granting ticket identifier which was overused,
     * prompting this exception.
     */
    private final String overusedTgtId;

    /**
     * The TGT was being presented to obtain a Service Ticket targetted at this
     * service when the TGT was discovered to be overused, or null if no target
     * Service.
     */
    private final Service targetService;

    public TgtOveruseException(String overusedTgtId) {
        // delegate to the all-field constructor
        this(overusedTgtId, null);
    }

    public TgtOveruseException(String overusedTgtId, Service targetService) {

        if (overusedTgtId == null) {
            throw new IllegalArgumentException(
                "Cannot overuse a null ticket granting ticket identifier.");
        }

        this.overusedTgtId = overusedTgtId;

        this.targetService = targetService;
    }

    /**
     * Returns the String representing the tgtid that was overused, prompting
     * this exception. Care should be taken in how this String is handled, as it
     * is the shared secret between CAS server and and end user or a
     * proxy-CAS-using service for participating in a CAS Single Sign On
     * session.
     * 
     * @return non-null overused String identifier for a ticket granting ticket.
     */
    public String getOverusedTgtId() {
        return this.overusedTgtId;
    }

    /**
     * Returns the Service for which a ServiceTicket was requested at the time
     * the TGT was discovered to be overused, or null if no such Service. The
     * Service can be instructive as to the cause of the over-use, e.g. if the
     * Service has a broken usage of CAS.
     * 
     * @return the target Service.
     */
    public Service getTargetService() {
        return this.targetService;
    }

}

@@ -286,34 +286,24 @@
 icket.Ticket
-Validation
 Exception;%0A%0A
@@ -301,16 +301,16 @@
 eption;%0A
+
 %0A/**%0A * 
@@ -494,18 +494,8 @@
 cket
-Validation
 Exce
@@ -576,16 +576,302 @@
 9078L;%0A%0A
+    /**%0A     * Code we will present to the TicketException superclass and which%0A     * TicketException will present in answer to getCode(). Codes are mapped to%0A     * messages, by default in messages.properties.%0A     */%0A    public static final String TGT_OVERUSE_CODE = %22TGT_OVERUSE%22;%0A%0A
     /**%0A
@@ -1486,16 +1486,166 @@
 rvice) %7B
+%0A        // configure the TicketException superclass to use the String code%0A        // appropriate for this exception%0A        super(TGT_OVERUSE_CODE);
 %0A%0A      
@@ -1852,24 +1852,24 @@
 usedTgtId;%0A%0A
-
         this
@@ -1899,32 +1899,568 @@
 Service;%0A    %7D%0A%0A
+    public String getMessage() %7B%0A        if (this.targetService == null) %7B%0A            return %22The ticket granting ticket %5B%22%0A                + this.overusedTgtId%0A                + %22%5D is overused and so was denied in its attempt to obtain a service ticket%22;%0A        %7D else %7B%0A            return %22The ticket granting ticket %5B%22%0A                + this.overusedTgtId%0A                + %22%5D is overused and so was denied in its attempt to obtain a service ticket to access service %5B%22%0A                + this.targetService + %22%5D%22;%0A        %7D%0A    %7D%0A%0A
     /**%0A     * R

Extend TicketException directly rather than TicketValidationException. Added CODE. Added getMessage() implementation, making the exception more loggable.