}
if( var.getValue().getItemType() != Type.JAVA_OBJECT ) {
throw( new XPathException( this, "Variable $response is not bound to a Java object." ) );
}
final JavaObjectValue response = (JavaObjectValue)var.getValue().itemAt( 0 );
//get parameters
final String name = getArgument( 0 ).eval( contextSequence, contextItem ).getStringValue();
final String value = getArgument( 1 ).eval( contextSequence, contextItem ).getStringValue();
Sequence ageSeq = Sequence.EMPTY_SEQUENCE;
Sequence secureSeq = Sequence.EMPTY_SEQUENCE;
Sequence domainSeq = Sequence.EMPTY_SEQUENCE;
Sequence pathSeq = Sequence.EMPTY_SEQUENCE;
int maxAge = -1;
if( getArgumentCount() > 2 ) {
ageSeq = getArgument( 2 ).eval( contextSequence, contextItem );
secureSeq = getArgument( 3 ).eval( contextSequence, contextItem );
if( !ageSeq.isEmpty() ) {
final Duration duration = ( (DurationValue)ageSeq.itemAt( 0 ) ).getCanonicalDuration();
maxAge = (int)( duration.getTimeInMillis( new Date( System.currentTimeMillis() ) ) / 1000L );
}
if( getArgumentCount() > 4 ) {
domainSeq = getArgument( 4 ).eval( contextSequence, contextItem );
pathSeq = getArgument( 5 ).eval( contextSequence, contextItem );
}
}
//set response header
if( response.getObject() instanceof ResponseWrapper ) {
switch( getArgumentCount() ) {
case 2: {
( (ResponseWrapper)response.getObject() ).addCookie( name, value );
break;
}
case 4: {
if( secureSeq.isEmpty() ) {
( (ResponseWrapper)response.getObject() ).addCookie( name, value, maxAge );
} else {
( (ResponseWrapper)response.getObject() ).addCookie( name, value, maxAge, ( (BooleanValue)secureSeq.itemAt( 0 ) ).effectiveBooleanValue() );
}
break;
}
case 6: {
boolean secure = false;
String domain = null;
String path = null;
if( !secureSeq.isEmpty() ) {
secure = ( (BooleanValue)secureSeq.itemAt( 0 ) ).effectiveBooleanValue();
}
if( !domainSeq.isEmpty() ) {
domain = domainSeq.itemAt( 0 ).getStringValue();
}
if( !pathSeq.isEmpty() ) {
path = pathSeq.itemAt( 0 ).getStringValue();
}
( (ResponseWrapper)response.getObject() ).addCookie( name, value, maxAge, secure, domain, path );
break;
}
}
} else {
throw( new XPathException( this, "Type error: variable $response is not bound to a response object" ) );