mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-06 05:07:41 +00:00
GUAC-830: Move parsing/formatting into field types.
This commit is contained in:
@@ -25,9 +25,7 @@ package org.glyptodon.guacamole.auth.jdbc.user;
|
||||
import com.google.inject.Inject;
|
||||
import java.sql.Date;
|
||||
import java.sql.Time;
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collection;
|
||||
@@ -268,40 +266,39 @@ public class ModeledUser extends ModeledDirectoryObject<UserModel> implements Us
|
||||
return userPermissionService.getPermissionSet(getCurrentUser(), this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given date into a string which follows the format used by
|
||||
* date attributes.
|
||||
*
|
||||
* @param date
|
||||
* The date value to format, which may be null.
|
||||
*
|
||||
* @return
|
||||
* The formatted date, or null if the provided time was null.
|
||||
*/
|
||||
private String formatDate(Date date) {
|
||||
DateFormat dateFormat = new SimpleDateFormat(DateField.FORMAT);
|
||||
return date == null ? null : dateFormat.format(date);
|
||||
}
|
||||
@Override
|
||||
public Map<String, String> getAttributes() {
|
||||
|
||||
/**
|
||||
* Converts the given time into a string which follows the format used by
|
||||
* time attributes.
|
||||
*
|
||||
* @param time
|
||||
* The time value to format, which may be null.
|
||||
*
|
||||
* @return
|
||||
* The formatted time, or null if the provided time was null.
|
||||
*/
|
||||
private String formatTime(Time time) {
|
||||
DateFormat timeFormat = new SimpleDateFormat(TimeField.FORMAT);
|
||||
return time == null ? null : timeFormat.format(time);
|
||||
Map<String, String> attributes = new HashMap<String, String>();
|
||||
|
||||
// Set disabled attribute
|
||||
attributes.put(DISABLED_ATTRIBUTE_NAME, getModel().isDisabled() ? "true" : null);
|
||||
|
||||
// Set password expired attribute
|
||||
attributes.put(EXPIRED_ATTRIBUTE_NAME, getModel().isExpired() ? "true" : null);
|
||||
|
||||
// Set access window start time
|
||||
attributes.put(ACCESS_WINDOW_START_ATTRIBUTE_NAME, TimeField.format(getModel().getAccessWindowStart()));
|
||||
|
||||
// Set access window end time
|
||||
attributes.put(ACCESS_WINDOW_END_ATTRIBUTE_NAME, TimeField.format(getModel().getAccessWindowEnd()));
|
||||
|
||||
// Set account validity start date
|
||||
attributes.put(VALID_FROM_ATTRIBUTE_NAME, DateField.format(getModel().getValidFrom()));
|
||||
|
||||
// Set account validity end date
|
||||
attributes.put(VALID_UNTIL_ATTRIBUTE_NAME, DateField.format(getModel().getValidUntil()));
|
||||
|
||||
// Set timezone attribute
|
||||
attributes.put(TIMEZONE_ATTRIBUTE_NAME, getModel().getTimeZone());
|
||||
|
||||
return attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given string into a corresponding date. The string must
|
||||
* follow the standard format used by date attributes, as defined by
|
||||
* DATE_FORMAT and as would be produced by formatDate().
|
||||
* DateField.FORMAT and as would be produced by DateField.format().
|
||||
*
|
||||
* @param dateString
|
||||
* The date string to parse, which may be null.
|
||||
@@ -318,19 +315,19 @@ public class ModeledUser extends ModeledDirectoryObject<UserModel> implements Us
|
||||
throws ParseException {
|
||||
|
||||
// Return null if no date provided
|
||||
if (dateString == null || dateString.isEmpty())
|
||||
java.util.Date parsedDate = DateField.parse(dateString);
|
||||
if (parsedDate == null)
|
||||
return null;
|
||||
|
||||
// Parse date according to format
|
||||
DateFormat dateFormat = new SimpleDateFormat(DateField.FORMAT);
|
||||
return new Date(dateFormat.parse(dateString).getTime());
|
||||
// Convert to SQL Date
|
||||
return new Date(parsedDate.getTime());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given string into a corresponding time. The string must
|
||||
* follow the standard format used by time attributes, as defined by
|
||||
* TIME_FORMAT and as would be produced by formatTime().
|
||||
* TimeField.FORMAT and as would be produced by TimeField.format().
|
||||
*
|
||||
* @param timeString
|
||||
* The time string to parse, which may be null.
|
||||
@@ -347,67 +344,15 @@ public class ModeledUser extends ModeledDirectoryObject<UserModel> implements Us
|
||||
throws ParseException {
|
||||
|
||||
// Return null if no time provided
|
||||
if (timeString == null || timeString.isEmpty())
|
||||
java.util.Date parsedDate = TimeField.parse(timeString);
|
||||
if (parsedDate == null)
|
||||
return null;
|
||||
|
||||
// Parse time according to format
|
||||
DateFormat timeFormat = new SimpleDateFormat(TimeField.FORMAT);
|
||||
return new Time(timeFormat.parse(timeString).getTime());
|
||||
// Convert to SQL Time
|
||||
return new Time(parsedDate.getTime());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given string into a time zone ID string. As these strings are
|
||||
* equivalent, the only transformation currently performed by this function
|
||||
* is to ensure that a blank time zone string is parsed into null.
|
||||
*
|
||||
* @param timeZone
|
||||
* The time zone string to parse, which may be null.
|
||||
*
|
||||
* @return
|
||||
* The ID of the time zone corresponding to the given string, or null
|
||||
* if the given time zone string was null or blank.
|
||||
*/
|
||||
private String parseTimeZone(String timeZone) {
|
||||
|
||||
// Return null if no time zone provided
|
||||
if (timeZone == null || timeZone.isEmpty())
|
||||
return null;
|
||||
|
||||
// Otherwise, assume time zone is valid
|
||||
return timeZone;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, String> getAttributes() {
|
||||
|
||||
Map<String, String> attributes = new HashMap<String, String>();
|
||||
|
||||
// Set disabled attribute
|
||||
attributes.put(DISABLED_ATTRIBUTE_NAME, getModel().isDisabled() ? "true" : null);
|
||||
|
||||
// Set password expired attribute
|
||||
attributes.put(EXPIRED_ATTRIBUTE_NAME, getModel().isExpired() ? "true" : null);
|
||||
|
||||
// Set access window start time
|
||||
attributes.put(ACCESS_WINDOW_START_ATTRIBUTE_NAME, formatTime(getModel().getAccessWindowStart()));
|
||||
|
||||
// Set access window end time
|
||||
attributes.put(ACCESS_WINDOW_END_ATTRIBUTE_NAME, formatTime(getModel().getAccessWindowEnd()));
|
||||
|
||||
// Set account validity start date
|
||||
attributes.put(VALID_FROM_ATTRIBUTE_NAME, formatDate(getModel().getValidFrom()));
|
||||
|
||||
// Set account validity end date
|
||||
attributes.put(VALID_UNTIL_ATTRIBUTE_NAME, formatDate(getModel().getValidUntil()));
|
||||
|
||||
// Set timezone attribute
|
||||
attributes.put(TIMEZONE_ATTRIBUTE_NAME, getModel().getTimeZone());
|
||||
|
||||
return attributes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAttributes(Map<String, String> attributes) {
|
||||
|
||||
@@ -446,7 +391,7 @@ public class ModeledUser extends ModeledDirectoryObject<UserModel> implements Us
|
||||
}
|
||||
|
||||
// Translate timezone attribute
|
||||
getModel().setTimeZone(parseTimeZone(attributes.get(TIMEZONE_ATTRIBUTE_NAME)));
|
||||
getModel().setTimeZone(TimeZoneField.parse(attributes.get(TIMEZONE_ATTRIBUTE_NAME)));
|
||||
|
||||
}
|
||||
|
||||
|
@@ -22,6 +22,11 @@
|
||||
|
||||
package org.glyptodon.guacamole.form;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Represents a date field. The field may contain only date values which
|
||||
* conform to a standard pattern, defined by DateField.FORMAT.
|
||||
@@ -45,4 +50,48 @@ public class DateField extends Field {
|
||||
super(name, Field.Type.DATE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given date into a string which follows the format used by
|
||||
* date fields.
|
||||
*
|
||||
* @param date
|
||||
* The date value to format, which may be null.
|
||||
*
|
||||
* @return
|
||||
* The formatted date, or null if the provided time was null.
|
||||
*/
|
||||
public static String format(Date date) {
|
||||
DateFormat dateFormat = new SimpleDateFormat(DateField.FORMAT);
|
||||
return date == null ? null : dateFormat.format(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given string into a corresponding date. The string must
|
||||
* follow the standard format used by date fields, as defined by FORMAT
|
||||
* and as would be produced by format().
|
||||
*
|
||||
* @param dateString
|
||||
* The date string to parse, which may be null.
|
||||
*
|
||||
* @return
|
||||
* The date corresponding to the given date string, or null if the
|
||||
* provided date string was null or blank.
|
||||
*
|
||||
* @throws ParseException
|
||||
* If the given date string does not conform to the standard format
|
||||
* used by date fields.
|
||||
*/
|
||||
public static Date parse(String dateString)
|
||||
throws ParseException {
|
||||
|
||||
// Return null if no date provided
|
||||
if (dateString == null || dateString.isEmpty())
|
||||
return null;
|
||||
|
||||
// Parse date according to format
|
||||
DateFormat dateFormat = new SimpleDateFormat(DateField.FORMAT);
|
||||
return dateFormat.parse(dateString);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -39,4 +39,46 @@ public class NumericField extends Field {
|
||||
super(name, Field.Type.NUMERIC);
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats the given integer in the format required by a numeric field.
|
||||
*
|
||||
* @param i
|
||||
* The integer to format, which may be null.
|
||||
*
|
||||
* @return
|
||||
* A string representation of the given integer, or null if the given
|
||||
* integer was null.
|
||||
*/
|
||||
public static String format(Integer i) {
|
||||
|
||||
if (i == null)
|
||||
return null;
|
||||
|
||||
return i.toString();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given string as an integer, where the given string is in the
|
||||
* format required by a numeric field.
|
||||
*
|
||||
* @param str
|
||||
* The string to parse as an integer, which may be null.
|
||||
*
|
||||
* @return
|
||||
* The integer representation of the given string, or null if the given
|
||||
* string was null.
|
||||
*
|
||||
* @throws NumberFormatException
|
||||
* If the given string is not in a parseable format.
|
||||
*/
|
||||
public static Integer parse(String str) throws NumberFormatException {
|
||||
|
||||
if (str == null || str.isEmpty())
|
||||
return null;
|
||||
|
||||
return new Integer(str);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -22,6 +22,11 @@
|
||||
|
||||
package org.glyptodon.guacamole.form;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* Represents a time field. The field may contain only time values which
|
||||
* conform to a standard pattern, defined by TimeField.FORMAT.
|
||||
@@ -45,4 +50,48 @@ public class TimeField extends Field {
|
||||
super(name, Field.Type.TIME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given string into a corresponding time. The string must
|
||||
* follow the standard format used by time fields, as defined by
|
||||
* FORMAT and as would be produced by format().
|
||||
*
|
||||
* @param timeString
|
||||
* The time string to parse, which may be null.
|
||||
*
|
||||
* @return
|
||||
* The time corresponding to the given time string, or null if the
|
||||
* provided time string was null or blank.
|
||||
*
|
||||
* @throws ParseException
|
||||
* If the given time string does not conform to the standard format
|
||||
* used by time fields.
|
||||
*/
|
||||
public static Date parse(String timeString)
|
||||
throws ParseException {
|
||||
|
||||
// Return null if no time provided
|
||||
if (timeString == null || timeString.isEmpty())
|
||||
return null;
|
||||
|
||||
// Parse time according to format
|
||||
DateFormat timeFormat = new SimpleDateFormat(TimeField.FORMAT);
|
||||
return timeFormat.parse(timeString);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given time into a string which follows the format used by
|
||||
* time fields.
|
||||
*
|
||||
* @param time
|
||||
* The time value to format, which may be null.
|
||||
*
|
||||
* @return
|
||||
* The formatted time, or null if the provided time was null.
|
||||
*/
|
||||
public static String format(Date time) {
|
||||
DateFormat timeFormat = new SimpleDateFormat(TimeField.FORMAT);
|
||||
return time == null ? null : timeFormat.format(time);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -40,4 +40,27 @@ public class TimeZoneField extends Field {
|
||||
super(name, Field.Type.TIMEZONE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the given string into a time zone ID string. As these strings are
|
||||
* equivalent, the only transformation currently performed by this function
|
||||
* is to ensure that a blank time zone string is parsed into null.
|
||||
*
|
||||
* @param timeZone
|
||||
* The time zone string to parse, which may be null.
|
||||
*
|
||||
* @return
|
||||
* The ID of the time zone corresponding to the given string, or null
|
||||
* if the given time zone string was null or blank.
|
||||
*/
|
||||
public static String parse(String timeZone) {
|
||||
|
||||
// Return null if no time zone provided
|
||||
if (timeZone == null || timeZone.isEmpty())
|
||||
return null;
|
||||
|
||||
// Otherwise, assume time zone is valid
|
||||
return timeZone;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user