66 |
brianR |
1 |
/*
|
|
|
2 |
* Copyright 2011 Brian Rosenberger (Brutex Network)
|
|
|
3 |
*
|
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
|
6 |
* You may obtain a copy of the License at
|
|
|
7 |
*
|
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
9 |
*
|
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
13 |
* See the License for the specific language governing permissions and
|
|
|
14 |
* limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
package net.brutex.xservices.ws.impl;
|
|
|
17 |
|
|
|
18 |
import java.math.BigInteger;
|
|
|
19 |
import java.text.ParseException;
|
|
|
20 |
import java.text.SimpleDateFormat;
|
|
|
21 |
import java.util.Date;
|
|
|
22 |
import java.util.GregorianCalendar;
|
|
|
23 |
import java.util.TimeZone;
|
|
|
24 |
|
|
|
25 |
import javax.jws.WebService;
|
|
|
26 |
|
|
|
27 |
import net.brutex.xservices.types.DateFormatType;
|
|
|
28 |
import net.brutex.xservices.types.DateTimeUnits;
|
|
|
29 |
import net.brutex.xservices.util.BrutexNamespaces;
|
|
|
30 |
import net.brutex.xservices.ws.DateService;
|
|
|
31 |
import net.brutex.xservices.ws.XServicesFault;
|
|
|
32 |
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* @author Brian Rosenberger
|
|
|
36 |
*
|
|
|
37 |
*/
|
|
|
38 |
@WebService(
|
|
|
39 |
targetNamespace = BrutexNamespaces.WS_XSERVICES,
|
|
|
40 |
endpointInterface = "net.brutex.xservices.ws.DateService",
|
|
|
41 |
serviceName = DateService.SERVICE_NAME
|
|
|
42 |
)
|
|
|
43 |
public class DateServiceImpl implements DateService {
|
|
|
44 |
|
|
|
45 |
private static String ERR_INVALIDFORMAT = "Invalid format pattern.";
|
|
|
46 |
private static String ERR_INVALIDTIMEZONE = "Invalid timezone.";
|
70 |
brianR |
47 |
|
66 |
brianR |
48 |
public GregorianCalendar getDate(String timezone) throws XServicesFault {
|
|
|
49 |
if (! isValidTimezone(timezone) ) {
|
|
|
50 |
String valid_ids = "";
|
|
|
51 |
String[] tid = TimeZone.getAvailableIDs();
|
|
|
52 |
for (String s : tid) {
|
|
|
53 |
valid_ids += s + "\n";
|
|
|
54 |
}
|
|
|
55 |
throw new XServicesFault("Please supply a valid timezone id or none. Valid timezones are:\n" + valid_ids,
|
|
|
56 |
new Exception( ));
|
|
|
57 |
}
|
|
|
58 |
if (timezone == null || timezone.length()<1 ) timezone = "GMT0";
|
|
|
59 |
GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone(timezone));
|
|
|
60 |
return c;
|
|
|
61 |
}
|
|
|
62 |
|
70 |
brianR |
63 |
|
66 |
brianR |
64 |
public BigInteger getTimestamp() {
|
|
|
65 |
Date d = new Date();
|
|
|
66 |
long l = d.getTime();
|
71 |
brianR |
67 |
return new BigInteger(Long.toString(l));
|
66 |
brianR |
68 |
}
|
71 |
brianR |
69 |
|
|
|
70 |
public BigInteger getTimestamp2() {
|
|
|
71 |
Date d = new Date();
|
|
|
72 |
long l = d.getTime()/1000;
|
|
|
73 |
return new BigInteger(Long.toString(l));
|
|
|
74 |
}
|
66 |
brianR |
75 |
|
70 |
brianR |
76 |
|
66 |
brianR |
77 |
public GregorianCalendar getInTimezone(GregorianCalendar cal,
|
|
|
78 |
String timezone) throws XServicesFault {
|
|
|
79 |
if(! isValidTimezone(timezone)) throw new XServicesFault(ERR_INVALIDTIMEZONE);
|
|
|
80 |
GregorianCalendar c = new GregorianCalendar(TimeZone.getTimeZone(timezone));
|
|
|
81 |
c.setTimeInMillis(cal.getTimeInMillis());
|
|
|
82 |
return c;
|
|
|
83 |
}
|
|
|
84 |
|
70 |
brianR |
85 |
|
66 |
brianR |
86 |
public String formatDate(GregorianCalendar cal, DateFormatType format) throws XServicesFault {
|
|
|
87 |
return formatDateAdvanced(cal, format.format());
|
|
|
88 |
}
|
|
|
89 |
|
70 |
brianR |
90 |
|
66 |
brianR |
91 |
public String formatDateAdvanced(GregorianCalendar cal, String format)
|
|
|
92 |
throws XServicesFault {
|
|
|
93 |
String result= null;
|
|
|
94 |
try {
|
|
|
95 |
SimpleDateFormat f = new SimpleDateFormat(format);
|
|
|
96 |
result = f.format(cal.getTime());
|
|
|
97 |
} catch (IllegalArgumentException e) {
|
|
|
98 |
throw new XServicesFault(ERR_INVALIDFORMAT + e.getMessage());
|
|
|
99 |
}
|
|
|
100 |
return result;
|
|
|
101 |
}
|
|
|
102 |
|
70 |
brianR |
103 |
|
66 |
brianR |
104 |
public GregorianCalendar parseDate(String s, DateFormatType format, String timezone) throws XServicesFault {
|
|
|
105 |
return parseDateAdvanced(s, format.format(), timezone);
|
|
|
106 |
}
|
|
|
107 |
|
70 |
brianR |
108 |
|
66 |
brianR |
109 |
public GregorianCalendar parseDateAdvanced(String s, String format, String timezone) throws XServicesFault {
|
|
|
110 |
SimpleDateFormat f = null;
|
|
|
111 |
Date date = null;
|
|
|
112 |
if(timezone==null | timezone.equals("")) timezone = TimeZone.getDefault().getID();
|
|
|
113 |
if(! isValidTimezone(timezone)) throw new XServicesFault(ERR_INVALIDTIMEZONE);
|
|
|
114 |
|
|
|
115 |
try {
|
|
|
116 |
f = new SimpleDateFormat(format);
|
|
|
117 |
date = f.parse(s);
|
|
|
118 |
} catch(IllegalArgumentException e) {
|
|
|
119 |
throw new XServicesFault(ERR_INVALIDFORMAT + e.getMessage());
|
|
|
120 |
} catch (ParseException e) {
|
|
|
121 |
throw new XServicesFault("Cannot parse date: "+ e.getMessage());
|
|
|
122 |
}
|
|
|
123 |
GregorianCalendar cal = new GregorianCalendar();
|
|
|
124 |
cal.setTimeZone(TimeZone.getTimeZone(timezone));
|
|
|
125 |
cal.setTime(date);
|
|
|
126 |
return cal;
|
|
|
127 |
}
|
|
|
128 |
|
70 |
brianR |
129 |
|
66 |
brianR |
130 |
public BigInteger dateTimeDiff(GregorianCalendar fromCal,
|
|
|
131 |
GregorianCalendar toCal) throws XServicesFault {
|
|
|
132 |
long diff = toCal.getTimeInMillis() - fromCal.getTimeInMillis();
|
|
|
133 |
BigInteger d = new BigInteger(String.valueOf(diff), 10);
|
|
|
134 |
return d;
|
|
|
135 |
}
|
|
|
136 |
|
70 |
brianR |
137 |
|
66 |
brianR |
138 |
public BigInteger dateTimeDiff2(GregorianCalendar fromCal,
|
|
|
139 |
GregorianCalendar toCal, DateTimeUnits unit) throws XServicesFault {
|
|
|
140 |
BigInteger d = dateTimeDiff(fromCal, toCal);
|
|
|
141 |
switch (unit) {
|
|
|
142 |
case SECONDS:
|
|
|
143 |
d = d.divide(new BigInteger("1000"));
|
|
|
144 |
break;
|
|
|
145 |
case MINUTES:
|
|
|
146 |
d = d.divide(new BigInteger("60000"));
|
|
|
147 |
break;
|
|
|
148 |
case HOURS:
|
|
|
149 |
d = d.divide(new BigInteger("3600000"));
|
|
|
150 |
break;
|
|
|
151 |
case DAYS:
|
|
|
152 |
d = d.divide(new BigInteger("86400000"));
|
|
|
153 |
}
|
|
|
154 |
return d;
|
|
|
155 |
}
|
|
|
156 |
|
70 |
brianR |
157 |
|
68 |
brianR |
158 |
public GregorianCalendar dateAdd(GregorianCalendar cal, BigInteger value, DateTimeUnits unit)
|
|
|
159 |
throws XServicesFault {
|
|
|
160 |
switch (unit) {
|
|
|
161 |
case SECONDS:
|
|
|
162 |
cal.add(GregorianCalendar.SECOND, value.intValue());
|
|
|
163 |
break;
|
|
|
164 |
case MINUTES:
|
|
|
165 |
cal.add(GregorianCalendar.MINUTE, value.intValue());
|
|
|
166 |
break;
|
|
|
167 |
case HOURS:
|
|
|
168 |
cal.add(GregorianCalendar.HOUR_OF_DAY, value.intValue());
|
|
|
169 |
break;
|
|
|
170 |
case DAYS:
|
|
|
171 |
cal.add(GregorianCalendar.DAY_OF_MONTH, value.intValue());
|
|
|
172 |
break;
|
|
|
173 |
default:
|
|
|
174 |
cal.add(GregorianCalendar.MILLISECOND, value.intValue());
|
|
|
175 |
}
|
|
|
176 |
return cal;
|
|
|
177 |
}
|
|
|
178 |
|
66 |
brianR |
179 |
private boolean isValidTimezone(String id) {
|
|
|
180 |
boolean yes = false;
|
|
|
181 |
for( String s: TimeZone.getAvailableIDs()) {
|
|
|
182 |
if(s.equals(id)) {
|
|
|
183 |
yes = true;
|
|
|
184 |
break;
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
return yes;
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
}
|