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 */
030public final class Request extends Message<Request> {
031
032    /** Exposes incoming request cookies. */
033    private final RequestCookies cookies = new RequestCookies(this);
034
035    /** The method to be performed on the resource. */
036    private String method;
037
038    /** The fully-qualified URI of the resource being accessed. */
039    private MutableUri uri;
040
041    /**
042     * Creates a new request message.
043     */
044    public Request() {
045        // Nothing to do.
046    }
047
048    /**
049     * Returns the incoming request cookies.
050     *
051     * @return The incoming request cookies.
052     */
053    public RequestCookies getCookies() {
054        return cookies;
055    }
056
057    /**
058     * Returns a copy of the query parameters and
059     * {@code application/x-www-form-urlencoded} entity decoded as a form.
060     * Modifications to the returned form are not reflected in this request.
061     *
062     * @return The query parameters and
063     *         {@code application/x-www-form-urlencoded} entity as a form.
064     */
065    public Form getForm() {
066        final Form form = new Form();
067        form.fromRequestQuery(this);
068        try {
069            form.fromRequestEntity(this);
070        } catch (IOException e) {
071            // Ignore: return empty form.
072        }
073        return form;
074    }
075
076    /**
077     * Returns the method to be performed on the resource.
078     *
079     * @return The method to be performed on the resource.
080     */
081    public String getMethod() {
082        return method;
083    }
084
085    /**
086     * Returns the fully-qualified URI of the resource being accessed.
087     *
088     * @return The fully-qualified URI of the resource being accessed.
089     */
090    public MutableUri getUri() {
091        return uri;
092    }
093
094    /**
095     * Sets the method to be performed on the resource.
096     *
097     * @param method
098     *            The method to be performed on the resource.
099     * @return This request.
100     */
101    public Request setMethod(final String method) {
102        this.method = method;
103        return this;
104    }
105
106    /**
107     * Sets the fully-qualified URI of the resource being accessed.
108     *
109     * @param uri
110     *            The fully-qualified URI of the resource being accessed.
111     * @return This request.
112     */
113    private Request setUri(final MutableUri uri) {
114        this.uri = uri;
115        return this;
116    }
117
118    /**
119     * Sets the fully-qualified string URI of the resource being accessed.
120     *
121     * @param uri
122     *            The fully-qualified string URI of the resource being accessed.
123     * @return This request.
124     * @throws URISyntaxException if the given URI string is not well-formed.
125     */
126    public Request setUri(final String uri) throws URISyntaxException {
127        return setUri(new MutableUri(uri));
128    }
129
130    /**
131     * Sets the fully-qualified URI of the resource being accessed.
132     *
133     * @param uri
134     *            The fully-qualified URI of the resource being accessed.
135     * @return This request.
136     */
137    public Request setUri(final URI uri) {
138        return setUri(new MutableUri(uri));
139    }
140
141    @Override
142    Request thisMessage() {
143        return this;
144    }
145}