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 2009 Sun Microsystems Inc.
015 * Portions Copyright 2010–2011 ApexIdentity Inc.
016 * Portions Copyright 2011-2014 ForgeRock AS.
017 */
018
019package org.forgerock.openig.http;
020
021import java.io.IOException;
022import java.net.URI;
023import java.net.URISyntaxException;
024
025import org.forgerock.openig.util.MutableUri;
026
027/**
028 * A request message.
029 * <p>
030 * A RequestResolver is linked to this class.
031 *
032 * @see org.forgerock.openig.resolver.RequestResolver
033 */
034public final class Request extends Message<Request> {
035
036    /** Exposes incoming request cookies. */
037    private final RequestCookies cookies = new RequestCookies(this);
038
039    /** The method to be performed on the resource. */
040    private String method;
041
042    /** The fully-qualified URI of the resource being accessed. */
043    private MutableUri uri;
044
045    /**
046     * Creates a new request message.
047     */
048    public Request() {
049        // Nothing to do.
050    }
051
052    /**
053     * Returns the incoming request cookies.
054     *
055     * @return The incoming request cookies.
056     */
057    public RequestCookies getCookies() {
058        return cookies;
059    }
060
061    /**
062     * Returns a copy of the query parameters and
063     * {@code application/x-www-form-urlencoded} entity decoded as a form.
064     * Modifications to the returned form are not reflected in this request.
065     *
066     * @return The query parameters and
067     *         {@code application/x-www-form-urlencoded} entity as a form.
068     */
069    public Form getForm() {
070        final Form form = new Form();
071        form.fromRequestQuery(this);
072        try {
073            form.fromRequestEntity(this);
074        } catch (IOException e) {
075            // Ignore: return empty form.
076        }
077        return form;
078    }
079
080    /**
081     * Returns the method to be performed on the resource.
082     *
083     * @return The method to be performed on the resource.
084     */
085    public String getMethod() {
086        return method;
087    }
088
089    /**
090     * Returns the fully-qualified URI of the resource being accessed.
091     *
092     * @return The fully-qualified URI of the resource being accessed.
093     */
094    public MutableUri getUri() {
095        return uri;
096    }
097
098    /**
099     * Sets the method to be performed on the resource.
100     *
101     * @param method
102     *            The method to be performed on the resource.
103     * @return This request.
104     */
105    public Request setMethod(final String method) {
106        this.method = method;
107        return this;
108    }
109
110    /**
111     * Sets the fully-qualified URI of the resource being accessed.
112     *
113     * @param uri
114     *            The fully-qualified URI of the resource being accessed.
115     * @return This request.
116     */
117    private Request setUri(final MutableUri uri) {
118        this.uri = uri;
119        return this;
120    }
121
122    /**
123     * Sets the fully-qualified string URI of the resource being accessed.
124     *
125     * @param uri
126     *            The fully-qualified string URI of the resource being accessed.
127     * @return This request.
128     * @throws URISyntaxException if the given URI string is not well-formed.
129     */
130    public Request setUri(final String uri) throws URISyntaxException {
131        return setUri(new MutableUri(uri));
132    }
133
134    /**
135     * Sets the fully-qualified URI of the resource being accessed.
136     *
137     * @param uri
138     *            The fully-qualified URI of the resource being accessed.
139     * @return This request.
140     */
141    public Request setUri(final URI uri) {
142        return setUri(new MutableUri(uri));
143    }
144
145    @Override
146    Request thisMessage() {
147        return this;
148    }
149
150    @Override
151    void prepareHeaders(final Headers headers) {
152        if (uri != null) {
153            final String uriHost = uri.getHost() + (uri.getPort() != -1 ? ":" + uri.getPort() : "");
154            // Checks if http header host has been updated since last invocation.
155            if (!uriHost.equals(headers.getFirst("host"))) {
156                headers.putSingle("host", uriHost);
157            }
158        }
159    }
160}