/*
 * Licensed to Jasig under one or more contributor license
 * agreements. See the NOTICE file distributed with this work
 * for additional information regarding copyright ownership.
 * Jasig licenses this file to you under the Apache License,
 * Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License.  You may obtain a
 * copy of the License at the following location:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.jasig.cas.web;

import javax.validation.constraints.NotNull;

import org.jasig.cas.CentralAuthenticationService;
import org.jasig.cas.authentication.principal.SimpleWebApplicationServiceImpl;
import org.jasig.cas.ticket.InvalidTicketException;
import org.jasig.cas.util.HttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Implementation of a Restlet resource for creating Service Tickets from a TicketGrantingTicket, as well as deleting a TicketGrantingTicket.
 * 
 * @author Scott Battaglia
 * @version $Revision: 1.1 $ $Date: 2005/08/19 18:27:17 $
 * @since 3.3
 * 
 */
@Controller
@RequestMapping(value = "tickets/services")
public final class ServiceTicketResourceController {

	private final static Logger log = LoggerFactory.getLogger(ServiceTicketResourceController.class);

	@Autowired
	private CentralAuthenticationService centralAuthenticationService;

	private String ticketGrantingTicketId;

	@Autowired
	@NotNull
	private HttpClient httpClient;

	@InitBinder
	public void init(@RequestParam("tgt") String ticketGrantingTicketId) {
		this.ticketGrantingTicketId = ticketGrantingTicketId;
	}

	public boolean allowDelete() {
		return true;
	}

	public boolean allowPost() {
		return true;
	}

	public void setHttpClient(final HttpClient httpClient) {
		this.httpClient = httpClient;
	}

	@RequestMapping(method = RequestMethod.GET)
	public ResponseEntity<String> acceptRepresentation(@RequestParam("service") String serviceUrl) {
		try {
			final String serviceTicketId = this.centralAuthenticationService.grantServiceTicket(this.ticketGrantingTicketId,
					new SimpleWebApplicationServiceImpl(serviceUrl, this.httpClient));
			return new ResponseEntity<String>(serviceTicketId, HttpStatus.OK);
		} catch (final InvalidTicketException e) {
			log.error(e.getMessage(), e);
			return new ResponseEntity<String>("TicketGrantingTicket could not be found.", HttpStatus.BAD_REQUEST);
		} catch (final Exception e) {
			log.error(e.getMessage(), e);
			return new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
}
