/
DeviceExporterCsv.java
190 lines (142 loc) · 7.01 KB
/
DeviceExporterCsv.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
/*******************************************************************************
* Copyright (c) 2017 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.app.console.module.device.servlet;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.model.id.KapuaEid;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.model.query.KapuaListResult;
import org.eclipse.kapua.service.account.Account;
import org.eclipse.kapua.service.account.AccountService;
import org.eclipse.kapua.service.device.registry.Device;
import com.opencsv.CSVWriter;
public class DeviceExporterCsv extends DeviceExporter {
private String accountId;
private String accountName;
private DateFormat dateFormat;
private CSVWriter writer;
private static final AccountService ACCOUNT_SERVICE = KapuaLocator.getInstance().getService(AccountService.class);
public DeviceExporterCsv(HttpServletResponse response) {
super(response);
}
@Override
public void init(String accountId, String accountName)
throws ServletException, IOException {
this.accountId = accountId;
this.accountName = accountName;
dateFormat = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss.SSS");
OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream(), Charset.forName("UTF-8"));
writer = new CSVWriter(osw);
List<String> cols = new ArrayList<String>();
Collections.addAll(cols, DEVICE_PROPERTIES);
writer.writeNext(cols.toArray(new String[] {}));
}
@Override
public void append(KapuaListResult<Device> devices)
throws ServletException, IOException, KapuaException {
Account account = null;
try {
account = KapuaSecurityUtils.doPrivileged(new Callable<Account>() {
@Override
public Account call() throws Exception {
return ACCOUNT_SERVICE.find(KapuaEid.parseCompactId(accountId));
}
});
} catch (KapuaException e) {
throw KapuaException.internalError(e);
}
for (Device device : devices.getItems()) {
List<String> cols = new ArrayList<String>();
// Account id
cols.add(this.accountId);
// Account name
cols.add(account.getName());
// Client id
cols.add(device.getClientId() != null ? device.getClientId() : BLANK);
// Device status
cols.add(device.getStatus() != null ? device.getStatus().name() : BLANK);
// Device connection status
cols.add(device.getConnection() != null ? device.getConnection().getStatus().name() : BLANK);
// Created on
cols.add(device.getCreatedOn() != null ? dateFormat.format(device.getCreatedOn()) : BLANK);
// Last event on
cols.add(device.getLastEvent() != null ? dateFormat.format(device.getLastEvent().getReceivedOn()) : BLANK);
// Last event type
cols.add(device.getLastEvent() != null ? device.getLastEvent().getResource() : BLANK);
// Client ip
cols.add(device.getConnection() != null ? device.getConnection().getClientIp() : BLANK);
// Display name
cols.add(device.getDisplayName() != null ? device.getDisplayName() : BLANK);
// Serial number
cols.add(device.getSerialNumber() != null ? device.getSerialNumber() : BLANK);
// Imei
cols.add(device.getImei() != null ? device.getImei() : BLANK);
// Imsi
cols.add(device.getImsi() != null ? device.getImsi() : BLANK);
// Iccid
cols.add(device.getIccid() != null ? device.getIccid() : BLANK);
// Model Id
cols.add(device.getModelId() != null ? device.getModelId() : BLANK);
// Model Name
cols.add(device.getModelName() != null ? device.getModelName() : BLANK);
// Bios version
cols.add(device.getBiosVersion() != null ? device.getBiosVersion() : BLANK);
// Firmware version
cols.add(device.getFirmwareVersion() != null ? device.getFirmwareVersion() : BLANK);
// OS version
cols.add(device.getOsVersion() != null ? device.getOsVersion() : BLANK);
// JVM version
cols.add(device.getJvmVersion() != null ? device.getJvmVersion() : BLANK);
// OSGi framework version
cols.add(device.getOsgiFrameworkVersion() != null ? device.getOsgiFrameworkVersion() : BLANK);
// Application framework version
cols.add(device.getApplicationFrameworkVersion() != null ? device.getApplicationFrameworkVersion() : BLANK);
// Application identifiers
cols.add(device.getApplicationIdentifiers() != null ? device.getApplicationIdentifiers() : BLANK);
// Accept encoding
cols.add(device.getAcceptEncoding() != null ? device.getAcceptEncoding() : BLANK);
// Custom attribute 1
cols.add(device.getCustomAttribute1() != null ? device.getCustomAttribute1() : BLANK);
// Custom attribute 2
cols.add(device.getCustomAttribute2() != null ? device.getCustomAttribute2() : BLANK);
// Custom attribute 3
cols.add(device.getCustomAttribute3() != null ? device.getCustomAttribute3() : BLANK);
// Custom attribute 4
cols.add(device.getCustomAttribute4() != null ? device.getCustomAttribute4() : BLANK);
// Custom attribute 5
cols.add(device.getCustomAttribute5() != null ? device.getCustomAttribute5() : BLANK);
writer.writeNext(cols.toArray(new String[] {}));
}
}
@Override
public void close()
throws ServletException, IOException {
response.setContentType("text/csv");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + URLEncoder.encode(accountName, "UTF-8") + "_devices.csv");
response.setHeader("Cache-Control", "no-transform, max-age=0");
writer.flush();
writer.close();
}
}