001package systems.dmx.core.service; 002 003import systems.dmx.core.DMXObject; 004import systems.dmx.core.JSONEnabled; 005 006import org.codehaus.jettison.json.JSONObject; 007 008 009 010/** 011 * A {@link systems.dmx.core.DMXObject}/{@link Directives} pair to be sent as response. 012 * <p> 013 * The DMXObject is injected via constructor. It is optional. 014 * The Directives are the thread-local ones assembled while request processing. 015 */ 016public class DirectivesResponse implements JSONEnabled { 017 018 // ---------------------------------------------------------------------------------------------- Instance Variables 019 020 private DMXObject object; 021 private Directives directives; 022 023 // ---------------------------------------------------------------------------------------------------- Constructors 024 025 public DirectivesResponse() { 026 this.object = null; 027 initDirectives(); 028 } 029 030 public DirectivesResponse(DMXObject object) { 031 this.object = object; 032 initDirectives(); 033 } 034 035 // -------------------------------------------------------------------------------------------------- Public Methods 036 037 public DMXObject getObject() { 038 return object; 039 } 040 041 public Directives getDirectives() { 042 return directives; 043 } 044 045 // *** JSONEnabled Implementation *** 046 047 @Override 048 public JSONObject toJSON() { 049 try { 050 JSONObject json = object != null ? object.toJSON() : new JSONObject(); 051 json.put("directives", directives.toJSONArray()); 052 return json; 053 } catch (Exception e) { 054 throw new RuntimeException("Serialization failed", e); 055 } 056 } 057 058 // ------------------------------------------------------------------------------------------------- Private Methods 059 060 private void initDirectives() { 061 directives = Directives.get(); 062 } 063}