/* * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.wso2.carbon.governance.rest.api.internal; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact; import org.wso2.carbon.governance.api.exception.GovernanceException; import org.wso2.carbon.governance.rest.api.model.TypedList; import org.wso2.carbon.governance.rest.api.util.Util; import javax.ws.rs.Consumes; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; import javax.ws.rs.ext.MessageBodyWriter; import javax.ws.rs.ext.Provider; import java.io.IOException; import java.io.OutputStream; import java.lang.annotation.Annotation; import java.lang.reflect.Type; @Provider @Consumes({"application/json"}) public class GenericArtifactMessageBodyWriter implements MessageBodyWriter<TypedList<GovernanceArtifact>> { private final Log log = LogFactory.getLog(GenericArtifactMessageBodyWriter.class); @Context UriInfo uriInfo; @Override public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { if (TypedList.class.isAssignableFrom(type)) { return true; } return false; } @Override public long getSize(TypedList<GovernanceArtifact> typedList, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { return 0; } @Override public void writeTo(TypedList<GovernanceArtifact> typedList, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException { GenericArtifactJSONWriter messageWriter = new GenericArtifactJSONWriter(); try { messageWriter.writeTo(typedList, entityStream, Util.getBaseURL(uriInfo)); } catch (GovernanceException e) { log.error(e); throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR); } } }