Search in sources :

Example 36 with DecoderException

use of org.apache.commons.codec.DecoderException in project fess by codelibs.

the class EncodingFilter method parseQueryString.

protected Map<String, String[]> parseQueryString(final String queryString, final String enc) throws IOException {
    final Map<String, List<String>> paramListMap = new HashMap<>();
    final String[] pairs = queryString.split("&");
    try {
        for (final String pair : pairs) {
            final int pos = pair.indexOf('=');
            if (pos >= 0) {
                final String key = urlCodec.decode(pair.substring(0, pos), enc);
                List<String> list = paramListMap.get(key);
                if (list == null) {
                    list = new ArrayList<>();
                    paramListMap.put(key, list);
                }
                if (pos + 1 < pair.length()) {
                    list.add(urlCodec.decode(pair.substring(pos + 1), enc));
                } else {
                    list.add(StringUtil.EMPTY);
                }
            } else {
                final String key = urlCodec.decode(pair, enc);
                List<String> list = paramListMap.get(key);
                if (list == null) {
                    list = new ArrayList<>();
                    paramListMap.put(key, list);
                }
                list.add(StringUtil.EMPTY);
            }
        }
    } catch (final DecoderException e) {
        throw new IOException(e);
    }
    final Map<String, String[]> paramMap = new HashMap<>(paramListMap.size());
    for (final Map.Entry<String, List<String>> entry : paramListMap.entrySet()) {
        final List<String> list = entry.getValue();
        paramMap.put(entry.getKey(), list.toArray(new String[list.size()]));
    }
    return paramMap;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) IOException(java.io.IOException) DecoderException(org.apache.commons.codec.DecoderException) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 37 with DecoderException

use of org.apache.commons.codec.DecoderException in project v7files by thiloplanz.

the class CopyCommand method main.

public static void main(String[] args) throws MongoException, IOException {
    if (args.length != 5) {
        System.err.println("Copy files");
        System.err.println("  by name:   copy <sourceRoot> <sourcePath> <targetRoot> <targetPath>");
        System.err.println("  by hash:   copy -sha <shaHex> <targetRoot> <targetPath>");
        System.exit(1);
    }
    DB db = Configuration.getMongo().getDB(Configuration.getProperty("mongo.db"));
    V7GridFS fs = new V7GridFS(db);
    if ("-sha".equals(args[1])) {
        MongoContentStorage storage = new MongoContentStorage(db);
        String sha = args[2];
        try {
            ContentSHA file = findContentByPrefix(storage, sha);
            if (file == null)
                throw new FileNotFoundException("-sha " + sha);
            String[] path = getPath(args[3], args[4]);
            createFile(fs, file, path, null);
        } catch (DecoderException e) {
            throw new IllegalArgumentException("invalid parameter :" + sha + " is not a hex-encoded SHA-1 prefix");
        }
    } else {
        String[] srcPath = getPath(args[1], args[2]);
        String[] targetPath = getPath(args[3], args[4]);
        V7File src = fs.getFile(srcPath);
        if (src == null) {
            throw new FileNotFoundException(args[1] + " " + args[2]);
        }
        if (src.hasContent()) {
            createFile(fs, src.getContentPointer(), targetPath, src.getContentType());
        } else {
            V7File existing = fs.getFile(targetPath);
            if (existing != null) {
                throw new IOException("copy target " + targetPath + " already exists");
            }
            V7File parent = getParent(fs, targetPath);
            src.copyTo(parent.getId(), targetPath[targetPath.length - 1]);
        }
    }
}
Also used : DecoderException(org.apache.commons.codec.DecoderException) MongoContentStorage(v7db.files.mongodb.MongoContentStorage) V7File(v7db.files.mongodb.V7File) ContentSHA(v7db.files.spi.ContentSHA) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) DB(com.mongodb.DB) V7GridFS(v7db.files.mongodb.V7GridFS)

Example 38 with DecoderException

use of org.apache.commons.codec.DecoderException in project voldemort by voldemort.

the class VoldemortAdminTool method executeQueryKey.

