All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.andrewribeiro.ribrest.services.dispatcher.DispatcherImpl Maven / Gradle / Ivy

Go to download

Ribrest Framework - A simple Java framework that truly improve your productivity when developing restful based webservices.

There is a newer version: 1.27.0
Show newest version
package br.com.andrewribeiro.ribrest.services.dispatcher;

import br.com.andrewribeiro.ribrest.model.interfaces.Model;
import br.com.andrewribeiro.ribrest.services.FlowContainer;
import br.com.andrewribeiro.ribrest.services.Result;
import br.com.andrewribeiro.ribrest.services.miner.interfaces.Miner;
import br.com.andrewribeiro.ribrest.services.miner.util.BidirectionalModelsExclusionStrategy;
import com.google.gson.GsonBuilder;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import javax.ws.rs.core.Response;

/**
 *
 * @author Andrew Ribeiro
 */
public class DispatcherImpl implements Dispatcher{
    
    @Inject
    FlowContainer flowContainer;
    
    GsonBuilder jsonBuilder = new GsonBuilder();
    
    @Override
    public Response send() {
        Miner currentMiner = flowContainer.getMiner();
        setModelsToResult();
        setupSerializationStrategy(currentMiner.extractIgnoredFields());
        return buildResultResponse();
    }
    
    private Response buildResultResponse(){
        return Response.status(flowContainer.getResult().getStatus()).entity(jsonBuilder.create().toJson(flowContainer.getResult())).build();
    }
    
    private void setupSerializationStrategy(List ignoreFields){
        BidirectionalModelsExclusionStrategy exclusionStrategy = new BidirectionalModelsExclusionStrategy(alwaysGetListOfModels(flowContainer.getResult()), ignoreFields);
        exclusionStrategy.removeCircularReferences();
        jsonBuilder.addDeserializationExclusionStrategy(exclusionStrategy);
    }
    
    private void setModelsToResult(){
        flowContainer.getResult().setHolder(flowContainer.getHolder());
    }
    
    private List alwaysGetListOfModels(Result result){
        return result.getHolder() == null ? new ArrayList() : result.getHolder().getModels();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy