Search in sources :

Example 6 with JsonException

use of com.serotonin.json.JsonException in project ma-core-public by infiniteautomation.

the class StatusServlet method doGet.

/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    IMangoLifecycle lifecycle = Providers.get(IMangoLifecycle.class);
    String timeString = request.getParameter("time");
    long time = -1;
    if (timeString != null) {
        try {
            time = Long.parseLong(timeString);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }
    }
    response.setContentType("application/json;charset=utf-8");
    response.setStatus(HttpServletResponse.SC_OK);
    response.setHeader("Access-Control-Allow-Origin", "*");
    // Get the Info and pack it up
    Map<String, Object> data = new HashMap<String, Object>();
    StringWriter sw = new StringWriter();
    JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, sw);
    // Limit to logged in users while running
    if ((lifecycle.getLifecycleState() != IMangoLifecycle.RUNNING) || (Common.getHttpUser() != null)) {
        data.put("messages", LoggingConsoleRT.instance.getMessagesSince(time));
    } else {
        data.put("messages", new ArrayList<String>());
    }
    data.put("startupProgress", lifecycle.getStartupProgress());
    data.put("shutdownProgress", lifecycle.getShutdownProgress());
    data.put("state", getLifecycleStateMessage(lifecycle.getLifecycleState()));
    // Can only get the Startup URI once the database is initalized
    if (lifecycle.getLifecycleState() > IMangoLifecycle.DATABASE_INITIALIZE) {
        Common.envProps.getBoolean("", false);
        Boolean isSsl = Common.envProps.getBoolean("ssl.on", false);
        String uri;
        if (isSsl) {
            int port = Common.envProps.getInt("ssl.port", 443);
            uri = "https://" + request.getServerName() + ":" + port + DefaultPagesDefinition.getLoginUri(request, response);
        } else {
            uri = DefaultPagesDefinition.getLoginUri(request, response);
        }
        data.put("startupUri", uri);
    }
    try {
        writer.writeObject(data);
        response.getWriter().write(sw.toString());
    } catch (JsonException e) {
        LOG.error(e);
    }
}
Also used : JsonException(com.serotonin.json.JsonException) HashMap(java.util.HashMap) JsonWriter(com.serotonin.json.JsonWriter) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) JsonException(com.serotonin.json.JsonException) StringWriter(java.io.StringWriter) IMangoLifecycle(com.serotonin.m2m2.IMangoLifecycle)

Example 7 with JsonException

use of com.serotonin.json.JsonException in project ma-core-public by infiniteautomation.

the class ConvertingRenderer method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    super.jsonRead(reader, jsonObject);
    // To ensure we have this property as it is a new one
    if (jsonObject.containsKey("useUnitAsSuffix")) {
        useUnitAsSuffix = jsonObject.getBoolean("useUnitAsSuffix");
    }
    String text = jsonObject.getString("unit");
    if (text != null) {
        try {
            unit = UnitUtil.parseUcum(text);
        } catch (Exception e) {
            throw new TranslatableJsonException("emport.error.parseError", "unit");
        }
    }
    text = jsonObject.getString("renderedUnit");
    if (text != null) {
        try {
            renderedUnit = UnitUtil.parseUcum(text);
        } catch (Exception e) {
            throw new TranslatableJsonException("emport.error.parseError", "renderedUnit");
        }
    } else {
        renderedUnit = unit;
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) IOException(java.io.IOException)

Example 8 with JsonException

use of com.serotonin.json.JsonException in project ma-core-public by infiniteautomation.

the class EmportDwr method export.

public static String export(Map<String, Object> data, int prettyIndent) {
    StringWriter stringWriter = new StringWriter();
    JsonWriter writer = new JsonWriter(Common.JSON_CONTEXT, stringWriter);
    writer.setPrettyIndent(prettyIndent);
    writer.setPrettyOutput(true);
    try {
        writer.writeObject(data);
        return stringWriter.toString();
    } catch (JsonException e) {
        throw new ShouldNeverHappenException(e);
    } catch (IOException e) {
        throw new ShouldNeverHappenException(e);
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) JsonException(com.serotonin.json.JsonException) StringWriter(java.io.StringWriter) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) IOException(java.io.IOException) JsonWriter(com.serotonin.json.JsonWriter)

Example 9 with JsonException

use of com.serotonin.json.JsonException in project ma-core-public by infiniteautomation.

the class JsonDataVO method jsonRead.

@Override
public void jsonRead(JsonReader reader, JsonObject jsonObject) throws JsonException {
    super.jsonRead(reader, jsonObject);
    String json = jsonObject.getString("jsonData");
    try {
        jsonData = JsonDataDao.instance.readValueFromString(json);
    } catch (Exception e) {
        throw new TranslatableJsonException("emport.error.parseError", "jsonData");
    }
}
Also used : TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) TranslatableJsonException(com.serotonin.m2m2.i18n.TranslatableJsonException) IOException(java.io.IOException) JsonException(com.serotonin.json.JsonException)

Example 10 with JsonException

use of com.serotonin.json.JsonException in project ma-core-public by infiniteautomation.

the class PopulateTest method main.

public static void main(String[] args) throws Exception {
    context = new JsonContext();
    context.addFactory(new ObjectFactory() {

        @Override
        public Object create(JsonValue jsonValue) throws JsonException {
            if (jsonValue.toJsonObject().containsKey("sub1Value"))
                return new Subclass1();
            if (jsonValue.toJsonObject().containsKey("sub2Value"))
                return new Subclass2();
            throw new JsonException("Unknown BaseClass: " + jsonValue);
        }
    }, BaseClass.class);
    test1();
    test2();
}
Also used : JsonException(com.serotonin.json.JsonException) JsonContext(com.serotonin.json.JsonContext) ObjectFactory(com.serotonin.json.spi.ObjectFactory) JsonValue(com.serotonin.json.type.JsonValue)

Aggregations

JsonException (com.serotonin.json.JsonException)40 TranslatableJsonException (com.serotonin.m2m2.i18n.TranslatableJsonException)22 JsonObject (com.serotonin.json.type.JsonObject)18 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)17 IOException (java.io.IOException)17 JsonValue (com.serotonin.json.type.JsonValue)6 JsonWriter (com.serotonin.json.JsonWriter)5 JsonReader (com.serotonin.json.JsonReader)4 HashMap (java.util.HashMap)4 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)3 JsonArray (com.serotonin.json.type.JsonArray)3 JsonTypeReader (com.serotonin.json.type.JsonTypeReader)3 SerializableProperty (com.serotonin.json.util.SerializableProperty)3 ProcessMessage (com.serotonin.m2m2.i18n.ProcessMessage)3 JsonSerializableUtility (com.serotonin.m2m2.util.JsonSerializableUtility)3 StringWriter (java.io.StringWriter)3 Method (java.lang.reflect.Method)3 ArrayList (java.util.ArrayList)3 JsonContext (com.serotonin.json.JsonContext)2 TypeDefinition (com.serotonin.json.util.TypeDefinition)2