private static void executeQueryKey(final Integer nodeId, AdminClient adminClient, List<String> storeNames, String keyString, String keyFormat) throws IOException {
    // decide queryingNode(s) for Key
    List<Integer> queryingNodes = new ArrayList<Integer>();
    if (nodeId < 0) {
        // means all nodes
        for (Node node : adminClient.getAdminClientCluster().getNodes()) {
            queryingNodes.add(node.getId());
        }
    } else {
        queryingNodes.add(nodeId);
    }
    // get basic info
    List<StoreDefinition> storeDefinitionList = getStoreDefinitions(adminClient, nodeId);
    Map<String, StoreDefinition> storeDefinitions = new HashMap<String, StoreDefinition>();
    for (StoreDefinition storeDef : storeDefinitionList) {
        storeDefinitions.put(storeDef.getName(), storeDef);
    }
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    // iterate through stores
    for (final String storeName : storeNames) {
        // store definition
        StoreDefinition storeDefinition = storeDefinitions.get(storeName);
        if (storeDefinition == null) {
            throw new StoreNotFoundException("Store " + storeName + " not found");
        }
        out.write("STORE_NAME: " + storeDefinition.getName() + "\n");
        // k-v serializer
        final SerializerDefinition keySerializerDef = storeDefinition.getKeySerializer();
        final SerializerDefinition valueSerializerDef = storeDefinition.getValueSerializer();
        SerializerFactory serializerFactory = new DefaultSerializerFactory();
        @SuppressWarnings("unchecked") final Serializer<Object> keySerializer = (Serializer<Object>) serializerFactory.getSerializer(keySerializerDef);
        @SuppressWarnings("unchecked") final Serializer<Object> valueSerializer = (Serializer<Object>) serializerFactory.getSerializer(valueSerializerDef);
        // compression strategy
        final CompressionStrategy keyCompressionStrategy;
        final CompressionStrategy valueCompressionStrategy;
        if (keySerializerDef != null && keySerializerDef.hasCompression()) {
            keyCompressionStrategy = new CompressionStrategyFactory().get(keySerializerDef.getCompression());
        } else {
            keyCompressionStrategy = null;
        }
        if (valueSerializerDef != null && valueSerializerDef.hasCompression()) {
            valueCompressionStrategy = new CompressionStrategyFactory().get(valueSerializerDef.getCompression());
        } else {
            valueCompressionStrategy = null;
        }
        if (keyCompressionStrategy == null) {
            out.write("KEY_COMPRESSION_STRATEGY: None\n");
        } else {
            out.write("KEY_COMPRESSION_STRATEGY: " + keyCompressionStrategy.getType() + "\n");
        }
        out.write("KEY_SERIALIZER_NAME: " + keySerializerDef.getName() + "\n");
        for (Map.Entry<Integer, String> entry : keySerializerDef.getAllSchemaInfoVersions().entrySet()) {
            out.write(String.format("KEY_SCHEMA VERSION=%d\n", entry.getKey()));
            out.write("====================================\n");
            out.write(entry.getValue());
            out.write("\n====================================\n");
        }
        out.write("\n");
        if (valueCompressionStrategy == null) {
            out.write("VALUE_COMPRESSION_STRATEGY: None\n");
        } else {
            out.write("VALUE_COMPRESSION_STRATEGY: " + valueCompressionStrategy.getType() + "\n");
        }
        out.write("VALUE_SERIALIZER_NAME: " + valueSerializerDef.getName() + "\n");
        for (Map.Entry<Integer, String> entry : valueSerializerDef.getAllSchemaInfoVersions().entrySet()) {
            out.write(String.format("VALUE_SCHEMA %d\n", entry.getKey()));
            out.write("====================================\n");
            out.write(entry.getValue());
            out.write("\n====================================\n");
        }
        out.write("\n");
        // although the streamingOps support multiple keys, we only query
        // one key here
        ByteArray key;
        try {
            if (keyFormat.equals("readable")) {
                Object keyObject;
                String keySerializerName = keySerializerDef.getName();
                if (isAvroSchema(keySerializerName)) {
                    Schema keySchema = Schema.parse(keySerializerDef.getCurrentSchemaInfo());
                    JsonDecoder decoder = new JsonDecoder(keySchema, keyString);
                    GenericDatumReader<Object> datumReader = new GenericDatumReader<Object>(keySchema);
                    keyObject = datumReader.read(null, decoder);
                } else if (keySerializerName.equals(DefaultSerializerFactory.JSON_SERIALIZER_TYPE_NAME)) {
                    JsonReader jsonReader = new JsonReader(new StringReader(keyString));
                    keyObject = jsonReader.read();
                } else {
                    keyObject = keyString;
                }
                key = new ByteArray(keySerializer.toBytes(keyObject));
            } else {
                key = new ByteArray(ByteUtils.fromHexString(keyString));
            }
        } catch (SerializationException se) {
            System.err.println("Error serializing key " + keyString);
            System.err.println("If this is a JSON key, you need to include escaped quotation marks in the command line if it is a string");
            se.printStackTrace();
            return;
        } catch (DecoderException de) {
            System.err.println("Error decoding key " + keyString);
            de.printStackTrace();
            return;
        } catch (IOException io) {
            System.err.println("Error parsing avro string " + keyString);
            io.printStackTrace();
            return;
        }
        boolean printedKey = false;
        // A Map<> could have been used instead of List<Entry<>> if
        // Versioned supported correct hash codes. Read the comment in
        // Versioned about the issue
        List<Entry<List<Versioned<byte[]>>, List<Integer>>> nodeValues = new ArrayList<Entry<List<Versioned<byte[]>>, List<Integer>>>();
        for (final Integer queryNodeId : queryingNodes) {
            Iterator<QueryKeyResult> iterator;
            iterator = adminClient.streamingOps.queryKeys(queryNodeId, storeName, Arrays.asList(key).iterator());
            final StringWriter stringWriter = new StringWriter();
            QueryKeyResult queryKeyResult = iterator.next();
            if (!printedKey) {
                // de-serialize and write key
                byte[] keyBytes = queryKeyResult.getKey().get();
                Object keyObject = keySerializer.toObject((null == keyCompressionStrategy) ? keyBytes : keyCompressionStrategy.inflate(keyBytes));
                writeVoldKeyOrValueInternal(keyBytes, keySerializer, keyCompressionStrategy, "KEY", out);
                printedKey = true;
            }
            // iterate through, de-serialize and write values
            if (queryKeyResult.hasValues() && queryKeyResult.getValues().size() > 0) {
                int elementId = -1;
                for (int i = 0; i < nodeValues.size(); i++) {
                    if (Objects.equal(nodeValues.get(i).getKey(), queryKeyResult.getValues())) {
                        elementId = i;
                        break;
                    }
                }
                if (elementId == -1) {
                    ArrayList<Integer> nodes = new ArrayList<Integer>();
                    nodes.add(queryNodeId);
                    nodeValues.add(new AbstractMap.SimpleEntry<List<Versioned<byte[]>>, List<Integer>>(queryKeyResult.getValues(), nodes));
                } else {
                    nodeValues.get(elementId).getValue().add(queryNodeId);
                }
                out.write(String.format("\nQueried node %d on store %s\n", queryNodeId, storeName));
                int versionCount = 0;
                if (queryKeyResult.getValues().size() > 1) {
                    out.write("VALUE " + versionCount + "\n");
                }
                for (Versioned<byte[]> versioned : queryKeyResult.getValues()) {
                    // write version
                    VectorClock version = (VectorClock) versioned.getVersion();
                    out.write("VECTOR_CLOCK_BYTE: " + ByteUtils.toHexString(version.toBytes()) + "\n");
                    out.write("VECTOR_CLOCK_TEXT: " + version.toString() + '[' + new Date(version.getTimestamp()).toString() + "]\n");
                    // write value
                    byte[] valueBytes = versioned.getValue();
                    writeVoldKeyOrValueInternal(valueBytes, valueSerializer, valueCompressionStrategy, "VALUE", out);
                    versionCount++;
                }
            } else // exception.
            if (queryKeyResult.hasException()) {
                boolean isInvalidMetadataException = queryKeyResult.getException() instanceof InvalidMetadataException;
                // you are querying only a single node.
                if (!isInvalidMetadataException || queryingNodes.size() == 1) {
                    out.write(String.format("\nNode %d on store %s returned exception\n", queryNodeId, storeName));
                    out.write(queryKeyResult.getException().toString());
                    out.write("\n====================================\n");
                }
            } else {
                if (queryingNodes.size() == 1) {
                    out.write(String.format("\nNode %d on store %s returned NULL\n", queryNodeId, storeName));
                    out.write("\n====================================\n");
                }
            }
            out.flush();
        }
        out.write("\n====================================\n");
        for (Map.Entry<List<Versioned<byte[]>>, List<Integer>> nodeValue : nodeValues) {
            out.write("Nodes with same Value " + Arrays.toString(nodeValue.getValue().toArray()));
            out.write("\n====================================\n");
        }
        if (nodeValues.size() > 1) {
            out.write("\n*** Multiple (" + nodeValues.size() + ") versions of key/value exist for the key ***\n");
        }
        out.flush();
    }
}
Also used : HashMap(java.util.HashMap) Node(voldemort.cluster.Node) Schema(org.apache.avro.Schema) InvalidMetadataException(voldemort.store.InvalidMetadataException) ArrayList(java.util.ArrayList) CompressionStrategy(voldemort.store.compress.CompressionStrategy) CompressionStrategyFactory(voldemort.store.compress.CompressionStrategyFactory) QueryKeyResult(voldemort.client.protocol.admin.QueryKeyResult) AbstractMap(java.util.AbstractMap) ByteArray(voldemort.utils.ByteArray) List(java.util.List) ArrayList(java.util.ArrayList) Serializer(voldemort.serialization.Serializer) StringSerializer(voldemort.serialization.StringSerializer) DefaultSerializerFactory(voldemort.serialization.DefaultSerializerFactory) JsonDecoder(org.apache.avro.io.JsonDecoder) Map(java.util.Map) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) Versioned(voldemort.versioning.Versioned) GenericDatumReader(org.apache.avro.generic.GenericDatumReader) BufferedWriter(java.io.BufferedWriter) StoreNotFoundException(com.sleepycat.persist.StoreNotFoundException) Entry(java.util.Map.Entry) StringWriter(java.io.StringWriter) StoreDefinition(voldemort.store.StoreDefinition) StringReader(java.io.StringReader) JsonReader(voldemort.serialization.json.JsonReader) DefaultSerializerFactory(voldemort.serialization.DefaultSerializerFactory) SerializerFactory(voldemort.serialization.SerializerFactory) SerializationException(voldemort.serialization.SerializationException) VectorClock(voldemort.versioning.VectorClock) IOException(java.io.IOException) Date(java.util.Date) DecoderException(org.apache.commons.codec.DecoderException) OutputStreamWriter(java.io.OutputStreamWriter) SerializerDefinition(voldemort.serialization.SerializerDefinition)

