summaryrefslogtreecommitdiffstats
path: root/src/org.gluster.storage.management.gateway/src/org/gluster/storage/management/gateway/resources/v1_0/AbstractResource.java
blob: f5b21f4579a0f7aef45f40f13c6799d8b64d3785 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*******************************************************************************
 * Copyright (c) 2006-2011 Gluster, Inc. <http://www.gluster.com>
 * This file is part of Gluster Management Gateway.
 *
 * Gluster Management Gateway is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Gluster Management Gateway is distributed in the hope that it
 * will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package org.gluster.storage.management.gateway.resources.v1_0;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;

import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.StreamingOutput;
import javax.ws.rs.core.UriInfo;

/**
 *
 */
public class AbstractResource {
	@Context
	protected UriInfo uriInfo;

	/**
	 * Creates a response with HTTP status code of 201 (created) and sets the "location" header to the URI created using
	 * the given path relative to current path.
	 * 
	 * @param relativePath
	 *            relative path of the created resource - will be set in the "location" header of response.
	 * @return the {@link Response} object
	 */
	protected Response createdResponse(String relativePath) {
		return Response.created(createRelatriveURI(relativePath)).build();
	}

	/**
	 * Creates a response with HTTP status code of 204 (no content)
	 * @return the {@link Response} object
	 */
	protected Response noContentResponse() {
		return Response.noContent().build();
	}

	/**
	 * Creates a response with HTTP status code of 202 (accepted), also setting the location header to given location.
	 * This is typically done while triggering long running tasks
	 * 
	 * @param locationURI
	 *            URI to be appended to the base URI
	 * @return the {@link Response} object
	 */
	protected Response acceptedResponse(String locationURI) {
		return Response.status(Status.ACCEPTED).location(createAbsoluteURI(locationURI)).build();
	}

	/**
	 * Creates a response with HTTP status code of 404 (not found), also setting the given message in the response body
	 * 
	 * @param message
	 *            Message to be set in the response body
	 * @return the {@link Response} object
	 */
	protected Response notFoundResponse(String message) {
		return Response.status(Status.NOT_FOUND).type(MediaType.TEXT_HTML).entity(message).build();
	}

	/**
	 * Creates a new URI that is relative to the <b>base URI</b> of the application
	 * @param uriString URI String to be appended to the base URI
	 * @return newly created URI
	 */
	private URI createAbsoluteURI(String uriString) {
		return uriInfo.getBaseUriBuilder().path(uriString).build();
	}

	/**
	 * Creates a response with HTTP status code of 204 (no content), also setting the location header to given location
	 * @param location path of the location to be set relative to current path 
	 * @return the {@link Response} object
	 */
	protected Response noContentResponse(String location) {
		return Response.noContent().location(createRelatriveURI(location)).build();
	}

	/**
	 * Creates a URI relative to current URI
	 * @param location path relative to current URI
	 * @return newly created URI
	 */
	protected URI createRelatriveURI(String location) {
		return uriInfo.getAbsolutePathBuilder().path(location).build();
	}

	/**
	 * Creates a response with HTTP status code of 500 (internal server error) and sets the error message in the
	 * response body
	 * 
	 * @param errMessage
	 *            Error message to be set in the response body
	 * @return the {@link Response} object
	 */
	protected Response errorResponse(String errMessage) {
		return Response.serverError().type(MediaType.TEXT_HTML).entity(errMessage).build();
	}

	/**
	 * Creates a response with HTTP status code of 400 (bad request) and sets the error message in the
	 * response body
	 * 
	 * @param errMessage
	 *            Error message to be set in the response body
	 * @return the {@link Response} object
	 */
	protected Response badRequestResponse(String errMessage) {
		return Response.status(Status.BAD_REQUEST).type(MediaType.TEXT_HTML).entity(errMessage).build();
	}
	
	/**
	 * Creates a response with HTTP status code of 401 (unauthorized)
	 * 
	 * @return the {@link Response} object
	 */
	protected Response unauthorizedResponse() {
		return Response.status(Status.UNAUTHORIZED).build();
	}

	/**
	 * Creates an OK response and sets the entity in the response body.
	 * 
	 * @param entity
	 *            Entity to be set in the response body
	 * @param mediaType
	 *            Media type to be set on the response
	 * @return the {@link Response} object
	 */
	protected Response okResponse(Object entity, String mediaType) {
		return Response.ok(entity).type(mediaType).build();
	}

	/**
	 * Creates a streaming output response and sets the given streaming output in the response. Typically used for
	 * "download" requests
	 * 
	 * @param entity
	 *            Entity to be set in the response body
	 * @param mediaType
	 *            Media type to be set on the response
	 * @return the {@link Response} object
	 */
	protected Response streamingOutputResponse(StreamingOutput output) {
		return Response.ok(output).type(MediaType.APPLICATION_OCTET_STREAM).build();
	}
	
	protected StreamingOutput createStreamingOutput(final byte[] data) {
		return new StreamingOutput() {
			@Override
			public void write(OutputStream output) throws IOException {
				output.write(data);
			}
		};
	}
}