if ( methodName == null || methodName.length() == 0 ) {
Object val = instance.getProperty(DEFAULT_METHOD);
if ( val instanceof String )
methodName = (String)val;
else
throw new RequestException("No method name was set");
}
instance.setMethodName(methodName);
BrokerContext context = Broker.getInstance().getBrokerContext();
Discovery disc = context.getDiscovery();
String[] paramNames = disc.listParams(methodName);
boolean postUsed = false;
for ( int i = 0; paramNames != null && i < paramNames.length; i++ ) {
boolean array = disc.isParamArray(methodName, i);
String[] vals = request.getValues(paramNames[i]);
if ( vals != null ) {
if ( array )
instance.setParameter(i, vals);
else
instance.setParameter(i, vals[0]);
}
else if ( !postUsed && request.hasContent() ) {
// If there is POST data, and this parameter is a String, Document,
// or byte array, then let us assume that the POST data is intended
// for this one.
try {
InputStream is = request.getInputStream();
ByteArrayOutputStream bos = new ByteArrayOutputStream(BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int size;
do {
size = is.read(buffer);
if ( size > 0 )
bos.write(buffer, 0, size);
}
while ( size != -1 );
instance.setParameter(i, bos.toByteArray());
postUsed = true;
}
catch ( IOException e ) {
/** @todo Hmmm */
}
}
else
instance.setParameter(i, null);
}
// Initially set the MIME type in case we get an HTTP 403 Exception
ID id = context.getId();
String contentType = getMimeType(id);
if ( contentType != null )
response.setHeader(HTTP.HEADER_CONTENT_TYPE, contentType);
Object o = instance.invoke();
Object type = context.getProperty(CONTENT_TYPE);
if ( type == null )
type = instance.getProperty(CONTENT_TYPE+'_'+methodName);
if ( type instanceof String ) {
contentType = (String)type;
response.setHeader(HTTP.HEADER_CONTENT_TYPE, contentType);
}
if ( contentType == null ) {
if ( o instanceof Node || o instanceof Element )
contentType = Headers.TYPE_TEXT_XML;
else
contentType = Headers.TYPE_TEXT_PLAIN;
response.setHeader(HTTP.HEADER_CONTENT_TYPE, contentType);
}
try {
OutputStream os = response.getOutputStream();
writeValue(os, o);
os.flush(); // Is this needed?
os.close(); // Is this needed?
response.close();
}
catch ( IOException e ) {
throw new RequestException(e.getMessage());
}
}