Example 39 with DecoderException

use of org.apache.commons.codec.DecoderException in project voldemort by voldemort.

the class KeyVersionFetcherCLI method sampleStore.

public boolean sampleStore(StoreDefinition storeDefinition) {
    String storeName = storeDefinition.getName();
    String keysFileName = inDir + System.getProperty("file.separator") + storeName + ".keys";
    File keysFile = new File(keysFileName);
    if (!keysFile.exists()) {
        logger.error("Keys file " + keysFileName + " does not exist!");
        return false;
    }
    String kvFileName = outDir + System.getProperty("file.separator") + storeName + ".kvs";
    File kvFile = new File(kvFileName);
    if (kvFile.exists()) {
        logger.info("Key-Version file " + kvFileName + " exists, so will not sample keys from file " + keysFileName + ".");
        return true;
    }
    BaseStoreRoutingPlan storeRoutingPlan = new BaseStoreRoutingPlan(cluster, storeDefinition);
    BufferedReader keyReader = null;
    BufferedWriter kvWriter = null;
    try {
        keyReader = new BufferedReader(new FileReader(keysFileName));
        kvWriter = new BufferedWriter(new FileWriter(kvFileName));
        boolean readAllKeys = false;
        while (!readAllKeys) {
            Queue<Future<String>> futureKVs = new LinkedList<Future<String>>();
            for (int numFetchTasks = 0; numFetchTasks < this.outputBatchSize; numFetchTasks++) {
                String keyLine = keyReader.readLine();
                if (keyLine == null) {
                    readAllKeys = true;
                    break;
                }
                byte[] keyInBytes = ByteUtils.fromHexString(keyLine.trim());
                FetchKeyVersionsTask kvFetcher = new FetchKeyVersionsTask(storeRoutingPlan, keyInBytes);
                Future<String> future = kvFetcherService.submit(kvFetcher);
                futureKVs.add(future);
            }
            if (futureKVs.size() > 0) {
                while (!futureKVs.isEmpty()) {
                    Future<String> future = futureKVs.poll();
                    String keyVersions = future.get();
                    kvWriter.append(keyVersions);
                }
            }
        }
        return true;
    } catch (DecoderException de) {
        logger.error("Could not decode key to sample for store " + storeName, de);
        return false;
    } catch (IOException ioe) {
        logger.error("IOException caught while sampling store " + storeName, ioe);
        return false;
    } catch (InterruptedException ie) {
        logger.error("InterruptedException caught while sampling store " + storeName, ie);
        return false;
    } catch (ExecutionException ee) {
        logger.error("Encountered an execution exception while sampling " + storeName, ee);
        ee.printStackTrace();
        return false;
    } finally {
        if (keyReader != null) {
            try {
                keyReader.close();
            } catch (IOException e) {
                logger.error("IOException caught while trying to close keyReader for store " + storeName, e);
                e.printStackTrace();
            }
        }
        if (kvWriter != null) {
            try {
                kvWriter.close();
            } catch (IOException e) {
                logger.error("IOException caught while trying to close kvWriter for store " + storeName, e);
                e.printStackTrace();
            }
        }
    }
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) LinkedList(java.util.LinkedList) BufferedWriter(java.io.BufferedWriter) DecoderException(org.apache.commons.codec.DecoderException) BufferedReader(java.io.BufferedReader) Future(java.util.concurrent.Future) FileReader(java.io.FileReader) BaseStoreRoutingPlan(voldemort.routing.BaseStoreRoutingPlan) ExecutionException(java.util.concurrent.ExecutionException) File(java.io.File)

