/
StamdataReplicationImpl.java
212 lines (172 loc) · 7.24 KB
/
StamdataReplicationImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Contributor(s): Contributors are attributed in the source code
* where applicable.
*
* The Original Code is "Stamdata".
*
* The Initial Developer of the Original Code is Trifork Public A/S.
*
* Portions created for the Original Code are Copyright 2011,
* Lægemiddelstyrelsen. All Rights Reserved.
*
* Portions created for the FMKi Project are Copyright 2011,
* National Board of e-Health (NSI). All Rights Reserved.
*/
package dk.nsi.stamdata.replication.webservice;
import static java.lang.String.format;
import java.io.IOException;
import java.util.Map;
import javax.jws.WebService;
import javax.xml.ws.Holder;
import org.hibernate.ScrollableResults;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.w3c.dom.Document;
import com.google.inject.Inject;
import com.sun.xml.ws.developer.SchemaValidation;
import com.trifork.stamdata.jaxws.GuiceInstanceResolver.GuiceWebservice;
import dk.nsi.dgws.ClientVocesCvr;
import dk.nsi.stamdata.replication.jaxws.Header;
import dk.nsi.stamdata.replication.jaxws.ObjectFactory;
import dk.nsi.stamdata.replication.jaxws.ReplicationFault;
import dk.nsi.stamdata.replication.jaxws.ReplicationRequestType;
import dk.nsi.stamdata.replication.jaxws.ReplicationResponseType;
import dk.nsi.stamdata.replication.jaxws.Security;
import dk.nsi.stamdata.replication.jaxws.StamdataReplication;
import dk.nsi.stamdata.replication.models.Client;
import dk.nsi.stamdata.replication.models.ClientDao;
import dk.nsi.stamdata.views.View;
import dk.nsi.stamdata.views.Views;
@GuiceWebservice
@SchemaValidation
@WebService(serviceName = "StamdataReplication", endpointInterface = "dk.nsi.stamdata.replication.jaxws.StamdataReplication")
public class StamdataReplicationImpl implements StamdataReplication {
private static final Logger logger = LoggerFactory.getLogger(StamdataReplicationImpl.class);
private static final int MAX_RECORD_LIMIT = 2000;
private final String cvr;
private final RecordDao dao;
private final Map<String, Class<? extends View>> viewClasses;
private final ClientDao clients;
private final AtomFeedWriter outputWriter;
@Inject
StamdataReplicationImpl(@ClientVocesCvr String cvr, RecordDao dao, ClientDao clients, Map<String, Class<? extends View>> viewClasses, AtomFeedWriter outputWriter) {
this.cvr = cvr;
this.dao = dao;
this.clients = clients;
this.viewClasses = viewClasses;
this.outputWriter = outputWriter;
}
@Override
public ReplicationResponseType replicate(Holder<Security> wsseHeader, Holder<Header> medcomHeader, ReplicationRequestType parameters) throws ReplicationFault
{
try
{
Class<? extends View> requestedView = getViewClass(parameters);
MDC.put("view", Views.getViewPath(requestedView));
MDC.put("cvr", cvr);
// Validate authentication.
//
Client client = clients.findByCvr(cvr);
if (client == null || !client.isAuthorizedFor(requestedView))
{
throw new ReplicationFault("The provided cvr is not authorized to fetch this datatype.", FaultCodes.UNAUTHORIZED);
}
// Validate the input parameters.
//
HistoryOffset offset = getOffset(parameters);
int limit = getRecordLimit(parameters);
MDC.put("offset", String.valueOf(offset));
MDC.put("limit", String.valueOf(limit));
// Fetch the records from the database and
// fill the output structure.
//
ScrollableResults results = dao.findPage(requestedView, offset.getRecordID(), offset.getModifiedDate(), limit);
Document feedDocument = createFeed(requestedView, results);
// Construct the output container.
//
ReplicationResponseType response = new ObjectFactory().createReplicationResponseType();
response.setAny(feedDocument.getFirstChild());
// Log that the client successfully accessed the data.
// Simply for audit purposes.
//
// The client details are included in the MDC.
//
logger.info("Records fetched, sending response.");
return response;
}
catch (ReplicationFault e)
{
// Log an throw is normally an anti-pattern, but since
// exceptions are part of JAX-WS's flow it is "OK" here.
logger.warn("The request could not be handled. This is likely the clients mistake.", e);
throw e;
}
catch (RuntimeException e)
{
logger.error("An unhandled error occured.", e);
throw e;
}
finally
{
// Clean up the thread's MDC.
// TODO: This might fit better in a filter or intercepter.
MDC.remove("view");
MDC.remove("cvr");
MDC.remove("offset");
MDC.remove("limit");
}
}
private int getRecordLimit(ReplicationRequestType parameters)
{
if (parameters.getMaxRecords() == null)
{
return MAX_RECORD_LIMIT;
}
else
{
return Math.min(parameters.getMaxRecords().intValue(), MAX_RECORD_LIMIT);
}
}
private Document createFeed(Class<? extends View> requestedView, ScrollableResults results) throws ReplicationFault
{
Document body;
try {
body = outputWriter.write(requestedView, results);
}
catch (IOException e) {
throw new ReplicationFault("A unexpected error occured. Processing stopped.", FaultCodes.IO_ERROR, e);
}
return body;
}
private Class<? extends View> getViewClass(ReplicationRequestType parameters) throws ReplicationFault
{
String viewPath = format("%s/%s/v%d", parameters.getRegister(), parameters.getDatatype(), parameters.getVersion());
Class<? extends View> requestedView = viewClasses.get(viewPath);
if (requestedView == null)
{
throw new ReplicationFault(format("No view with identifier register=%s, datatype=%s and version=%d can be found.", parameters.getRegister(), parameters.getDatatype(), parameters.getVersion()), FaultCodes.UNKNOWN_VIEW);
}
return requestedView;
}
private HistoryOffset getOffset(ReplicationRequestType parameters) throws ReplicationFault
{
try
{
return new HistoryOffset(parameters.getOffset());
}
catch (Exception e) {
throw new ReplicationFault("Invalid offset in the request.", FaultCodes.INVALID_OFFSET, e);
}
}
}