Search in sources :

Example 56 with ParseException

use of org.json.simple.parser.ParseException in project tika by apache.

the class DL4JInceptionV3Net method initialize.

@Override
public void initialize(Map<String, Param> params) throws TikaConfigException {
    //STEP 1: resolve weights file, download if necessary
    if (modelWeightsPath.startsWith("http://") || modelWeightsPath.startsWith("https://")) {
        LOG.debug("Config instructed to download the weights file, doing so.");
        try {
            modelWeightsPath = cachedDownload(cacheDir, URI.create(modelWeightsPath)).getAbsolutePath();
        } catch (IOException e) {
            throw new TikaConfigException(e.getMessage(), e);
        }
    } else {
        File modelFile = retrieveFile(modelWeightsPath);
        if (!modelFile.exists()) {
            LOG.error("modelWeights does not exist at :: {}", modelWeightsPath);
            return;
        }
        modelWeightsPath = modelFile.getAbsolutePath();
    }
    //STEP 2: resolve model JSON
    File modelJsonFile = retrieveFile(modelJsonPath);
    if (modelJsonFile == null || !modelJsonFile.exists()) {
        LOG.error("Could not locate file {}", modelJsonPath);
        return;
    }
    modelJsonPath = modelJsonFile.getAbsolutePath();
    //STEP 3: Load labels map
    try (InputStream stream = retrieveResource(labelFile)) {
        this.labelMap = loadClassIndex(stream);
    } catch (IOException | ParseException e) {
        LOG.error("Could not load labels map", e);
        return;
    }
    //STEP 4: initialize the graph
    try {
        this.imageLoader = new NativeImageLoader(imgHeight, imgWidth, imgChannels);
        LOG.info("Going to load Inception network...");
        long st = System.currentTimeMillis();
        this.graph = KerasModelImport.importKerasModelAndWeights(modelJsonPath, modelWeightsPath, false);
        long time = System.currentTimeMillis() - st;
        LOG.info("Loaded the Inception model. Time taken={}ms", time);
    } catch (IOException | InvalidKerasConfigurationException | UnsupportedKerasConfigurationException e) {
        throw new TikaConfigException(e.getMessage(), e);
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) TikaConfigException(org.apache.tika.exception.TikaConfigException) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException) UnsupportedKerasConfigurationException(org.deeplearning4j.nn.modelimport.keras.UnsupportedKerasConfigurationException) File(java.io.File) InvalidKerasConfigurationException(org.deeplearning4j.nn.modelimport.keras.InvalidKerasConfigurationException) NativeImageLoader(org.datavec.image.loader.NativeImageLoader)

Example 57 with ParseException

use of org.json.simple.parser.ParseException in project elastic-core-maven by OrdinaryDude.

the class PeerServlet method process.

/**
 * Process the peer request
 *
 * @param   peer                Peer
 * @param   inputReader         Input reader
 * @return                      JSON response
 */
private JSONStreamAware process(PeerImpl peer, Reader inputReader) {
    // 
    if (peer.isBlacklisted()) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("error", Errors.BLACKLISTED);
        jsonObject.put("cause", peer.getBlacklistingCause());
        return jsonObject;
    }
    Peers.addPeer(peer);
    // 
    try (CountingInputReader cr = new CountingInputReader(inputReader, Peers.MAX_REQUEST_SIZE)) {
        JSONObject request = (JSONObject) JSONValue.parseWithException(cr);
        peer.updateDownloadedVolume(cr.getCount());
        if (request.get("protocol") == null || ((Number) request.get("protocol")).intValue() != 1) {
            Logger.logDebugMessage("Unsupported protocol " + request.get("protocol"));
            return UNSUPPORTED_PROTOCOL;
        }
        PeerRequestHandler peerRequestHandler = peerRequestHandlers.get((String) request.get("requestType"));
        if (peerRequestHandler == null) {
            return UNSUPPORTED_REQUEST_TYPE;
        }
        if (peer.getState() == Peer.State.DISCONNECTED) {
            peer.setState(Peer.State.CONNECTED);
        }
        if (peer.getVersion() == null && !"getInfo".equals(request.get("requestType"))) {
            return SEQUENCE_ERROR;
        }
        if (!peer.isInbound()) {
            if (Peers.hasTooManyInboundPeers()) {
                return MAX_INBOUND_CONNECTIONS;
            }
            Peers.notifyListeners(peer, Peers.Event.ADD_INBOUND);
        }
        peer.setLastInboundRequest(Nxt.getEpochTime());
        if (peerRequestHandler.rejectWhileDownloading()) {
            if (blockchainProcessor.isDownloading()) {
                return DOWNLOADING;
            }
            if (Constants.isLightClient) {
                return LIGHT_CLIENT;
            }
        }
        return peerRequestHandler.processRequest(request, peer);
    } catch (RuntimeException | ParseException | IOException e) {
        Logger.logDebugMessage("Error processing POST request: " + e.toString());
        peer.blacklist(e);
        return error(e);
    }
}
Also used : JSONObject(org.json.simple.JSONObject) CountingInputReader(org.xel.util.CountingInputReader) ParseException(org.json.simple.parser.ParseException) IOException(java.io.IOException)

Example 58 with ParseException

use of org.json.simple.parser.ParseException in project elastic-core-maven by OrdinaryDude.

