Split Restconf implementations (draft02 and RFC) - move
[netconf.git] / restconf / restconf-nb-bierman02 / src / main / java / org / opendaylight / restconf / restful / utils / ResponseFactory.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.restconf.restful.utils;
9
10 import java.net.URI;
11 import javax.ws.rs.core.Response;
12 import javax.ws.rs.core.Response.ResponseBuilder;
13 import javax.ws.rs.core.Response.Status;
14 import org.apache.commons.lang3.builder.Builder;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16
17 /**
18  * Response factory.
19  *
20  * @deprecated move to splitted module restconf-nb-rfc8040
21  */
22 @Deprecated
23 final class ResponseFactory extends FutureDataFactory<Void> implements Builder<Response> {
24
25     private ResponseBuilder responseBuilder;
26
27     ResponseFactory(final NormalizedNode<?, ?> readData) {
28         final Status status = prepareStatus(readData);
29         this.responseBuilder = Response.status(status);
30     }
31
32     ResponseFactory(final NormalizedNode<?, ?> readData, final URI location) {
33         final Status status = prepareStatus(readData);
34         this.responseBuilder = Response.status(status);
35         this.responseBuilder.location(location);
36     }
37
38     ResponseFactory() {
39         this.responseBuilder = Response.status(Status.OK);
40     }
41
42     @Override
43     public Response build() {
44         if (getFailureStatus()) {
45             responseBuilder = responseBuilder.status(Response.Status.INTERNAL_SERVER_ERROR);
46         }
47         return this.responseBuilder.build();
48     }
49
50     private static Status prepareStatus(final NormalizedNode<?, ?> readData) {
51         return readData != null ? Status.OK : Status.CREATED;
52     }
53 }