001// *************************************************************************************************************************** 002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * 003// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * 004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance * 005// * with the License. You may obtain a copy of the License at * 006// * * 007// * http://www.apache.org/licenses/LICENSE-2.0 * 008// * * 009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an * 010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * 011// * specific language governing permissions and limitations under the License. * 012// *************************************************************************************************************************** 013package org.apache.juneau.rest; 014 015import static org.apache.juneau.internal.StringUtils.*; 016 017import java.net.*; 018import java.text.*; 019 020/** 021 * REST methods can return this object as a shortcut for performing <code>HTTP 302</code> redirects. 022 * 023 * <h5 class='section'>See Also:</h5> 024 * <ul> 025 * <li class='link'><a class="doclink" href="../../../../overview-summary.html#juneau-rest-server.Redirect">Overview > juneau-rest-server > Redirect</a> 026 * </ul> 027 */ 028public final class Redirect { 029 030 private final int httpResponseCode; 031 private final URI uri; 032 033 /** 034 * Redirect to the specified URL. 035 * 036 * <p> 037 * Relative paths are interpreted as relative to the servlet path. 038 * 039 * @param uri 040 * The URL to redirect to. 041 * <br>Can be any of the following: 042 * <ul> 043 * <li><code>URL</code> 044 * <li><code>URI</code> 045 * <li><code>CharSequence</code> 046 * </ul> 047 * @param args Optional {@link MessageFormat}-style arguments. 048 */ 049 public Redirect(Object uri, Object...args) { 050 this(0, uri, args); 051 } 052 053 /** 054 * Convenience method for redirecting to instance of {@link URL} and {@link URI}. 055 * 056 * <p> 057 * Same as calling <code>toString()</code> on the object and using the other constructor. 058 * 059 * @param uri 060 * The URL to redirect to. 061 * <br>Can be any of the following: 062 * <ul> 063 * <li><code>URL</code> 064 * <li><code>URI</code> 065 * <li><code>CharSequence</code> 066 * </ul> 067 */ 068 public Redirect(Object uri) { 069 this(0, uri, (Object[])null); 070 } 071 072 /** 073 * Redirect to the specified URL. 074 * 075 * <p> 076 * Relative paths are interpreted as relative to the servlet path. 077 * 078 * @param httpResponseCode The HTTP response code. 079 * @param url 080 * The URL to redirect to. 081 * <br>Can be any of the following: 082 * <ul> 083 * <li><code>URL</code> 084 * <li><code>URI</code> 085 * <li><code>CharSequence</code> 086 * </ul> 087 * @param args Optional {@link MessageFormat}-style arguments. 088 */ 089 public Redirect(int httpResponseCode, Object url, Object...args) { 090 this.httpResponseCode = httpResponseCode; 091 if (url == null) 092 url = ""; 093 this.uri = toURI(format(url.toString(), args)); 094 } 095 096 /** 097 * Shortcut for redirecting to the servlet root. 098 */ 099 public Redirect() { 100 this(0, null, (Object[])null); 101 } 102 103 /** 104 * Returns the response code passed in through the constructor. 105 * 106 * @return The response code passed in through the constructor, or <code>0</code> if response code wasn't specified. 107 */ 108 public int getHttpResponseCode() { 109 return httpResponseCode; 110 } 111 112 /** 113 * Returns the URI to redirect to. 114 * 115 * @return The URI to redirect to. 116 */ 117 public URI getURI() { 118 return uri; 119 } 120}