Example 40 with DecoderException

use of org.apache.commons.codec.DecoderException in project hive by apache.

the class LoadSemanticAnalyzer method initializeFromURI.

private URI initializeFromURI(String fromPath, boolean isLocal) throws IOException, URISyntaxException, SemanticException {
    URI fromURI = new Path(fromPath).toUri();
    String fromScheme = fromURI.getScheme();
    String fromAuthority = fromURI.getAuthority();
    String path = fromURI.getPath();
    // directory
    if (!path.startsWith("/")) {
        if (isLocal) {
            try {
                path = new String(URLCodec.decodeUrl(new Path(System.getProperty("user.dir"), fromPath).toUri().toString().getBytes("US-ASCII")), "US-ASCII");
            } catch (DecoderException de) {
                throw new SemanticException("URL Decode failed", de);
            }
        } else {
            path = new Path(new Path("/user/" + System.getProperty("user.name")), path).toString();
        }
    }
    // set correct scheme and authority
    if (StringUtils.isEmpty(fromScheme)) {
        if (isLocal) {
            // file for local
            fromScheme = "file";
        } else {
            // use default values from fs.default.name
            URI defaultURI = FileSystem.get(conf).getUri();
            fromScheme = defaultURI.getScheme();
            fromAuthority = defaultURI.getAuthority();
        }
    }
    // if scheme is specified but not authority then use the default authority
    if ((!fromScheme.equals("file")) && StringUtils.isEmpty(fromAuthority)) {
        URI defaultURI = FileSystem.get(conf).getUri();
        fromAuthority = defaultURI.getAuthority();
    }
    LOG.debug(fromScheme + "@" + fromAuthority + "@" + path);
    return new URI(fromScheme, fromAuthority, path, null, null);
}
Also used : Path(org.apache.hadoop.fs.Path) DecoderException(org.apache.commons.codec.DecoderException) URI(java.net.URI)

Aggregations

DecoderException (org.apache.commons.codec.DecoderException)41 IOException (java.io.IOException)16 CharSequenceX (com.samourai.wallet.util.CharSequenceX)8 MnemonicException (org.bitcoinj.crypto.MnemonicException)8 JSONException (org.json.JSONException)7 DecryptionException (com.samourai.wallet.crypto.DecryptionException)6 ArrayList (java.util.ArrayList)6 Intent (android.content.Intent)5 ProgressDialog (android.app.ProgressDialog)4 HD_Wallet (com.samourai.wallet.hd.HD_Wallet)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 HashMap (java.util.HashMap)4 List (java.util.List)4 BufferedReader (java.io.BufferedReader)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 ByteBuffer (java.nio.ByteBuffer)3 Map (java.util.Map)3 Cipher (javax.crypto.Cipher)3 AddressFormatException (org.bitcoinj.core.AddressFormatException)3