use of lucee.runtime.type.StructImpl in project Lucee by lucee.
the class HttpUtil method getAttributesAsStruct.
public static Struct getAttributesAsStruct(HttpServletRequest req) {
Struct attributes = new StructImpl();
Enumeration e = req.getAttributeNames();
String name;
while (e.hasMoreElements()) {
// MUST (hhlhgiug) can throw ConcurrentModificationException
name = (String) e.nextElement();
if (name != null)
attributes.setEL(name, req.getAttribute(name));
}
return attributes;
}
use of lucee.runtime.type.StructImpl in project Lucee by lucee.
the class MultiPartResponseUtils method extractHeaders.
private static Struct extractHeaders(String rawHeaders) throws PageException {
Struct result = new StructImpl();
String[] headers = ListUtil.listToStringArray(rawHeaders, '\n');
for (String rawHeader : headers) {
String[] headerArray = ListUtil.listToStringArray(rawHeader, ':');
String headerName = headerArray[0];
if (!StringUtil.isEmpty(headerName, true)) {
String value = StringUtils.join(Arrays.copyOfRange(headerArray, 1, headerArray.length), ":").trim();
result.set(headerName, value);
}
}
return result;
}
use of lucee.runtime.type.StructImpl in project Lucee by lucee.
the class AxisCaster method toLuceeType.
public static Object toLuceeType(PageContext pc, String customType, Object value) throws PageException {
pc = ThreadLocalPageContext.get(pc);
if (pc != null && value instanceof Pojo) {
if (!StringUtil.isEmpty(customType)) {
Component cfc = toComponent(pc, (Pojo) value, customType, null);
if (cfc != null)
return cfc;
}
/*
// try package/class name as component name
String compPath=value.getClass().getName();
Component cfc = toComponent(pc, (Pojo)value, compPath, null);
if(cfc!=null) return cfc;
// try class name as component name
compPath=ListUtil.last(compPath, '.');
cfc = toComponent(pc, (Pojo)value, compPath, null);
if(cfc!=null) return cfc;
*/
}
if (value instanceof Date || value instanceof Calendar) {
// do not change to caster.isDate
return Caster.toDate(value, null);
}
if (value instanceof Object[]) {
Object[] arr = (Object[]) value;
if (!ArrayUtil.isEmpty(arr)) {
boolean allTheSame = true;
// byte
if (arr[0] instanceof Byte) {
for (int i = 1; i < arr.length; i++) {
if (!(arr[i] instanceof Byte)) {
allTheSame = false;
break;
}
}
if (allTheSame) {
byte[] bytes = new byte[arr.length];
for (int i = 0; i < arr.length; i++) {
bytes[i] = Caster.toByteValue(arr[i]);
}
return bytes;
}
}
}
}
if (value instanceof Byte[]) {
Byte[] arr = (Byte[]) value;
if (!ArrayUtil.isEmpty(arr)) {
byte[] bytes = new byte[arr.length];
for (int i = 0; i < arr.length; i++) {
bytes[i] = arr[i].byteValue();
}
return bytes;
}
}
if (value instanceof byte[]) {
return value;
}
if (Decision.isArray(value)) {
Array a = Caster.toArray(value);
int len = a.size();
Object o;
String ct;
for (int i = 1; i <= len; i++) {
o = a.get(i, null);
if (o != null) {
ct = customType != null && customType.endsWith("[]") ? customType.substring(0, customType.length() - 2) : null;
a.setEL(i, toLuceeType(pc, ct, o));
}
}
return a;
}
if (value instanceof Map) {
Struct sct = new StructImpl();
Iterator it = ((Map) value).entrySet().iterator();
Map.Entry entry;
while (it.hasNext()) {
entry = (Entry) it.next();
sct.setEL(Caster.toString(entry.getKey()), toLuceeType(pc, null, entry.getValue()));
}
return sct;
// return StructUtil.copyToStruct((Map)value);
}
if (isQueryBean(value)) {
QueryBean qb = (QueryBean) value;
String[] strColumns = qb.getColumnList();
Object[][] data = qb.getData();
int recorcount = data.length;
Query qry = new QueryImpl(strColumns, recorcount, "QueryBean");
QueryColumn[] columns = new QueryColumn[strColumns.length];
for (int i = 0; i < columns.length; i++) {
columns[i] = qry.getColumn(strColumns[i]);
}
int row;
for (row = 1; row <= recorcount; row++) {
for (int i = 0; i < columns.length; i++) {
columns[i].set(row, toLuceeType(pc, null, data[row - 1][i]));
}
}
return qry;
}
if (Decision.isQuery(value)) {
Query q = Caster.toQuery(value);
int recorcount = q.getRecordcount();
String[] strColumns = q.getColumns();
QueryColumn col;
int row;
for (int i = 0; i < strColumns.length; i++) {
col = q.getColumn(strColumns[i]);
for (row = 1; row <= recorcount; row++) {
col.set(row, toLuceeType(pc, null, col.get(row, null)));
}
}
return q;
}
return value;
}
use of lucee.runtime.type.StructImpl in project Lucee by lucee.
the class Axis1Client method _call.
private Object _call(PageContext pc, Config secondChanceConfig, String methodName, Struct namedArguments, Object[] arguments) throws PageException, ServiceException, RemoteException {
ApplicationContextSupport acs = (ApplicationContextSupport) pc.getApplicationContext();
javax.wsdl.Service service = getWSDLService();
Service axisService = new Service(parser, service.getQName());
axisService.setMaintainSession(acs.getWSMaintainSession());
TypeMappingUtil.registerDefaults(axisService.getTypeMappingRegistry());
Port port = WSUtil.getSoapPort(service);
Binding binding = port.getBinding();
SymbolTable symbolTable = parser.getSymbolTable();
BindingEntry bEntry = symbolTable.getBindingEntry(binding.getQName());
// get matching operation/method
Iterator<Entry<Operation, Parameters>> itr = bEntry.getParameters().entrySet().iterator();
Operation operation = null;
Entry<Operation, Parameters> e;
Parameters parameters = null;
while (itr.hasNext()) {
e = itr.next();
if (e.getKey().getName().equalsIgnoreCase(methodName)) {
operation = e.getKey();
parameters = e.getValue();
break;
}
}
// no operation found!
if (operation == null || parameters == null) {
// get array of existing methods
Set<Operation> set = bEntry.getParameters().keySet();
Iterator<Operation> it = set.iterator();
Collection.Key[] keys = new Collection.Key[set.size()];
int index = 0;
while (it.hasNext()) {
keys[index++] = KeyImpl.init(it.next().getName());
}
throw new RPCException(ExceptionUtil.similarKeyMessage(keys, methodName, "method/operation", "methods/operations", null, true) + " Webservice: " + wsdlUrl);
}
org.apache.axis.client.Call call = (Call) axisService.createCall(QName.valueOf(port.getName()), QName.valueOf(operation.getName()));
if (!StringUtil.isEmpty(username, true)) {
call.setUsername(username);
call.setPassword(password);
}
org.apache.axis.encoding.TypeMapping tm = call.getTypeMapping();
Vector<String> inNames = new Vector<String>();
Vector<Parameter> inTypes = new Vector<Parameter>();
Vector<String> outNames = new Vector<String>();
Vector<Parameter> outTypes = new Vector<Parameter>();
Parameter p = null;
for (int j = 0; j < parameters.list.size(); j++) {
p = (Parameter) parameters.list.get(j);
map(pc, symbolTable, secondChanceConfig, tm, p.getType());
switch(p.getMode()) {
case Parameter.IN:
inNames.add(p.getQName().getLocalPart());
inTypes.add(p);
break;
case Parameter.OUT:
outNames.add(p.getQName().getLocalPart());
outTypes.add(p);
break;
case Parameter.INOUT:
inNames.add(p.getQName().getLocalPart());
inTypes.add(p);
outNames.add(p.getQName().getLocalPart());
outTypes.add(p);
break;
}
}
// set output type
if (parameters.returnParam != null) {
QName rtnQName = parameters.returnParam.getQName();
// TypeEntry rtnType = parameters.returnParam.getType();
map(pc, symbolTable, secondChanceConfig, tm, parameters.returnParam.getType());
outNames.add(rtnQName.getLocalPart());
outTypes.add(parameters.returnParam);
}
// get timezone
TimeZone tz;
if (pc == null)
tz = ThreadLocalPageContext.getTimeZone(secondChanceConfig);
else
tz = ThreadLocalPageContext.getTimeZone(pc);
// check arguments
Object[] inputs = new Object[inNames.size()];
if (arguments != null) {
if (inNames.size() != arguments.length)
throw new RPCException("Invalid arguments count for operation " + methodName + " (" + arguments.length + " instead of " + inNames.size() + ")");
for (int pos = 0; pos < inNames.size(); pos++) {
p = inTypes.get(pos);
inputs[pos] = getArgumentData(tm, tz, p, arguments[pos]);
}
} else {
UDFUtil.argumentCollection(namedArguments);
if (inNames.size() != namedArguments.size())
throw new RPCException("Invalid arguments count for operation " + methodName + " (" + namedArguments.size() + " instead of " + inNames.size() + ")");
Object arg;
for (int pos = 0; pos < inNames.size(); pos++) {
p = inTypes.get(pos);
arg = namedArguments.get(KeyImpl.init(p.getName()), null);
if (arg == null) {
throw new RPCException("Invalid arguments for operation " + methodName, getErrorDetailForArguments(inNames.toArray(new String[inNames.size()]), CollectionUtil.keysAsString(namedArguments)));
}
inputs[pos] = getArgumentData(tm, tz, p, arg);
}
}
Object ret = null;
// add header
if (headers != null && !headers.isEmpty()) {
Iterator<SOAPHeaderElement> it = headers.iterator();
while (it.hasNext()) {
call.addHeader(it.next());
}
}
try {
ret = invoke(call, inputs);
} catch (AxisFault af) {
boolean rethrow = true;
Throwable cause = af.getCause();
if (cause != null) {
/*
// first check if that missing type is around
String[] notFound=new String[]{"could not find deserializer for type","No deserializer for"};
int index;
if(msg!=null)for(int i=0; i<notFound.length;i++) {
if((index=msg.indexOf(notFound[i]))==-1)continue;;
String raw=msg.substring(index+notFound[i].length()+1).trim();
QName qn = QName.valueOf(raw);
print.e(qn.getLocalPart());
print.e(qn.getNamespaceURI());
Type type = symbolTable.getType(qn);
if(type!=null) {
map(pc,secondChanceConfig,call.getTypeMapping(),type);
ret = invoke(call,inputs);
rethrow=false;
}
}*/
// get the missing types from the SOAP Body, if possible
String msg = cause.getMessage();
// if(StringUtil.indexOfIgnoreCase(msg, "deserializer")!=-1) {
try {
InputSource is = new InputSource(new StringReader(call.getResponseMessage().getSOAPPartAsString()));
Document doc = XMLUtil.parse(is, null, false);
Element body = XMLUtil.getChildWithName("soapenv:Body", doc.getDocumentElement());
Vector types = SOAPUtil.getTypes(body, symbolTable);
map(pc, symbolTable, secondChanceConfig, (org.apache.axis.encoding.TypeMapping) (axisService.getTypeMappingRegistry().getDefaultTypeMapping()), types);
ret = invoke(call, inputs);
rethrow = false;
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
}
// }
}
if (rethrow)
throw af;
}
last = call;
if (outNames.size() <= 1)
return AxisCaster.toLuceeType(null, ret);
// getParamData((org.apache.axis.client.Call)call,parameters.returnParam,ret);
Map outputs = call.getOutputParams();
Struct sct = new StructImpl();
for (int pos = 0; pos < outNames.size(); pos++) {
String name = outNames.get(pos);
// print.ln(name);
Object value = outputs.get(name);
if (value == null && pos == 0) {
sct.setEL(name, AxisCaster.toLuceeType(null, ret));
} else {
sct.setEL(name, AxisCaster.toLuceeType(null, value));
}
}
return sct;
}
use of lucee.runtime.type.StructImpl in project Lucee by lucee.
the class HttpGetWithBody method setUnknownHost.
private void setUnknownHost(Struct cfhttp, Throwable t) {
cfhttp.setEL(CHARSET, "");
cfhttp.setEL(ERROR_DETAIL, "Unknown host: " + t.getMessage());
cfhttp.setEL(KeyConstants._filecontent, "Connection Failure");
cfhttp.setEL(KeyConstants._header, "");
cfhttp.setEL(KeyConstants._mimetype, "Unable to determine MIME type of file.");
cfhttp.setEL(RESPONSEHEADER, new StructImpl());
cfhttp.setEL(STATUSCODE, "Connection Failure. Status code unavailable.");
cfhttp.setEL(STATUS_CODE, new Double(0));
cfhttp.setEL(STATUS_TEXT, "Connection Failure");
cfhttp.setEL(KeyConstants._text, Boolean.TRUE);
}
Aggregations