001 package org.hackystat.telemetry.service.resource.chart; 002 003 import java.io.StringWriter; 004 005 import javax.xml.bind.JAXBContext; 006 import javax.xml.bind.Marshaller; 007 import javax.xml.parsers.DocumentBuilder; 008 import javax.xml.parsers.DocumentBuilderFactory; 009 import javax.xml.transform.Transformer; 010 import javax.xml.transform.TransformerFactory; 011 import javax.xml.transform.dom.DOMSource; 012 import javax.xml.transform.stream.StreamResult; 013 014 import org.hackystat.telemetry.service.resource.chart.jaxb.TelemetryChartDefinition; 015 import org.hackystat.telemetry.service.resource.chart.jaxb.ParameterDefinition; 016 import org.hackystat.telemetry.service.resource.chart.jaxb.Type; 017 import org.hackystat.telemetry.analyzer.configuration.jaxb.TelemetryDefinition; 018 import org.hackystat.telemetry.analyzer.configuration.jaxb.Parameter; 019 import org.hackystat.telemetry.service.resource.telemetry.TelemetryResource; 020 import org.restlet.Context; 021 import org.restlet.data.MediaType; 022 import org.restlet.data.Request; 023 import org.restlet.data.Response; 024 import org.restlet.resource.Representation; 025 import org.restlet.resource.Variant; 026 import org.w3c.dom.Document; 027 028 /** 029 * Processes GET {host}/chart/{chart} and returns a TelemetryChartDefinition representation. 030 * Requires an authenticated user for the SensorBase associated with this service. 031 * 032 * @author Philip Johnson 033 */ 034 public class ChartDefinitionResource extends TelemetryResource { 035 036 /** 037 * The standard constructor. 038 * 039 * @param context The context. 040 * @param request The request object. 041 * @param response The response object. 042 */ 043 public ChartDefinitionResource(Context context, Request request, Response response) { 044 super(context, request, response); 045 } 046 047 /** 048 * Returns a TelemetryChartDefinition for the requested Chart, or returns an error code 049 * if the definition of the chart could not be found. 050 * 051 * @param variant The representational variant requested. 052 * @return The representation. 053 */ 054 @Override 055 public Representation represent(Variant variant) { 056 try { 057 if (variant.getMediaType().equals(MediaType.TEXT_XML)) { 058 TelemetryChartDefinition chartDef = new TelemetryChartDefinition(); 059 for (TelemetryDefinition definition : this.getTelemetryDefinitions()) { 060 if ((definition.getDefinitionType().equalsIgnoreCase("Chart")) 061 && (definition.getName().equals(this.chart))) { 062 chartDef.setDescription(definition.getDescription()); 063 chartDef.setName(definition.getName()); 064 chartDef.setSourceCode(definition.getSourceCode()); 065 for (Parameter param : definition.getParameter()) { 066 ParameterDefinition chartParam = new ParameterDefinition(); 067 chartParam.setName(param.getName()); 068 chartParam.setDescription(param.getDescription()); 069 Type type = new Type(); 070 type.setDefault(param.getType().getDefault()); 071 type.setName(param.getType().getName()); 072 if (param.getType().getMinValue() != null) { // NOPMD 073 type.setMinValue(param.getType().getMinValue()); 074 } 075 if (param.getType().getMaxValue() != null) { // NOPMD 076 type.setMaxValue(param.getType().getMaxValue()); 077 } 078 for (String value : param.getType().getValue()) { 079 type.getValue().add(value); 080 } 081 chartParam.setType(type); 082 chartDef.getParameterDefinition().add(chartParam); 083 } 084 // Made the definition, so return now. 085 return super.getStringRepresentation(makeChartDefinitionXml(chartDef)); 086 } 087 } 088 // got past the for loop without finding the chart and returning 089 setStatusError("Could not find chart definition " + this.chart); 090 return null; 091 } 092 } 093 catch (Exception e) { 094 setStatusError("Error getting chart definition.", e); 095 return null; 096 } 097 return null; 098 } 099 100 /** 101 * Returns the passed TelemetryChartDefinition instance as a String encoding of its XML. 102 * 103 * @param chartDef The SensorData instance. 104 * @return The XML String representation. 105 */ 106 private String makeChartDefinitionXml(TelemetryChartDefinition chartDef) { 107 StringWriter writer = new StringWriter(); 108 try { 109 JAXBContext jaxbContext = JAXBContext 110 .newInstance(org.hackystat.telemetry.service.resource.chart.jaxb.ObjectFactory.class); 111 Marshaller marshaller = jaxbContext.createMarshaller(); 112 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 113 dbf.setNamespaceAware(true); 114 DocumentBuilder documentBuilder = dbf.newDocumentBuilder(); 115 Document doc = documentBuilder.newDocument(); 116 marshaller.marshal(chartDef, doc); 117 DOMSource domSource = new DOMSource(doc); 118 StreamResult result = new StreamResult(writer); 119 TransformerFactory tf = TransformerFactory.newInstance(); 120 Transformer transformer = tf.newTransformer(); 121 transformer.transform(domSource, result); 122 } 123 catch (Exception e) { 124 System.out.println(e); 125 } 126 return writer.toString(); 127 } 128 129 130 }