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.api.converter;
18  
19  import javax.annotation.Nullable;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  import com.fasterxml.jackson.core.JsonProcessingException;
25  import com.google.common.collect.Iterables;
26  
27  import com.linecorp.armeria.common.HttpData;
28  import com.linecorp.armeria.common.HttpHeaders;
29  import com.linecorp.armeria.common.HttpMethod;
30  import com.linecorp.armeria.common.HttpRequest;
31  import com.linecorp.armeria.common.HttpResponse;
32  import com.linecorp.armeria.common.HttpStatus;
33  import com.linecorp.armeria.common.MediaType;
34  import com.linecorp.armeria.common.RequestContext;
35  import com.linecorp.armeria.common.ResponseHeaders;
36  import com.linecorp.armeria.common.ResponseHeadersBuilder;
37  import com.linecorp.armeria.server.ServiceRequestContext;
38  import com.linecorp.armeria.server.annotation.ResponseConverterFunction;
39  import com.linecorp.centraldogma.internal.Jackson;
40  import com.linecorp.centraldogma.server.internal.api.HttpApiUtil;
41  
42  /**
43   * A default {@link ResponseConverterFunction} of HTTP API.
44   */
45  public final class HttpApiResponseConverter implements ResponseConverterFunction {
46  
47      private static final Logger logger = LoggerFactory.getLogger(HttpApiResponseConverter.class);
48  
49      @Override
50      public HttpResponse convertResponse(ServiceRequestContext ctx, ResponseHeaders headers,
51                                          @Nullable Object resObj,
52                                          HttpHeaders trailingHeaders) throws Exception {
53          try {
54              final HttpRequest request = RequestContext.current().request();
55              if (resObj == null || HttpMethod.DELETE == request.method() ||
56                  (resObj instanceof Iterable && Iterables.size((Iterable<?>) resObj) == 0)) {
57                  return HttpResponse.of(HttpStatus.NO_CONTENT);
58              }
59  
60              final ResponseHeaders resHeaders;
61              if (headers.contentType() == null) {
62                  final ResponseHeadersBuilder builder = headers.toBuilder();
63                  builder.contentType(MediaType.JSON_UTF_8);
64                  resHeaders = builder.build();
65              } else {
66                  resHeaders = headers;
67              }
68  
69              final HttpData httpData = HttpData.wrap(Jackson.writeValueAsBytes(resObj));
70              return HttpResponse.of(resHeaders, httpData, trailingHeaders);
71          } catch (JsonProcessingException e) {
72              logger.debug("Failed to convert a response:", e);
73              return HttpApiUtil.newResponse(ctx, HttpStatus.INTERNAL_SERVER_ERROR, e);
74          }
75      }
76  }