url += "format=json&";
}
if (!url.contains("callback=")) {
- //IMPORTANT : the url should ended with ?callback= or &callback= for JSONP calls
+ // IMPORTANT : the url should ended with ?callback= or &callback= for JSONP calls
url += "callback=";
}
makeJSONRequest(requestId++, URL.encode(url), handler);
}
- public static native void makeJSONRequest(int requestId, String url, JSONHandler handler) /*-{
+ public static native void makeJSONRequest(int requestId, String url, JSONHandler handler)
+ /*-{
var callback = "callback" + requestId;
// create SCRIPT tag, and set SRC attribute equal to JSON feed URL + callback function name
public static Date getDate(JSONObject json, String field) {
String date = getString(json, field);
if (date != null) {
- DateTimeFormat frmt = DateTimeFormat.getFormat("yyyy-MM-dd'T'HH:mm:ssZ");
- if (date.endsWith("Z") && date.length() > 2) {
- // see SONAR-1182
- date = date.substring(0, date.length() - 2) + "+0000";
- }
- return frmt.parse(date);
+ return parseDateTime(date);
}
return null;
}
throw new JavaScriptException("Not implemented");
}
-}
+ /**
+ * @since 2.5
+ */
+ public static Date parseDateTime(String dateTime) {
+ DateTimeFormat frmt = DateTimeFormat.getFormat("yyyy-MM-dd'T'HH:mm:ssZ");
+ if (dateTime.endsWith("Z") && dateTime.length() > 2) {
+ // see SONAR-1182
+ dateTime = dateTime.substring(0, dateTime.length() - 2) + "+0000";
+ }
+ return frmt.parse(dateTime);
+ }
+}
.setRuleKey(JsonUtils.getString(json, "rule_key"))
.setRuleName(JsonUtils.getString(json, "rule_name"))
.setRuleCategory(JsonUtils.getString(json, "rule_category"))
- .setRulePriority(JsonUtils.getString(json, "rule_priority"))
+ .setRuleSeverity(JsonUtils.getString(json, "rule_priority"))
.setCharacteristicKey(JsonUtils.getString(json, "ctic_key"))
.setCharacteristicName(JsonUtils.getString(json, "ctic_name"));
return measure;
--- /dev/null
+package org.sonar.wsclient.gwt.unmarshallers;
+
+import com.google.gwt.core.client.JavaScriptObject;
+import com.google.gwt.json.client.JSONArray;
+import com.google.gwt.json.client.JSONObject;
+import org.sonar.gwt.JsonUtils;
+import org.sonar.wsclient.services.TimeMachineData;
+
+import java.util.*;
+
+public class TimeMachineUnmarshaller implements Unmarshaller<TimeMachineData> {
+
+ public TimeMachineData toModel(JavaScriptObject json) {
+ JSONObject map = new JSONObject(json);
+ Map<Date, List<String>> data = new HashMap<Date, List<String>>();
+ for (String dateTimeStr : map.keySet()) {
+ JSONArray array = map.get(dateTimeStr).isArray();
+ List<String> values = new ArrayList<String>();
+ for (int i = 0; i < JsonUtils.getArraySize(array); i++) {
+ String value = array.get(i).isString().stringValue();
+ values.add(value);
+ }
+ data.put(JsonUtils.parseDateTime(dateTimeStr), values);
+ }
+ return new TimeMachineData().setData(data);
+ }
+
+ public List<TimeMachineData> toModels(JavaScriptObject json) {
+ return Arrays.asList(toModel(json));
+ }
+
+}
unmarshallers.put(Violation.class, new ViolationUnmarshaller());
unmarshallers.put(Server.class, new ServerUnmarshaller());
unmarshallers.put(DependencyTree.class, new DependencyTreeUnmarshaller());
+ unmarshallers.put(TimeMachineData.class, new TimeMachineUnmarshaller());
}
public static Unmarshaller forModel(Class modelClass) {
--- /dev/null
+package org.sonar.wsclient.services;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class TimeMachineData extends Model {
+
+ private Map<Date, List<String>> data = new HashMap<Date, List<String>>();
+
+ public Map<Date, List<String>> getData() {
+ return data;
+ }
+
+ public TimeMachineData setData(Map<Date, List<String>> data) {
+ this.data = data;
+ return this;
+ }
+
+}
--- /dev/null
+package org.sonar.wsclient.services;
+
+import java.util.Date;
+
+public class TimeMachineQuery extends Query<TimeMachineData> {
+
+ public static final String BASE_URL = "/api/timemachine";
+
+ private String resourceKeyOrId;
+ private String[] metrics;
+ private Date from;
+ private Date to;
+
+ public TimeMachineQuery(String resourceKeyOrId) {
+ this.resourceKeyOrId = resourceKeyOrId;
+ }
+
+ public String[] getMetrics() {
+ return metrics;
+ }
+
+ public TimeMachineQuery setMetrics(String... metrics) {
+ this.metrics = metrics;
+ return this;
+ }
+
+ public Date getFrom() {
+ return from;
+ }
+
+ public TimeMachineQuery setFrom(Date from) {
+ this.from = from;
+ return this;
+ }
+
+ public Date getTo() {
+ return to;
+ }
+
+ public TimeMachineQuery setTo(Date to) {
+ this.to = to;
+ return this;
+ }
+
+ @Override
+ public String getUrl() {
+ StringBuilder url = new StringBuilder(BASE_URL);
+ url.append('?');
+ appendUrlParameter(url, "resource", resourceKeyOrId);
+ appendUrlParameter(url, "metrics", metrics);
+ appendUrlParameter(url, "first_date", from);
+ appendUrlParameter(url, "last_date", to);
+ return url.toString();
+ }
+
+ @Override
+ public Class<TimeMachineData> getModelClass() {
+ return TimeMachineData.class;
+ }
+
+ public static TimeMachineQuery create(String resourceKeyOrId) {
+ return new TimeMachineQuery(resourceKeyOrId);
+ }
+
+}
*/
package org.sonar.wsclient.unmarshallers;
+import org.json.simple.JSONArray;
+
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
return null;
}
+ /**
+ * @since 2.5
+ */
+ public static JSONArray getArray(Map obj, String field) {
+ return (JSONArray) obj.get(field);
+ }
+
public static Date getDateTime(Map obj, String field) {
return parseDate(obj, field, "yyyy-MM-dd'T'HH:mm:ssZ");
}
}
return null;
}
+
+ /**
+ * @since 2.5
+ */
+ public static Date parseDateTime(String dateTime) {
+ try {
+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
+ return dateFormat.parse(dateTime);
+ } catch (ParseException e) {
+ throw new RuntimeException(e);
+ }
+ }
}
@Override
protected Metric parse(JSONObject json) {
return new Metric()
- .setKey((String) json.get("key"))
- .setName((String) json.get("name"))
- .setDomain((String) json.get("domain"))
- .setDescription((String) json.get("description"))
+ .setKey(JsonUtils.getString(json, "key"))
+ .setName(JsonUtils.getString(json, "name"))
+ .setDomain(JsonUtils.getString(json, "domain"))
+ .setDescription(JsonUtils.getString(json, "description"))
.setDirection(JsonUtils.getInteger(json, "direction"))
.setType(JsonUtils.getString(json, "val_type"))
.setUserManaged(JsonUtils.getBoolean(json, "user_managed"))
@Override
protected Property parse(JSONObject json) {
return new Property()
- .setKey((String) json.get("key"))
- .setValue((String) json.get("value"));
+ .setKey(JsonUtils.getString(json, "key"))
+ .setValue(JsonUtils.getString(json, "value"));
}
}
\ No newline at end of file
}
private void parseMeasures(JSONObject json, Resource resource) {
- JSONArray measuresJson = (JSONArray) json.get("msr");
+ JSONArray measuresJson = JsonUtils.getArray(json, "msr");
if (measuresJson != null) {
resource.setMeasures(parseMeasures(measuresJson));
}
--- /dev/null
+package org.sonar.wsclient.unmarshallers;
+
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+import org.json.simple.JSONValue;
+import org.sonar.wsclient.services.TimeMachineData;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class TimeMachineUnmarshaller implements Unmarshaller<TimeMachineData> {
+
+ public TimeMachineData toModel(String json) {
+ JSONObject map = (JSONObject) JSONValue.parse(json);
+ Map<Date, List<String>> data = new HashMap<Date, List<String>>();
+ for (Object key : map.keySet()) {
+ JSONArray array = (JSONArray) map.get(key);
+ data.put(JsonUtils.parseDateTime((String) key), array);
+ }
+ return new TimeMachineData().setData(data);
+ }
+
+ public List<TimeMachineData> toModels(String json) {
+ throw new UnsupportedOperationException();
+ }
+
+}
unmarshallers.put(Favourite.class, new FavouriteUnmarshaller());
unmarshallers.put(Plugin.class, new PluginUnmarshaller());
unmarshallers.put(Rule.class, new RuleUnmarshaller());
+ unmarshallers.put(TimeMachineData.class, new TimeMachineUnmarshaller());
}
public static <MODEL extends Model> Unmarshaller<MODEL> forModel(Class<MODEL> modelClass) {
--- /dev/null
+package org.sonar.wsclient.unmarshallers;
+
+import org.apache.commons.io.IOUtils;
+import org.junit.Test;
+import org.sonar.wsclient.services.TimeMachineData;
+
+import java.io.IOException;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.assertThat;
+
+public class TimeMachineUnmarshallerTest {
+
+ @Test
+ public void toModel() throws IOException {
+ TimeMachineData data = new TimeMachineUnmarshaller().toModel(loadFile("/timemachine/timemachine.json"));
+
+ assertThat(data.getData().size(), is(2));
+ }
+
+ private static String loadFile(String path) throws IOException {
+ return IOUtils.toString(TimeMachineUnmarshallerTest.class.getResourceAsStream(path));
+ }
+
+}
--- /dev/null
+{
+ "2010-10-04T00:00:00+0000": ["18.0", null, "13.1"],
+ "2010-12-04T00:00:00+0000": ["20.0", null, "12.8"]
+}