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-2016 ForgeRock AS.
017 */
018
019package org.forgerock.http.protocol;
020
021import java.io.IOException;
022import java.net.URI;
023import java.net.URISyntaxException;
024
025import org.forgerock.http.MutableUri;
026
027/**
028 * A request message.
029 */
030public final class Request extends MessageImpl<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     * Creates a defensive copy of the given {@code request} message.
050     *
051     * @param request
052     *         request to be copied
053     * @throws IOException
054     *         when entity cannot be cloned
055     */
056    public Request(final Request request) throws IOException {
057        super(request);
058        method = request.method;
059        uri = new MutableUri(request.uri.asURI());
060    }
061
062    /**
063     * Returns the incoming request cookies.
064     *
065     * @return The incoming request cookies.
066     */
067    public RequestCookies getCookies() {
068        return cookies;
069    }
070
071    /**
072     * Returns a copy of the query parameters and
073     * {@code application/x-www-form-urlencoded} entity decoded as a form.
074     * Modifications to the returned form are not reflected in this request.
075     *
076     * @return The query parameters and
077     *         {@code application/x-www-form-urlencoded} entity as a form.
078     */
079    public Form getForm() {
080        final Form form = new Form();
081        form.fromRequestQuery(this);
082        try {
083            form.fromRequestEntity(this);
084        } catch (IOException e) {
085            // Ignore: return empty form.
086        }
087        return form;
088    }
089
090    /**
091     * Returns the method to be performed on the resource.
092     *
093     * @return The method to be performed on the resource.
094     */
095    public String getMethod() {
096        return method;
097    }
098
099    /**
100     * Returns the fully-qualified URI of the resource being accessed.
101     *
102     * @return The fully-qualified URI of the resource being accessed.
103     */
104    public MutableUri getUri() {
105        return uri;
106    }
107
108    @Override
109    public Request setEntity(Object o) {
110        setEntity0(o);
111        return this;
112    }
113
114    /**
115     * Sets the method to be performed on the resource.
116     *
117     * @param method
118     *            The method to be performed on the resource.
119     * @return This request.
120     */
121    public Request setMethod(final String method) {
122        this.method = method;
123        return this;
124    }
125
126    /**
127     * Sets the fully-qualified URI of the resource being accessed.
128     *
129     * @param uri
130     *            The fully-qualified URI of the resource being accessed.
131     * @return This request.
132     */
133    private Request setUri(final MutableUri uri) {
134        this.uri = uri;
135        return this;
136    }
137
138    /**
139     * Sets the fully-qualified string URI of the resource being accessed.
140     *
141     * @param uri
142     *            The fully-qualified string URI of the resource being accessed.
143     * @return This request.
144     * @throws URISyntaxException
145     *             if the given URI string is not well-formed.
146     */
147    public Request setUri(final String uri) throws URISyntaxException {
148        return setUri(new MutableUri(uri));
149    }
150
151    /**
152     * Sets the fully-qualified URI of the resource being accessed.
153     *
154     * @param uri
155     *            The fully-qualified URI of the resource being accessed.
156     * @return This request.
157     */
158    public Request setUri(final URI uri) {
159        return setUri(new MutableUri(uri));
160    }
161
162    @Override
163    public Request setVersion(String version) {
164        setVersion0(version);
165        return this;
166    }
167
168}