2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.view;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.fasterxml.jackson.databind.SerializationFeature;
26 import javax.ws.rs.Produces;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.MultivaluedMap;
29 import javax.ws.rs.ext.MessageBodyWriter;
30 import javax.ws.rs.ext.Provider;
31 import java.io.IOException;
32 import java.io.OutputStream;
33 import java.lang.annotation.Annotation;
34 import java.lang.reflect.Type;
35 import java.util.List;
36 import java.util.Objects;
37 import java.util.stream.Collectors;
38 import java.util.stream.Stream;
41 * A class that is then responsible for converting a message payload with a dedicated mixin from an instance of a specific Java type into a json representation.
44 @Produces(MediaType.APPLICATION_JSON)
45 public class MixinModelWriter implements MessageBodyWriter<Object> {
48 public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
49 return hasResponseViewAndMixinTargetAnnotations(annotations) && mediaType.isCompatible(MediaType.APPLICATION_JSON_TYPE);
53 public long getSize(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
58 public void writeTo(Object object, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
59 ObjectMapper objectMapper = new ObjectMapper();
60 objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
61 List<MixinSourceTarget> mixinSourceTargets = getMixinSourceTarget(annotations);
62 mixinSourceTargets.forEach(mixinSourceTarget -> objectMapper.addMixIn(mixinSourceTarget.getTarget(), mixinSourceTarget.getMixinSource()));
63 objectMapper.writeValue(entityStream, object);
66 private List<MixinSourceTarget> getMixinSourceTarget(Annotation[] annotations) {
67 return Stream.of(annotations)
68 .filter(annotation -> annotation.annotationType().equals(ResponseView.class))
69 .map(annotation -> (ResponseView) annotation)
70 .flatMap(responseView -> Stream.of(responseView.mixin()))
71 .map(mixinClass -> new MixinSourceTarget(mixinClass, mixinClass.getAnnotation(MixinTarget.class).target()))
72 .collect(Collectors.toList());
75 private boolean hasResponseViewAndMixinTargetAnnotations(Annotation[] annotations) {
76 return Stream.of(annotations)
77 .filter(annotation -> annotation.annotationType().equals(ResponseView.class))
78 .map(annotation -> (ResponseView) annotation)
79 .flatMap(responseView -> Stream.of(responseView.mixin()))
80 .anyMatch(mixinClass -> Objects.nonNull(mixinClass.getAnnotation(MixinTarget.class)));