Package: HttpStatusException
HttpStatusException
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
HttpStatusException(int) |
|
|
|
|
|
||||||||||||||||||||
HttpStatusException(int, Map) |
|
|
|
|
|
||||||||||||||||||||
createUrl(Site, String) |
|
|
|
|
|
||||||||||||||||||||
getHeaders() |
|
|
|
|
|
||||||||||||||||||||
getHttpStatus() |
|
|
|
|
|
||||||||||||||||||||
isError() |
|
|
|
|
|
||||||||||||||||||||
permanentRedirect(Site, String) |
|
|
|
|
|
||||||||||||||||||||
static {...} |
|
|
|
|
|
||||||||||||||||||||
temporaryRedirect(Site, String) |
|
|
|
|
|
||||||||||||||||||||
toString() |
|
|
|
|
|
||||||||||||||||||||
withHeader(String, String) |
|
|
|
|
|
Coverage
1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * NorthernWind - lightweight CMS
6: * http://northernwind.tidalwave.it - git clone https://bitbucket.org/tidalwave/northernwind-src.git
7: * %%
8: * Copyright (C) 2011 - 2023 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: *
24: * *********************************************************************************************************************
25: * #L%
26: */
27: package it.tidalwave.northernwind.core.model;
28:
29: import javax.annotation.Nonnull;
30: import java.util.Collections;
31: import java.util.HashMap;
32: import java.util.List;
33: import java.util.Map;
34: import lombok.Getter;
35: import lombok.ToString;
36: import static javax.servlet.http.HttpServletResponse.*;
37:
38: /***********************************************************************************************************************
39: *
40: * An exceptional response representing a situation that should be reported to the client with a specific HTTP status
41: * code. Note that this class doesn't necessarily represent an error, but it could be e.g. a redirect and such. This
42: * class can also carry headers to be included as part of the response
43: *
44: * @author Fabrizio Giudici
45: *
46: **********************************************************************************************************************/
47: @ToString
48: public class HttpStatusException extends Exception
49: {
50: /** Status codes that don't imply an error. */
51: private static final List<Integer> GOOD_CODES = List.of(SC_FOUND, SC_MOVED_PERMANENTLY, SC_MOVED_TEMPORARILY);
52:
53: @Getter
54: private final int httpStatus;
55:
56: @Getter
57: private final Map<String, String> headers;
58:
59: /*******************************************************************************************************************
60: *
61: * Creates an exception representing a temporary redirect.
62: *
63: * @param site the {@link Site}
64: * @param target the target to redirect to
65: * @return the exception
66: *
67: ******************************************************************************************************************/
68: @Nonnull
69: public static HttpStatusException temporaryRedirect (@Nonnull final Site site, @Nonnull final String target)
70: {
71: // FIXME: inject Site
72: return new HttpStatusException(SC_MOVED_TEMPORARILY).withHeader("Location", createUrl(site, target));
73: }
74:
75: /*******************************************************************************************************************
76: *
77: * Creates an exception representing a permanent redirect.
78: *
79: * @param site the {@link Site}
80: * @param target the target to redirect to
81: * @return the exception
82: *
83: ******************************************************************************************************************/
84: @Nonnull
85: public static HttpStatusException permanentRedirect (@Nonnull final Site site, @Nonnull final String target)
86: {
87: // FIXME: inject Site
88: return new HttpStatusException(SC_MOVED_PERMANENTLY).withHeader("Location", createUrl(site, target));
89: }
90:
91: /*******************************************************************************************************************
92: *
93: * Creates an instance with the given HTTP status.
94: *
95: * @param httpStatus the status
96: *
97: ******************************************************************************************************************/
98: public HttpStatusException (final int httpStatus)
99: {
100: this(httpStatus, Collections.emptyMap());
101: }
102:
103: /*******************************************************************************************************************
104: *
105: *
106: ******************************************************************************************************************/
107: private HttpStatusException (final int httpStatus, @Nonnull final Map<String, String> headers)
108: {
109: super(String.format("httpStatus=%d, headers=%s", httpStatus, headers));
110: this.httpStatus = httpStatus;
111: this.headers = headers;
112: }
113:
114: /*******************************************************************************************************************
115: *
116: * Creates a clone with the given header.
117: *
118: * @param name the header name
119: * @param value the header value
120: * @return the clone
121: *
122: ******************************************************************************************************************/
123: @Nonnull
124: public HttpStatusException withHeader (@Nonnull final String name, @Nonnull final String value)
125: {
126: final Map<String, String> newHeaders = new HashMap<>(headers);
127: newHeaders.put(name, value);
128: return new HttpStatusException(httpStatus, newHeaders);
129: }
130:
131: /*******************************************************************************************************************
132: *
133: * Return {@code true} whether this exception represents an error.
134: *
135: * @return {@code true} in case of error
136: *
137: ******************************************************************************************************************/
138: public boolean isError()
139: {
140:• return !GOOD_CODES.contains(httpStatus);
141: }
142:
143: /*******************************************************************************************************************
144: *
145: * @param site
146: * @param target
147: * @return
148: *
149: ******************************************************************************************************************/
150: @Nonnull
151: private static String createUrl (@Nonnull final Site site, @Nonnull final String target)
152: {
153:• return target.startsWith("http://") || target.startsWith("https://")
154: ? target
155: : site.createLink(ResourcePath.of(target));
156: }
157: }