the class ParameterParser method parseTransaction.

public static Transaction.Builder parseTransaction(String transactionJSON, String transactionBytes, String prunableAttachmentJSON) throws ParameterException {
    if (transactionBytes == null && transactionJSON == null) {
        throw new ParameterException(MISSING_TRANSACTION_BYTES_OR_JSON);
    }
    if (transactionBytes != null && transactionJSON != null) {
        throw new ParameterException(either("transactionBytes", "transactionJSON"));
    }
    if (prunableAttachmentJSON != null && transactionBytes == null) {
        throw new ParameterException(JSONResponses.missing("transactionBytes"));
    }
    if (transactionJSON != null) {
        try {
            JSONObject json = (JSONObject) JSONValue.parseWithException(transactionJSON);
            return Nxt.newTransactionBuilder(json);
        } catch (NxtException.ValidationException | RuntimeException | ParseException e) {
            Logger.logDebugMessage(e.getMessage(), e);
            JSONObject response = new JSONObject();
            JSONData.putException(response, e, "Incorrect transactionJSON");
            throw new ParameterException(response);
        }
    } else {
        try {
            byte[] bytes = Convert.parseHexString(transactionBytes);
            JSONObject prunableAttachments = prunableAttachmentJSON == null ? null : (JSONObject) JSONValue.parseWithException(prunableAttachmentJSON);
            return Nxt.newTransactionBuilder(bytes, prunableAttachments);
        } catch (NxtException.ValidationException | RuntimeException | ParseException e) {
            Logger.logDebugMessage(e.getMessage(), e);
            JSONObject response = new JSONObject();
            JSONData.putException(response, e, "Incorrect transactionBytes");
            throw new ParameterException(response);
        }
    }
}
Also used : JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException)

Example 59 with ParseException

use of org.json.simple.parser.ParseException in project stocator by SparkTC.

the class PasswordScopeAccessProvider method authenticate.

/**
 * Authentication logic
 *
 * @return Access JOSS access object
 */
@Override
public Access authenticate() {
    try {
        JSONObject user = new JSONObject();
        user.put("id", mUserId);
        user.put("password", mPassword);
        JSONObject password = new JSONObject();
        password.put("user", user);
        JSONArray methods = new JSONArray();
        methods.add("password");
        JSONObject identity = new JSONObject();
        identity.put("methods", methods);
        identity.put("password", password);
        JSONObject project = new JSONObject();
        project.put("id", mProjectId);
        JSONObject scope = new JSONObject();
        scope.put("project", project);
        JSONObject auth = new JSONObject();
        auth.put("identity", identity);
        auth.put("scope", scope);
        JSONObject requestBody = new JSONObject();
        requestBody.put("auth", auth);
        HttpURLConnection connection = (HttpURLConnection) new URL(mAuthUrl).openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Content-Type", "application/json");
        OutputStream output = connection.getOutputStream();
        output.write(requestBody.toString().getBytes());
        HttpStatusChecker.verifyCode(STATUS_CHECKERS, connection.getResponseCode());
        final String res;
        try (final BufferedReader bufReader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
            res = bufReader.readLine();
        }
        JSONParser parser = new JSONParser();
        JSONObject jsonResponse = (JSONObject) parser.parse(res);
        String token = connection.getHeaderField("X-Subject-Token");
        PasswordScopeAccess access = new PasswordScopeAccess(jsonResponse, token, mPrefferedRegion);
        connection.disconnect();
        return access;
    } catch (IOException | ParseException e) {
        LOG.error(e.getMessage());
        throw new CommandException("Unable to execute the HTTP call or to convert the HTTP Response", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) OutputStream(java.io.OutputStream) JSONArray(org.json.simple.JSONArray) IOException(java.io.IOException) CommandException(org.javaswift.joss.exception.CommandException) URL(java.net.URL) HttpURLConnection(java.net.HttpURLConnection) JSONObject(org.json.simple.JSONObject) BufferedReader(java.io.BufferedReader) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException)

Example 60 with ParseException

use of org.json.simple.parser.ParseException in project metron by apache.

the class JoinBoltTest method parseMessages.

@Before
public void parseMessages() {
    JSONParser parser = new JSONParser();
    try {
        joinedMessage = (JSONObject) parser.parse(joinedMessageString);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    joinBolt = new StandAloneJoinBolt("zookeeperUrl");
    joinBolt.setCuratorFramework(client);
    joinBolt.setZKCache(cache);
}
Also used : JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) Before(org.junit.Before)

Aggregations

ParseException (org.json.simple.parser.ParseException)64 JSONObject (org.json.simple.JSONObject)41 JSONParser (org.json.simple.parser.JSONParser)39 HashMap (java.util.HashMap)18 JSONArray (org.json.simple.JSONArray)18 IOException (java.io.IOException)16 ArrayList (java.util.ArrayList)10 Map (java.util.Map)10 List (java.util.List)7 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)7 InputStreamReader (java.io.InputStreamReader)6 File (java.io.File)5 MapLayer (au.org.emii.portal.menu.MapLayer)4 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)4 MapLayerMetadata (au.org.emii.portal.menu.MapLayerMetadata)3 BufferedReader (java.io.BufferedReader)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 LocalDateTime (java.time.LocalDateTime)3