org.wso2.balana.attr.TimeAttribute Java Examples

The following examples show how to use org.wso2.balana.attr.TimeAttribute. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TimeInRangeFunction.java    From balana with Apache License 2.0 5 votes vote down vote up
/**
 * Private helper method that is used to resolve the correct values for min and max. If an
 * explicit timezone is provided for either, then that value gets used. Otherwise we need to
 * pick the timezone the middle time is using, and move the other time into that timezone.
 */
private long resolveTime(TimeAttribute middleTime, TimeAttribute otherTime) {
    long time = otherTime.getMilliseconds();
    int tz = otherTime.getTimeZone();

    // if there's no explicit timezone, then the otherTime needs to
    // be shifted to the middleTime's timezone
    if (tz == TimeAttribute.TZ_UNSPECIFIED) {
        // the other time didn't specify a timezone, so we use the
        // timezone specified in the middle time...
        int middleTz = middleTime.getTimeZone();

        // ...and we get the default timezone from the otherTime
        tz = otherTime.getDefaultedTimeZone();

        // if there was no specified timezone for the middleTime, use
        // the default timezone for that too
        if (middleTz == TimeAttribute.TZ_UNSPECIFIED)
            middleTz = middleTime.getDefaultedTimeZone();

        // use the timezone to offset the time value, if the two aren't
        // already in the same timezone
        if (middleTz != tz) {
            time -= ((middleTz - tz) * MILLIS_PER_MINUTE);
            time = handleWrap(time);
        }
    }

    return time;
}
 
Example #2
Source File: CurrentEnvModule.java    From balana with Apache License 2.0 5 votes vote down vote up
/**
 * Handles requests for the current Time.
 */
private EvaluationResult handleTime(URI type, String issuer, EvaluationCtx context) {
    // make sure they're asking for a time attribute
    if (!type.toString().equals(TimeAttribute.identifier))
        return new EvaluationResult(BagAttribute.createEmptyBag(type));

    // get the value from the context
    return makeBag(context.getCurrentTime());
}
 
Example #3
Source File: TimeInRangeFunction.java    From balana with Apache License 2.0 4 votes vote down vote up
/**
 * Default constructor.
 */
public TimeInRangeFunction() {
    super(NAME, 0, TimeAttribute.identifier, false, 3, BooleanAttribute.identifier, false);
}
 
Example #4
Source File: TimeInRangeFunction.java    From balana with Apache License 2.0 4 votes vote down vote up
/**
 * Evaluates the time-in-range function, which takes three <code>TimeAttribute</code> values.
 * This function return true if the first value falls between the second and third values (ie.,
 * on or after the second time and on or before the third time). If no time zone is specified
 * for the second and/or third time value, then the timezone from the first time value is used.
 * This lets you say time-in-range(current-time, 9am, 5pm) and always have the evaluation happen
 * in your current-time timezone.
 * 
 * @param inputs a <code>List</code> of <code>Evaluatable</code> objects representing the
 *            arguments passed to the function
 * @param context the respresentation of the request
 * 
 * @return an <code>EvaluationResult</code> containing true or false
 */
public EvaluationResult evaluate(List inputs, EvaluationCtx context) {
    AttributeValue[] argValues = new AttributeValue[inputs.size()];
    EvaluationResult result = evalArgs(inputs, context, argValues);

    // check if any errors occured while resolving the inputs
    if (result != null)
        return result;

    // get the three time values
    TimeAttribute attr = (TimeAttribute) (argValues[0]);
    long middleTime = attr.getMilliseconds();
    long minTime = resolveTime(attr, (TimeAttribute) (argValues[1]));
    long maxTime = resolveTime(attr, (TimeAttribute) (argValues[2]));

    // first off, if the min and max are the same, then this can only
    // be true is the middle is also the same value
    if (minTime == maxTime)
        return EvaluationResult.getInstance(middleTime == minTime);

    // shift the minTime to 00:00:00 so we can do a normal comparison,
    // taking care to shift in the correct direction (left if the
    // maxTime is bigger, otherwise right), and making sure that we
    // handle any wrapping values for the middle time (the maxTime will
    // never wrap around 00:00:00 GMT as long as we're dealing with
    // windows of less than 24 hours)                                              ur

    // the amount we're shifting
    long shiftSpan;

    // figure out the right direction and get the shift amount
    if (minTime < maxTime)
        shiftSpan = -minTime;
    else
        shiftSpan = MILLIS_PER_DAY - minTime;

    // shift the maxTime and the middleTime
    maxTime = maxTime + shiftSpan;
    middleTime = handleWrap(middleTime + shiftSpan);

    // we're in the range if the middle is now between 0 and maxTime
    return EvaluationResult.getInstance((middleTime >= 0) && (middleTime <= maxTime));
}
 
Example #5
Source File: TimeAttributeProxy.java    From balana with Apache License 2.0 4 votes vote down vote up
public AttributeValue getInstance(Node root) throws Exception {
    return TimeAttribute.getInstance(root);
}
 
Example #6
Source File: TimeAttributeProxy.java    From balana with Apache License 2.0 4 votes vote down vote up
public AttributeValue getInstance(String value) throws Exception {
    return TimeAttribute.getInstance(value);
}
 
Example #7
Source File: BasicEvaluationCtx.java    From balana with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the value for the current time. The current time, current date, and current dateTime
 * are consistent, so that they all represent the same moment. If this is the first time that
 * one of these three values has been requested, and caching is enabled, then the three values
 * will be resolved and stored.
 * <p/>
 * Note that the value supplied here applies only to dynamically resolved values, not those
 * supplied in the Request. In other words, this always returns a dynamically resolved value
 * local to the PDP, even if a different value was supplied in the Request. This is handled
 * correctly when the value is requested by its identifier.
 *
 * @return the current time
 */
public synchronized TimeAttribute getCurrentTime() {
    long millis = dateTimeHelper();

    if (useCachedEnvValues)
        return currentTime;
    else
        return new TimeAttribute(new Date(millis));
}
 
Example #8
Source File: EvaluationCtx.java    From balana with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the value for the current time as known by the PDP (if this value was also supplied
 * in the Request, this will generally be a different value). Details of caching or
 * location-based resolution are left to the underlying implementation.
 * 
 * @return the current time
 */
public TimeAttribute getCurrentTime();