1   /*
2    * Copyright 2017 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.server.internal.admin.util;
18  
19  import javax.annotation.Nullable;
20  
21  import com.fasterxml.jackson.core.JsonProcessingException;
22  
23  import com.linecorp.armeria.common.HttpData;
24  import com.linecorp.armeria.common.HttpHeaders;
25  import com.linecorp.armeria.common.HttpMethod;
26  import com.linecorp.armeria.common.HttpRequest;
27  import com.linecorp.armeria.common.HttpResponse;
28  import com.linecorp.armeria.common.HttpStatus;
29  import com.linecorp.armeria.common.MediaType;
30  import com.linecorp.armeria.common.RequestContext;
31  import com.linecorp.armeria.common.ResponseHeaders;
32  import com.linecorp.armeria.common.ResponseHeadersBuilder;
33  import com.linecorp.armeria.server.ServiceRequestContext;
34  import com.linecorp.armeria.server.annotation.ResponseConverterFunction;
35  import com.linecorp.centraldogma.internal.Jackson;
36  import com.linecorp.centraldogma.server.internal.api.HttpApiUtil;
37  
38  /**
39   * A default response converter of CentralDogma admin service.
40   */
41  public class RestfulJsonResponseConverter implements ResponseConverterFunction {
42  
43      private static final HttpData EMPTY_RESULT = HttpData.ofUtf8("{}");
44  
45      @Override
46      public HttpResponse convertResponse(ServiceRequestContext ctx, ResponseHeaders headers,
47                                          @Nullable Object resObj,
48                                          HttpHeaders trailingHeaders) throws Exception {
49          try {
50              final HttpRequest request = RequestContext.current().request();
51              final HttpData httpData =
52                      resObj != null &&
53                      resObj.getClass() == Object.class ? EMPTY_RESULT
54                                                        : HttpData.wrap(Jackson.writeValueAsBytes(resObj));
55  
56              final ResponseHeadersBuilder builder = headers.toBuilder();
57              if (HttpMethod.POST == request.method()) {
58                  builder.status(HttpStatus.CREATED);
59              }
60              if (builder.contentType() == null) {
61                  builder.contentType(MediaType.JSON_UTF_8);
62              }
63              return HttpResponse.of(builder.build(), httpData, trailingHeaders);
64          } catch (JsonProcessingException e) {
65              return HttpApiUtil.newResponse(ctx, HttpStatus.INTERNAL_SERVER_ERROR, e);
66          }
67      }
68  }