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;
}
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]);
}
}
}
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();
}
}
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();
}
}
}
}
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);
}
Aggregations