class TicketDistributionServiceImpl extends AbstractTicketService implements TicketDistributionService {

	@Override
	public TicketDistribution getTicketDistributionForTicket(final Ticket ticket) throws JargonException {

		log.info("getTicketDistributionForTicket()");

		if (ticket == null) {
			throw new IllegalArgumentException("null ticket");
		}

		log.info("ticket: {}", ticket);

		// a few short sanity checks on the ticket

		if (ticket.getTicketString() == null || ticket.getTicketString().isEmpty()) {
			throw new IllegalArgumentException("no ticketString in ticket, appears not to be valid");
		}

		if (ticket.getIrodsAbsolutePath() == null || ticket.getIrodsAbsolutePath().isEmpty()) {
			throw new IllegalArgumentException("no irodsAbsolutePath in ticket, appears not to be valid");
		}

		TicketDistribution ticketDistribution = new TicketDistribution();
		ticketDistribution.setTicket(ticket);
		ticketDistribution.setIrodsAccessURI(IRODSUriUtils
				.buildURIForAnAccountWithNoUserInformationIncluded(irodsAccount, ticket.getIrodsAbsolutePath()));

		if (!ticketDistributionContext.getHost().isEmpty()) {
			log.info(
					"host info is present in ticket distribution context, so add a generated URL given the other information...");
			// create a string in the expected scheme
			// <scheme>://<authority><path>?<query>#<fragment>
			StringBuilder sb = new StringBuilder();
			if (ticketDistributionContext.isSsl()) {
				sb.append("https://");
			} else {
				sb.append("http://");
			}

			sb.append(ticketDistributionContext.getHost());

			if (ticketDistributionContext.getPort() > 0) {
				sb.append(':');
				sb.append(ticketDistributionContext.getPort());
			}

			sb.append(ticketDistributionContext.getContext());
			sb.append("?ticketString=");

			try {
				sb.append(URLEncoder.encode(ticket.getTicketString(), "UTF-8"));
			} catch (UnsupportedEncodingException e) {
				log.error("unsupported encoding of ticket string", e);
				throw new JargonException("cannot encode ticketString", e);
			}

			sb.append("&objectType=");
			sb.append(ticket.getObjectType());
			sb.append("&ticketType=");
			sb.append(ticket.getType());

			sb.append("&irodsURI=");
			sb.append(ticketDistribution.getIrodsAccessURI().toASCIIString());
			URL accessURL;
			try {
				accessURL = new URL(sb.toString());
				log.info("generated url:{}", accessURL);
			} catch (MalformedURLException e) {
				log.error("malformed url from:{}", sb.toString(), e);
				throw new JargonException(
						"malformed URL for ticketDistribution, probably a malformed ticketDistributionContext");
			}
			ticketDistribution.setTicketURL(accessURL);

			/*
			 * Tack on a ticket landing page for the url with landing page information. This
			 * URL will request display of an intermediate page if the ticket is redeemed,
			 * versus direct download of a file. The processing of such a request is
			 * dependent on the client.
			 */

			sb.append("&landingPage=true");

			URL landingPageURL;
			try {
				landingPageURL = new URL(sb.toString());
				log.info("generated landing url:{}", accessURL);
			} catch (MalformedURLException e) {
				log.error("malformed url from:{}", sb.toString(), e);
				throw new JargonException(
						"malformed URL for ticketDistribution, probably a malformed ticketDistributionContext");
			}
			ticketDistribution.setTicketURLWithLandingPage(landingPageURL);
		}

		return ticketDistribution;

	}

	  TicketDistributionServiceImpl(final IRODSAccessObjectFactory irodsAccessObjectFactory,
			final IRODSAccount irodsAccount, final TicketServiceFactory ticketServiceFactory,
			final TicketDistributionContext ticketDistributionContext);

}