From 44f01bfd90211832a94c87f7679262a78accabcc Mon Sep 17 00:00:00 2001 From: Brian Rosenberger Date: Wed, 25 May 2011 11:52:42 +0000 Subject: [PATCH] Added new DateService git-svn-id: https://brutex.net/svn/xservices/trunk@66 e7e49efb-446e-492e-b9ec-fcafc1997a86 --- .../xservices/types/DateFormatType.java | 65 +++++++ .../brutex/xservices/types/DateTimeUnits.java | 71 ++++++++ .../net/brutex/xservices/ws/DateService.java | 146 ++++++++++++++++ .../brutex/xservices/ws/ExecuteService.java | 16 ++ .../xservices/ws/impl/DateServiceImpl.java | 163 ++++++++++++++++++ 5 files changed, 461 insertions(+) create mode 100644 src/java/net/brutex/xservices/types/DateFormatType.java create mode 100644 src/java/net/brutex/xservices/types/DateTimeUnits.java create mode 100644 src/java/net/brutex/xservices/ws/DateService.java create mode 100644 src/java/net/brutex/xservices/ws/impl/DateServiceImpl.java diff --git a/src/java/net/brutex/xservices/types/DateFormatType.java b/src/java/net/brutex/xservices/types/DateFormatType.java new file mode 100644 index 0000000..d852105 --- /dev/null +++ b/src/java/net/brutex/xservices/types/DateFormatType.java @@ -0,0 +1,65 @@ +/* + * Copyright 2011 Brian Rosenberger (Brutex Network) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.brutex.xservices.types; + +import javax.xml.bind.annotation.XmlEnum; +import javax.xml.bind.annotation.XmlEnumValue; + +/** + * Different pre-defined date formats. + * + * @author Brian Rosenberger, bru@brutex.de + */ +@XmlEnum(value = String.class) +public enum DateFormatType { + + /** + * ISO 8601 format (2011-05-24T14:39Z) + */ + @XmlEnumValue("ISO 8601") + ISO8601("ISO 8601", "yyyy-MM-dd'T'HH:mm:ssZ"), + /** + * yyyy/mm/dd + */ + @XmlEnumValue("yyyy/mm/dd") + YYYYMMDD("yyyy/mm/dd", "yyyy/MM/dd"), + + /** + * dd.mm.yyyy + */ + @XmlEnumValue("dd.mm.yyyy") + DDMMYY("dd.mm.yyyy", "dd.MM.yyyy"); + + private String value; + private String format; + + DateFormatType(String value, String format) { + this.value = value; + this.format = format; + } + + /** + * Return the value of the enum. + * @return String representation of the mime type + */ + public String value() { + return value; + } + + public String format() { + return format; + } +} diff --git a/src/java/net/brutex/xservices/types/DateTimeUnits.java b/src/java/net/brutex/xservices/types/DateTimeUnits.java new file mode 100644 index 0000000..289a33c --- /dev/null +++ b/src/java/net/brutex/xservices/types/DateTimeUnits.java @@ -0,0 +1,71 @@ +/* + * Copyright 2011 Brian Rosenberger (Brutex Network) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.brutex.xservices.types; + +import javax.xml.bind.annotation.XmlEnum; +import javax.xml.bind.annotation.XmlEnumValue; + +/** + * Different pre-defined date formats. + * + * @author Brian Rosenberger, bru@brutex.de + */ +@XmlEnum(value = String.class) +public enum DateTimeUnits { + + /** + * milliseconds + */ + @XmlEnumValue("milliseconds") + MILLISECONDS("milliseconds"), + /** + * seconds + */ + @XmlEnumValue("seconds") + SECONDS("seconds"), + + /** + * minutes + */ + @XmlEnumValue("minutes") + MINUTES("minutes"), + + /** + * hours + */ + @XmlEnumValue("hours") + HOURS("hours"), + + /** + * days + */ + @XmlEnumValue("days") + DAYS("days"); + + private String value; + + DateTimeUnits(String value) { + this.value = value; + } + + /** + * Return the value of the enum. + * @return String representation of the mime type + */ + public String value() { + return value; + } +} diff --git a/src/java/net/brutex/xservices/ws/DateService.java b/src/java/net/brutex/xservices/ws/DateService.java new file mode 100644 index 0000000..dbf7119 --- /dev/null +++ b/src/java/net/brutex/xservices/ws/DateService.java @@ -0,0 +1,146 @@ +/* + * Copyright 2011 Brian Rosenberger (Brutex Network) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.brutex.xservices.ws; + +import java.math.BigInteger; +import java.util.GregorianCalendar; + +import javax.jws.WebMethod; +import javax.jws.WebParam; +import javax.jws.WebService; +import javax.xml.bind.annotation.XmlElement; + +import net.brutex.xservices.types.DateFormatType; +import net.brutex.xservices.types.DateTimeUnits; +import net.brutex.xservices.util.BrutexNamespaces; + +import org.apache.cxf.annotations.WSDLDocumentation; +import org.apache.cxf.annotations.WSDLDocumentationCollection; + +/** + * Date and time related services. + * @author Brian Rosenberger + * + */ +@WebService(targetNamespace = BrutexNamespaces.WS_XSERVICES) +@WSDLDocumentationCollection( + { + @WSDLDocumentation(value = BrutexNamespaces.BRUTEX_COPYRIGHT, placement = WSDLDocumentation.Placement.TOP) + } +) +public interface DateService { + + public static final String SERVICE_NAME = "DateService"; + public static final String OPERATION_GETDATE = "getDate"; + public static final String OPERATION_GETTIMESTAMP = "getTimestamp"; + public static final String OPERATION_GETINTIMEZONE = "getInTimezone"; + public static final String OPERATION_FORMATDATE = "formatDate"; + public static final String OPERATION_FORMATDATEADVANCED = "formatDateAdvanced"; + public static final String OPERATION_PARSEDATE = "parseDate"; + public static final String OPERATION_PARSEDATEADVANCED = "parseDateAdvanced"; + public static final String OPERATION_DATETIMEDIFF = "dateTimeDiff"; + public static final String OPERATION_DATETIMEDIFF2 = "dateTimeDiff2"; + + public static final String PARAM_TIMEZONE = "timezone"; + public static final String PARAM_DATETIME = "datetime"; + public static final String PARAM_FORMAT = "format"; + + /** + * Get current date and time. + * + * @param timezone Optional timezone. Defaults to server timezone. + * @return Current date and time. + * @throws XServicesFault + */ + @WebMethod(operationName=OPERATION_GETDATE) + @WSDLDocumentation(value="Get current date and time.") + public abstract GregorianCalendar getDate( + @WebParam(name=PARAM_TIMEZONE) String timezone) throws XServicesFault; + + /** + * Get milliseconds since 01.01.1970. + * + * @return timestamp milliseconds + */ + @WebMethod(operationName=OPERATION_GETTIMESTAMP) + @WSDLDocumentation(value="Get milliseconds since 01.01.1970 (Unix timestap).") + public abstract BigInteger getTimestamp(); + + /** + * Display a date time with a different time zone. + * Changes representation only (no conversion). + * + * @param cal date time. + * @param timezone time zone + * @return date time + * @throws XServicesFault + */ + @WebMethod(operationName=OPERATION_GETINTIMEZONE) + public abstract GregorianCalendar getInTimezone( + @WebParam(name=PARAM_DATETIME) @XmlElement(required=true) GregorianCalendar cal, + @WebParam(name=PARAM_TIMEZONE) @XmlElement(required=true) String timezone) throws XServicesFault; + + /** + * @param cal + * @param format + * @return formatted date/time string + * @throws XServicesFault + */ + @WebMethod(operationName=OPERATION_FORMATDATE) + public abstract String formatDate( + @WebParam(name=PARAM_DATETIME) @XmlElement(required=true) GregorianCalendar cal, + @WebParam(name=PARAM_FORMAT) @XmlElement(required=true) DateFormatType format) throws XServicesFault; + + @WebMethod(operationName=OPERATION_FORMATDATEADVANCED) + public abstract String formatDateAdvanced( + @WebParam(name=PARAM_DATETIME) @XmlElement(required=true) GregorianCalendar cal, + @WebParam(name=PARAM_FORMAT) @XmlElement(required=true) String format) throws XServicesFault; + + @WebMethod(operationName=OPERATION_PARSEDATE) + public abstract GregorianCalendar parseDate( + @WebParam(name=PARAM_DATETIME) @XmlElement(required=true) String s, + @WebParam(name=PARAM_FORMAT) @XmlElement(required=true) DateFormatType format, + @WebParam(name=PARAM_TIMEZONE) String timezone) throws XServicesFault; + + @WebMethod(operationName=OPERATION_PARSEDATEADVANCED) + public abstract GregorianCalendar parseDateAdvanced( + @WebParam(name=PARAM_DATETIME) @XmlElement(required=true) String s, + @WebParam(name=PARAM_FORMAT) @XmlElement(required=true) String format, + @WebParam(name=PARAM_TIMEZONE) String timezone) throws XServicesFault; + + @WebMethod(operationName=OPERATION_DATETIMEDIFF) + public abstract BigInteger dateTimeDiff( + @WebParam(name="fromDateTime") @XmlElement(required=true) GregorianCalendar fromCal, + @WebParam(name="toDateTime") @XmlElement(required=true) GregorianCalendar toCal) throws XServicesFault; + + /** + * Fully elapsed units between two dates. + * 4:15:10-4:15:55 in minutes = 0 and in seconds = 45 + * + * @param fromCal + * @param toCal + * @param unit + * @return Date/time difference in unit + * @throws XServicesFault + */ + @WebMethod(operationName=OPERATION_DATETIMEDIFF2) + @WSDLDocumentation(value="Get elapsed time between to dates.") + public abstract BigInteger dateTimeDiff2( + @WebParam(name="fromDateTime") @XmlElement(required=true) GregorianCalendar fromCal, + @WebParam(name="toDateTime") @XmlElement(required=true) GregorianCalendar toCal, + @WebParam(name="unit") DateTimeUnits unit) throws XServicesFault; +} diff --git a/src/java/net/brutex/xservices/ws/ExecuteService.java b/src/java/net/brutex/xservices/ws/ExecuteService.java index d90a617..121b2f1 100644 --- a/src/java/net/brutex/xservices/ws/ExecuteService.java +++ b/src/java/net/brutex/xservices/ws/ExecuteService.java @@ -1,3 +1,19 @@ +/* + * Copyright 2011 Brian Rosenberger (Brutex Network) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package net.brutex.xservices.ws; import javax.jws.WebMethod; diff --git a/src/java/net/brutex/xservices/ws/impl/DateServiceImpl.java b/src/java/net/brutex/xservices/ws/impl/DateServiceImpl.java new file mode 100644 index 0000000..276e389 --- /dev/null +++ b/src/java/net/brutex/xservices/ws/impl/DateServiceImpl.java @@ -0,0 +1,163 @@ +/* + * Copyright 2011 Brian Rosenberger (Brutex Network) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.brutex.xservices.ws.impl; + +import java.math.BigInteger; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.TimeZone; + +import javax.jws.WebService; + +import net.brutex.xservices.types.DateFormatType; +import net.brutex.xservices.types.DateTimeUnits; +import net.brutex.xservices.util.BrutexNamespaces; +import net.brutex.xservices.ws.DateService; +import net.brutex.xservices.ws.XServicesFault; + + +/** + * @author Brian Rosenberger + * + */ +@WebService( + targetNamespace = BrutexNamespaces.WS_XSERVICES, + endpointInterface = "net.brutex.xservices.ws.DateService", + serviceName = DateService.SERVICE_NAME + ) +public class DateServiceImpl implements DateService { + + private static String ERR_INVALIDFORMAT = "Invalid format pattern."; + private static String ERR_INVALIDTIMEZONE = "Invalid timezone."; + @Override + public GregorianCalendar getDate(String timezone) throws XServicesFault { + if (! isValidTimezone(timezone) ) { + String valid_ids = ""; + String[] tid = TimeZone.getAvailableIDs(); + for (String s : tid) { + valid_ids += s + "\n"; + } + throw new XServicesFault("Please supply a valid timezone id or none. Valid timezones are:\n" + valid_ids, + new Exception( )); + } + if (timezone == null || timezone.length()<1 ) timezone = "GMT0"; + GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone(timezone)); + return c; + } + + @Override + public BigInteger getTimestamp() { + Date d = new Date(); + long l = d.getTime(); + BigInteger timestamp = new BigInteger(Long.toString(l)); + return timestamp; + } + + @Override + public GregorianCalendar getInTimezone(GregorianCalendar cal, + String timezone) throws XServicesFault { + if(! isValidTimezone(timezone)) throw new XServicesFault(ERR_INVALIDTIMEZONE); + GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone(timezone)); + c.setTimeInMillis(cal.getTimeInMillis()); + return c; + } + + @Override + public String formatDate(GregorianCalendar cal, DateFormatType format) throws XServicesFault { + return formatDateAdvanced(cal, format.format()); + } + + @Override + public String formatDateAdvanced(GregorianCalendar cal, String format) + throws XServicesFault { + String result= null; + try { + SimpleDateFormat f = new SimpleDateFormat(format); + result = f.format(cal.getTime()); + } catch (IllegalArgumentException e) { + throw new XServicesFault(ERR_INVALIDFORMAT + e.getMessage()); + } + return result; + } + + @Override + public GregorianCalendar parseDate(String s, DateFormatType format, String timezone) throws XServicesFault { + return parseDateAdvanced(s, format.format(), timezone); + } + + @Override + public GregorianCalendar parseDateAdvanced(String s, String format, String timezone) throws XServicesFault { + SimpleDateFormat f = null; + Date date = null; + if(timezone==null | timezone.equals("")) timezone = TimeZone.getDefault().getID(); + if(! isValidTimezone(timezone)) throw new XServicesFault(ERR_INVALIDTIMEZONE); + + try { + f = new SimpleDateFormat(format); + date = f.parse(s); + } catch(IllegalArgumentException e) { + throw new XServicesFault(ERR_INVALIDFORMAT + e.getMessage()); + } catch (ParseException e) { + throw new XServicesFault("Cannot parse date: "+ e.getMessage()); + } + GregorianCalendar cal = new GregorianCalendar(); + cal.setTimeZone(TimeZone.getTimeZone(timezone)); + cal.setTime(date); + return cal; + } + + @Override + public BigInteger dateTimeDiff(GregorianCalendar fromCal, + GregorianCalendar toCal) throws XServicesFault { + long diff = toCal.getTimeInMillis() - fromCal.getTimeInMillis(); + BigInteger d = new BigInteger(String.valueOf(diff), 10); + return d; + } + + @Override + public BigInteger dateTimeDiff2(GregorianCalendar fromCal, + GregorianCalendar toCal, DateTimeUnits unit) throws XServicesFault { + BigInteger d = dateTimeDiff(fromCal, toCal); + switch (unit) { + case SECONDS: + d = d.divide(new BigInteger("1000")); + break; + case MINUTES: + d = d.divide(new BigInteger("60000")); + break; + case HOURS: + d = d.divide(new BigInteger("3600000")); + break; + case DAYS: + d = d.divide(new BigInteger("86400000")); + } + return d; + } + + private boolean isValidTimezone(String id) { + boolean yes = false; + for( String s: TimeZone.getAvailableIDs()) { + if(s.equals(id)) { + yes = true; + break; + } + } + return yes; + } + +}