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-2014 ForgeRock AS.
015 */
016
017package org.forgerock.openig.header;
018
019import org.forgerock.openig.http.Message;
020
021/**
022 * Processes the <strong>{@code Location}</strong> message header. For more information see
023 * <a href="http://www.ietf.org/rfc/rfc2616.txt">RFC 2616</a> ยง14.30.
024 */
025public final class LocationHeader implements Header {
026
027    /** The name of the header that this object represents. */
028    public static final String NAME = "Location";
029
030    /** The location URI value from the header, or empty string if not specified. */
031    private String locationURI = "";
032
033    /** Constructs a new empty <strong>{@code LocationHeader}</strong>. **/
034    public LocationHeader() {
035        // Nothing to do.
036    }
037
038    /**
039     * Constructs a new <strong>{@code LocationHeader}</strong>, initialized from the specified message.
040     *
041     * @param message The message to initialize the header from.
042     */
043    public LocationHeader(Message<?> message) {
044        fromMessage(message);
045    }
046
047    /**
048     * Constructs a new <strong>{@code LocationHeader}</strong>, initialized from the specified String value.
049     *
050     * @param string The value to initialize the header from.
051     */
052    public LocationHeader(String string) {
053        fromString(string);
054    }
055
056    /**
057     * Returns the location URI or {@code null} if empty.
058     *
059     * @return The location URI or {@code null} if empty.
060     */
061    public String getLocationURI() {
062        return "".equals(locationURI) ? null : locationURI;
063    }
064
065    @Override
066    public String getKey() {
067        return NAME;
068    }
069
070    @Override
071    public void fromMessage(Message<?> message) {
072        if (message != null && message.getHeaders() != null) {
073            // expect only one header value
074            fromString(message.getHeaders().getFirst(NAME));
075        }
076    }
077
078    @Override
079    public void fromString(String string) {
080        locationURI = "";
081        if (string != null) {
082            locationURI = string;
083        }
084    }
085
086    @Override
087    public void toMessage(Message<?> message) {
088        final String value = toString();
089        if (value != null) {
090            message.getHeaders().putSingle(NAME, value);
091        }
092    }
093
094    @Override
095    public String toString() {
096        return getLocationURI();
097    }
098
099    @Override
100    public boolean equals(Object o) {
101        return o == this
102            || (o instanceof LocationHeader
103                && locationURI.equals(((LocationHeader) o).locationURI));
104    }
105
106    @Override
107    public int hashCode() {
108        return locationURI.hashCode();
109    }
110}