001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2012-2015 ForgeRock AS.
015 */
016
017package org.forgerock.http.header;
018
019import static org.forgerock.http.header.HeaderUtil.*;
020
021import java.util.Collections;
022import java.util.List;
023
024import org.forgerock.http.protocol.Header;
025import org.forgerock.http.protocol.Message;
026
027/**
028 * Processes the <strong>{@code Location}</strong> message header. For more
029 * information see <a href="http://www.ietf.org/rfc/rfc2616.txt">RFC 2616</a>
030 * ยง14.30.
031 */
032public final class LocationHeader extends Header {
033
034    /**
035     * Constructs a new header, initialized from the specified message.
036     *
037     * @param message
038     *            The message to initialize the header from.
039     * @return The parsed header.
040     */
041    public static LocationHeader valueOf(final Message message) {
042        return valueOf(parseSingleValuedHeader(message, NAME));
043    }
044
045    /**
046     * Constructs a new header, initialized from the specified string value.
047     *
048     * @param string
049     *            The value to initialize the header from.
050     * @return The parsed header.
051     */
052    public static LocationHeader valueOf(final String string) {
053        return new LocationHeader(string);
054    }
055
056    /** The name of this header. */
057    public static final String NAME = "Location";
058
059    /**
060     * The location URI value from the header, or {@code null}.
061     */
062    private final String locationUri;
063
064    /**
065     * Constructs a new empty header whose location is {@code null}.
066     */
067    public LocationHeader() {
068        this(null);
069    }
070
071    /**
072     * Constructs a new header with the provided location URI.
073     *
074     * @param locationUri
075     *            The location URI, or {@code null} if no location has been set.
076     */
077    public LocationHeader(String locationUri) {
078        this.locationUri = locationUri != null && locationUri.isEmpty() ? null : locationUri;
079    }
080
081    /**
082     * Returns the location URI or {@code null} if empty.
083     *
084     * @return The location URI or {@code null} if empty.
085     */
086    public String getLocationUri() {
087        return locationUri;
088    }
089
090    @Override
091    public String getName() {
092        return NAME;
093    }
094
095    @Override
096    public List<String> getValues() {
097        return locationUri != null ? Collections.singletonList(locationUri) : Collections.<String>emptyList();
098    }
099
100    static class Factory extends AbstractSingleValuedHeaderFactory<LocationHeader> {
101
102        @Override
103        public LocationHeader parse(String value) {
104            return valueOf(value);
105        }
106    }
107}