1   /*
2    * Copyright 2018 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.fasterxml.jackson.databind.JsonNode;
26  import com.fasterxml.jackson.databind.node.ObjectNode;
27  
28  import com.linecorp.armeria.common.HttpData;
29  import com.linecorp.armeria.common.HttpHeaderNames;
30  import com.linecorp.armeria.common.HttpHeaders;
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.ResponseHeaders;
35  import com.linecorp.armeria.common.ResponseHeadersBuilder;
36  import com.linecorp.armeria.server.ServiceRequestContext;
37  import com.linecorp.armeria.server.annotation.ResponseConverterFunction;
38  import com.linecorp.centraldogma.internal.Jackson;
39  import com.linecorp.centraldogma.server.internal.api.HttpApiUtil;
40  
41  /**
42   * A {@link ResponseConverterFunction} for the HTTP API which creates a resource.
43   * This class adds the {@link HttpHeaderNames#LOCATION} with the url of the newly created resource.
44   */
45  public final class CreateApiResponseConverter implements ResponseConverterFunction {
46  
47      private static final Logger logger = LoggerFactory.getLogger(CreateApiResponseConverter.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 ResponseHeadersBuilder builder = headers.toBuilder();
55              if (builder.contentType() == null) {
56                  builder.contentType(MediaType.JSON_UTF_8);
57              }
58  
59              final JsonNode jsonNode = Jackson.valueToTree(resObj);
60              if (builder.get(HttpHeaderNames.LOCATION) == null) {
61                  final String url = jsonNode.get("url").asText();
62  
63                  // Remove the url field and send it with the LOCATION header.
64                  ((ObjectNode) jsonNode).remove("url");
65                  builder.add(HttpHeaderNames.LOCATION, url);
66              }
67  
68              return HttpResponse.of(builder.build(), HttpData.wrap(Jackson.writeValueAsBytes(jsonNode)),
69                                     trailingHeaders);
70          } catch (JsonProcessingException e) {
71              logger.debug("Failed to convert a response:", e);
72              return HttpApiUtil.newResponse(ctx, HttpStatus.INTERNAL_SERVER_ERROR, e);
73          }
74      }
75  }