/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $Id: GetResource.java 526603 2007-04-08 21:26:04Z vgritsenko $
*/
package org.apache.xindice.server.rpc.messages;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xindice.core.Collection;
import org.apache.xindice.server.rpc.RPCDefaultMessage;
import org.apache.xindice.util.SymbolSerializer;
import org.apache.xindice.xml.TextWriter;
import org.apache.xindice.xml.dom.CompressedDocument;
import org.w3c.dom.Document;
import java.util.Hashtable;
/**
*
* @version $Revision: 526603 $, $Date: 2007-04-08 17:26:04 -0400 (Sun, 08 Apr 2007) $
*/
public class GetResource extends RPCDefaultMessage {
private static final Log log = LogFactory.getLog(GetResource.class);
public Hashtable execute(Hashtable message) throws Exception {
if (!message.containsKey(COLLECTION)) {
throw new Exception(MISSING_COLLECTION_PARAM);
}
if (!message.containsKey(NAME)) {
throw new Exception(MISSING_NAME_PARAM);
}
Collection col = getCollection((String) message.get(COLLECTION));
Object obj = col.getEntry(message.get(NAME));
Hashtable result = new Hashtable();
if (obj == null) {
// Return empty result
} else if (obj instanceof byte[]) {
// Binary resource
result.put(RESULT, obj);
} else if (message.containsKey(COMPRESSED)) {
SymbolSerializer symbolSerializer = null;
try {
symbolSerializer = new SymbolSerializer(col.getSymbols());
} catch (Exception e) {
// It's ok (in theory) for a Collection to have no symbol table
if (log.isWarnEnabled()) {
log.warn("ignored exception", e);
}
}
long timestamp = 1;
/* TODO: Timestamp optimization.
Longs are causing problems with XML-RPC
so we'll try to get everything working without them.
if (!message.containsKey(TIMESTAMP)) {
throw new Exception(MISSING_TIMESTAMP_PARAM);
}
int timestamp = ((Integer) message.get("timestamp")).intValue();
*/
// Document might be compressed (with bytes) or not. In a latter case, convert to string.
Document doc = (Document) obj;
if (/*( timestamp != -1) &&*/ symbolSerializer != null && ((CompressedDocument) doc).getDataBytes() != null) {
result.put(RESULT,
symbolSerializer.convertFromDocument(doc, timestamp));
} else {
result.put(RESULT, TextWriter.toString(doc));
}
} else {
Document doc = (Document) obj;
result.put(RESULT, TextWriter.toString(doc));
}
return result;
}
}