• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

smartsheet / smartsheet-java-sdk / #41

24 Aug 2023 04:59PM UTC coverage: 50.458% (+0.01%) from 50.444%
#41

push

github-actions

web-flow
Fix Checkstyle Violations in "Impl" Classes (#53)

241 of 241 new or added lines in 32 files covered. (100.0%)

3417 of 6772 relevant lines covered (50.46%)

0.5 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

34.94
/src/main/java/com/smartsheet/api/internal/UserResourcesImpl.java
1
package com.smartsheet.api.internal;
2

3
/*
4
 * #[license]
5
 * Smartsheet SDK for Java
6
 * %%
7
 * Copyright (C) 2023 Smartsheet
8
 * %%
9
 * Licensed under the Apache License, Version 2.0 (the "License");
10
 * you may not use this file except in compliance with the License.
11
 * You may obtain a copy of the License at
12
 *
13
 *      http://www.apache.org/licenses/LICENSE-2.0
14
 *
15
 * Unless required by applicable law or agreed to in writing, software
16
 * distributed under the License is distributed on an "AS IS" BASIS,
17
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
 * See the License for the specific language governing permissions and
19
 * limitations under the License.
20
 * %[license]
21
 */
22

23
import com.smartsheet.api.AuthorizationException;
24
import com.smartsheet.api.InvalidRequestException;
25
import com.smartsheet.api.ResourceNotFoundException;
26
import com.smartsheet.api.ServiceUnavailableException;
27
import com.smartsheet.api.SmartsheetException;
28
import com.smartsheet.api.UserResources;
29
import com.smartsheet.api.internal.http.HttpEntity;
30
import com.smartsheet.api.internal.http.HttpMethod;
31
import com.smartsheet.api.internal.http.HttpRequest;
32
import com.smartsheet.api.internal.http.HttpResponse;
33
import com.smartsheet.api.internal.util.QueryUtil;
34
import com.smartsheet.api.internal.util.Util;
35
import com.smartsheet.api.models.AlternateEmail;
36
import com.smartsheet.api.models.DeleteUserParameters;
37
import com.smartsheet.api.models.PagedResult;
38
import com.smartsheet.api.models.PaginationParameters;
39
import com.smartsheet.api.models.Sheet;
40
import com.smartsheet.api.models.User;
41
import com.smartsheet.api.models.UserProfile;
42
import com.smartsheet.api.models.enums.ListUserInclusion;
43
import com.smartsheet.api.models.enums.UserInclusion;
44

45
import java.io.File;
46
import java.io.FileInputStream;
47
import java.io.FileNotFoundException;
48
import java.io.InputStream;
49
import java.net.URLEncoder;
50
import java.nio.charset.StandardCharsets;
51
import java.text.SimpleDateFormat;
52
import java.util.Date;
53
import java.util.EnumSet;
54
import java.util.HashMap;
55
import java.util.List;
56
import java.util.Map;
57
import java.util.Set;
58

59
/**
60
 * This is the implementation of the UserResources.
61
 * <p>
62
 * Thread Safety: This class is thread safe because it is immutable and its base class is thread safe.
63
 */
64
public class UserResourcesImpl extends AbstractResources implements UserResources {
65

66
    private static final String USERS = "users";
67
    private static final String ALTERNATE_EMAILS = "alternateemails";
68

69
    /**
70
     * Constructor.
71
     * <p>
72
     * Exceptions: - IllegalArgumentException : if any argument is null
73
     *
74
     * @param smartsheet the smartsheet
75
     */
76
    public UserResourcesImpl(SmartsheetImpl smartsheet) {
77
        super(smartsheet);
1✔
78
    }
1✔
79

80
    /**
81
     * List all users.
82
     * <p>
83
     * It mirrors to the following Smartsheet REST API method: GET /users
84
     *
85
     * @return the list of all users
86
     * @throws IllegalArgumentException if any argument is null or empty string
87
     * @throws InvalidRequestException if there is any problem with the REST API request
88
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
89
     * @throws ResourceNotFoundException if the resource cannot be found
90
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
91
     * @throws SmartsheetException if there is any other error during the operation
92
     */
93
    public PagedResult<User> listUsers() throws SmartsheetException {
94
        return this.listResourcesWithWrapper(USERS, User.class);
1✔
95
    }
96

97
    /**
98
     * List all users.
99
     * <p>
100
     * It mirrors to the following Smartsheet REST API method: GET /users
101
     * <p>
102
     * Exceptions:
103
     *   - InvalidRequestException : if there is any problem with the REST API request
104
     *   - AuthorizationException : if there is any problem with the REST API authorization(access token)
105
     *   - ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
106
     *   - SmartsheetRestException : if there is any other REST API related error occurred during the operation
107
     *   - SmartsheetException : if there is any other error occurred during the operation
108
     *
109
     * @param email the list of email addresses
110
     * @param pagination the object containing the pagination query parameters
111
     * @return all users (note that empty list will be returned if there is none)
112
     * @throws SmartsheetException the smartsheet exception
113
     */
114
    public PagedResult<User> listUsers(Set<String> email, PaginationParameters pagination) throws SmartsheetException {
115
        return this.listUsers(email, null, pagination);
1✔
116
    }
117

118
    /**
119
     * List all users.
120
     * <p>
121
     * It mirrors to the following Smartsheet REST API method: GET /users
122
     * <p>
123
     * Exceptions:
124
     *   - InvalidRequestException : if there is any problem with the REST API request
125
     *   - AuthorizationException : if there is any problem with the REST API authorization(access token)
126
     *   - ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
127
     *   - SmartsheetRestException : if there is any other REST API related error occurred during the operation
128
     *   - SmartsheetException : if there is any other error occurred during the operation
129
     *
130
     * @param email the list of email addresses
131
     * @param includes elements to include in the response
132
     * @param pagination the object containing the pagination query parameters
133
     * @return all users (note that empty list will be returned if there is none)
134
     * @throws SmartsheetException the smartsheet exception
135
     */
136
    public PagedResult<User> listUsers(Set<String> email, EnumSet<ListUserInclusion> includes,
137
                                       PaginationParameters pagination) throws SmartsheetException {
138
        String path = USERS;
1✔
139
        Map<String, Object> parameters = new HashMap<>();
1✔
140

141
        if (pagination != null) {
1✔
142
            parameters = pagination.toHashMap();
1✔
143
        }
144
        parameters.put("email", QueryUtil.generateCommaSeparatedList(email));
1✔
145
        parameters.put("include", QueryUtil.generateCommaSeparatedList(includes));
1✔
146

147
        path += QueryUtil.generateUrl(null, parameters);
1✔
148
        return this.listResourcesWithWrapper(path, User.class);
1✔
149
    }
150

151
    /**
152
     * Add a user to the organization, without sending email.
153
     * <p>
154
     * It mirrors to the following Smartsheet REST API method: POST /users
155
     * <p>
156
     * Exceptions:
157
     *   - IllegalArgumentException : if any argument is null
158
     *   - InvalidRequestException : if there is any problem with the REST API request
159
     *   - AuthorizationException : if there is any problem with the REST API authorization(access token)
160
     *   - ResourceNotFoundException : if the resource can not be found
161
     *   - ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
162
     *   - SmartsheetRestException : if there is any other REST API related error occurred during the operation
163
     *   - SmartsheetException : if there is any other error occurred during the operation
164
     *
165
     * @param user the user object limited to the following attributes: * admin * email * licensedSheetCreator
166
     * @return the user
167
     * @throws SmartsheetException the smartsheet exception
168
     */
169
    public User addUser(User user) throws SmartsheetException {
170
        return this.createResource(USERS, User.class, user);
1✔
171
    }
172

173
    /**
174
     * Add a user to the organization, without sending email.
175
     * <p>
176
     * It mirrors to the following Smartsheet REST API method: POST /users
177
     * <p>
178
     * Exceptions:
179
     *   - IllegalArgumentException : if any argument is null
180
     *   - InvalidRequestException : if there is any problem with the REST API request
181
     *   - AuthorizationException : if there is any problem with the REST API authorization(access token)
182
     *   - ResourceNotFoundException : if the resource can not be found
183
     *   - ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
184
     *   - SmartsheetRestException : if there is any other REST API related error occurred during the operation
185
     *   - SmartsheetException : if there is any other error occurred during the operation
186
     *
187
     * @param user the created user
188
     * @param sendEmail whether to send email
189
     * @return the user object limited to the following attributes: * admin * email * licensedSheetCreator
190
     * @throws SmartsheetException the smartsheet exception
191
     */
192
    public User addUser(User user, boolean sendEmail) throws SmartsheetException {
193
        return this.createResource("users?sendEmail=" + sendEmail, User.class, user);
1✔
194
    }
195

196
    /**
197
     * Get the current user.
198
     * <p>
199
     * It mirrors to the following Smartsheet REST API method: GET /users/{userId}
200
     *
201
     * @param userId the user id
202
     * @return the user
203
     * @throws IllegalArgumentException if any argument is null or empty string
204
     * @throws InvalidRequestException if there is any problem with the REST API request
205
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
206
     * @throws ResourceNotFoundException if the resource cannot be found
207
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
208
     * @throws SmartsheetException if there is any other error during the operation
209
     */
210
    public UserProfile getUser(long userId) throws SmartsheetException {
211
        return this.getResource(USERS + "/" + userId, UserProfile.class);
1✔
212
    }
213

214
    /**
215
     * Get the current user.
216
     * <p>
217
     * It mirrors to the following Smartsheet REST API method: GET /users/me
218
     * <p>
219
     * Exceptions:
220
     *   - InvalidRequestException : if there is any problem with the REST API request
221
     *   - AuthorizationException : if there is any problem with the REST API authorization(access token)
222
     *   - ResourceNotFoundException : if the resource can not be found
223
     *   - ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
224
     *   - SmartsheetRestException : if there is any other REST API related error occurred during the operation
225
     *   - SmartsheetException : if there is any other error occurred during the operation
226
     *
227
     * @return the resource (note that if there is no such resource, this method will throw ResourceNotFoundException
228
     *     rather than returning null).
229
     * @throws SmartsheetException the smartsheet exception
230
     */
231
    public UserProfile getCurrentUser() throws SmartsheetException {
232
        return this.getResource("users/me", UserProfile.class);
1✔
233
    }
234

235
    /**
236
     * <p>Get the current user.</p>
237
     *
238
     * <p>It mirrors to the following Smartsheet REST API method: GET /user/me</p>
239
     *
240
     * @param includes used to specify the optional objects to include.
241
     * @return the current user
242
     * @throws IllegalArgumentException if any argument is null or empty string
243
     * @throws InvalidRequestException if there is any problem with the REST API request
244
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
245
     * @throws ResourceNotFoundException if the resource cannot be found
246
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
247
     * @throws SmartsheetException if there is any other error during the operation
248
     */
249
    public UserProfile getCurrentUser(EnumSet<UserInclusion> includes) throws SmartsheetException {
250
        String path = "users/me";
×
251

252
        Map<String, Object> parameters = new HashMap<>();
×
253
        parameters.put("include", QueryUtil.generateCommaSeparatedList(includes));
×
254

255
        path += QueryUtil.generateUrl(null, parameters);
×
256
        return this.getResource(path, UserProfile.class);
×
257
    }
258

259
    /**
260
     * List all organisation sheets.
261
     * <p>
262
     * It mirrors to the following Smartsheet REST API method: GET /users/sheets
263
     *
264
     * @param pagination the object containing the pagination query parameters
265
     * @return the list of all organisation sheets
266
     * @throws IllegalArgumentException if any argument is null or empty string
267
     * @throws InvalidRequestException if there is any problem with the REST API request
268
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
269
     * @throws ResourceNotFoundException if the resource cannot be found
270
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
271
     * @throws SmartsheetException if there is any other error during the operation
272
     */
273
    public PagedResult<Sheet> listOrgSheets(PaginationParameters pagination, Date modifiedSince) throws SmartsheetException {
274
        String path = "users/sheets";
1✔
275

276
        Map<String, Object> parameters = new HashMap<>();
1✔
277
        if (pagination != null) {
1✔
278
            parameters = pagination.toHashMap();
1✔
279
        }
280
        if (modifiedSince != null) {
1✔
281
            String isoDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(modifiedSince);
×
282
            parameters.put("modifiedSince", isoDate);
×
283
        }
284
        path += QueryUtil.generateUrl(null, parameters);
1✔
285
        return this.listResourcesWithWrapper(path, Sheet.class);
1✔
286
    }
287

288
    public PagedResult<Sheet> listOrgSheets(PaginationParameters pagination) throws SmartsheetException {
289
        return this.listOrgSheets(pagination, null);
×
290
    }
291

292
    /**
293
     * List all user alternate emails.
294
     * <p>
295
     * It mirrors to the following Smartsheet REST API method: GET /users/{userId}/alternateemails
296
     *
297
     * @param userId the id of the user
298
     * @param pagination the object containing the pagination query parameters
299
     * @return the list of all user alternate emails
300
     * @throws IllegalArgumentException if any argument is null or empty string
301
     * @throws InvalidRequestException if there is any problem with the REST API request
302
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
303
     * @throws ResourceNotFoundException if the resource cannot be found
304
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
305
     * @throws SmartsheetException if there is any other error during the operation
306
     */
307
    public PagedResult<AlternateEmail> listAlternateEmails(long userId, PaginationParameters pagination) throws SmartsheetException {
308
        String path = USERS + "/" + userId + "/" + ALTERNATE_EMAILS;
×
309

310
        if (pagination != null) {
×
311
            path += pagination.toQueryString();
×
312
        }
313
        return this.listResourcesWithWrapper(path, AlternateEmail.class);
×
314
    }
315

316
    /**
317
     * Get alternate email.
318
     * <p>
319
     * It mirrors to the following Smartsheet REST API method: GET /users/{userId}/alternateemails/{alternateEmailId}
320
     *
321
     * @param userId the id of the user
322
     * @param altEmailId the alternate email id for the alternate email to retrieve.
323
     * @return the resource (note that if there is no such resource, this method will throw
324
     *     ResourceNotFoundException rather than returning null).
325
     * @throws IllegalArgumentException if any argument is null or empty string
326
     * @throws InvalidRequestException if there is any problem with the REST API request
327
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
328
     * @throws ResourceNotFoundException if the resource cannot be found
329
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
330
     * @throws SmartsheetException if there is any other error during the operation
331
     */
332
    public AlternateEmail getAlternateEmail(long userId, long altEmailId) throws SmartsheetException {
333
        return this.getResource(USERS + "/" + userId + "/" + ALTERNATE_EMAILS + "/" + altEmailId, AlternateEmail.class);
×
334
    }
335

336
    /**
337
     * Add an alternate email.
338
     * <p>
339
     * It mirrors to the following Smartsheet REST API method: POST /users/{userId}/alternateemails
340
     *
341
     * @param userId the id of the user
342
     * @param altEmails AlternateEmail alternate email address to add.
343
     * @return the resource (note that if there is no such resource, this method will throw
344
     *     ResourceNotFoundException rather than returning null).
345
     * @throws IllegalArgumentException if any argument is null or empty string
346
     * @throws InvalidRequestException if there is any problem with the REST API request
347
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
348
     * @throws ResourceNotFoundException if the resource cannot be found
349
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
350
     * @throws SmartsheetException if there is any other error during the operation
351
     */
352
    public List<AlternateEmail> addAlternateEmail(long userId, List<AlternateEmail> altEmails) throws SmartsheetException {
353
        Util.throwIfNull(altEmails);
×
354
        if (altEmails.size() == 0) {
×
355
            return altEmails;
×
356
        }
357
        return this.postAndReceiveList(USERS + "/" + userId + "/" + ALTERNATE_EMAILS, altEmails, AlternateEmail.class);
×
358
    }
359

360
    /**
361
     * Delete an alternate email.
362
     * <p>
363
     * It mirrors to the following Smartsheet REST API method: DELETE /users/{userId}/alternateemails/{alternateEmailId}
364
     *
365
     * @param userId the id of the user
366
     * @param altEmailId the alternate email id for the alternate email to retrieve.
367
     * @throws IllegalArgumentException if any argument is null or empty string
368
     * @throws InvalidRequestException if there is any problem with the REST API request
369
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
370
     * @throws ResourceNotFoundException if the resource cannot be found
371
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
372
     * @throws SmartsheetException if there is any other error during the operation
373
     */
374
    public void deleteAlternateEmail(long userId, long altEmailId) throws SmartsheetException {
375
        this.deleteResource(USERS + "/" + userId + "/" + ALTERNATE_EMAILS + "/" + altEmailId, AlternateEmail.class);
×
376
    }
×
377

378
    /**
379
     * Promote and alternate email to primary.
380
     *
381
     * @param userId id of the user
382
     * @param altEmailId alternate email id
383
     * @return alternateEmail of the primary
384
     * @throws IllegalArgumentException if any argument is null or empty string
385
     * @throws InvalidRequestException if there is any problem with the REST API request
386
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
387
     * @throws ResourceNotFoundException if the resource cannot be found
388
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
389
     * @throws SmartsheetException f there is any other error during the operation
390
     */
391
    public AlternateEmail promoteAlternateEmail(long userId, long altEmailId) throws SmartsheetException {
392

393
        HttpRequest request = createHttpRequest(smartsheet.getBaseURI().resolve(
×
394
                USERS + "/" + userId + "/" + ALTERNATE_EMAILS + "/" + altEmailId + "/makeprimary"), HttpMethod.POST);
395

396
        Object obj = null;
×
397
        try {
398
            HttpResponse response = this.smartsheet.getHttpClient().request(request);
×
399
            switch (response.getStatusCode()) {
×
400
                case 200:
401
                    obj = this.smartsheet.getJsonSerializer().deserializeResult(AlternateEmail.class,
×
402
                            response.getEntity().getContent());
×
403
                    break;
×
404
                default:
405
                    handleError(response);
×
406
            }
407
        } finally {
408
            smartsheet.getHttpClient().releaseConnection();
×
409
        }
410

411
        return (AlternateEmail) obj;
×
412
    }
413

414
    /**
415
     * Uploads a profile image for the specified user.
416
     *
417
     * @param userId id of the user
418
     * @param file path to the image file
419
     * @param fileType content type of the image file
420
     * @return user
421
     * @throws IllegalArgumentException if any argument is null or empty string
422
     * @throws InvalidRequestException if there is any problem with the REST API request
423
     * @throws AuthorizationException if there is any problem with  the REST API authorization (access token)
424
     * @throws ResourceNotFoundException if the resource cannot be found
425
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
426
     * @throws SmartsheetException f there is any other error during the operation
427
     */
428
    public User addProfileImage(long userId, String file, String fileType) throws SmartsheetException, FileNotFoundException {
429
        return attachProfileImage(USERS + "/" + userId + "/profileimage", file, fileType);
×
430
    }
431

432
    private User attachProfileImage(String path, String file, String contentType) throws SmartsheetException, FileNotFoundException {
433
        Util.throwIfNull(file);
×
434

435
        if (contentType == null) {
×
436
            contentType = "application/octet-stream";
×
437
        }
438

439
        Map<String, Object> parameters = new HashMap<>();
×
440
        path += QueryUtil.generateUrl(null, parameters);
×
441

442
        HttpRequest request = createHttpRequest(this.smartsheet.getBaseURI().resolve(path), HttpMethod.POST);
×
443
        String attachmentHeaderValue = "attachment; filename=\"" + URLEncoder.encode(file, StandardCharsets.UTF_8) + "\"";
×
444
        request.getHeaders().put("Content-Disposition", attachmentHeaderValue);
×
445

446
        File f = new File(file);
×
447
        InputStream is = new FileInputStream(f);
×
448

449
        HttpEntity entity = new HttpEntity();
×
450
        entity.setContentType(contentType);
×
451
        entity.setContent(is);
×
452
        entity.setContentLength(f.length());
×
453
        request.setEntity(entity);
×
454

455
        User obj = null;
×
456
        try {
457
            HttpResponse response = this.smartsheet.getHttpClient().request(request);
×
458
            switch (response.getStatusCode()) {
×
459
                case 200:
460
                    obj = this.smartsheet.getJsonSerializer().deserializeResult(User.class,
×
461
                            response.getEntity().getContent()).getResult();
×
462
                    break;
×
463
                default:
464
                    handleError(response);
×
465
            }
466
        } finally {
467
            smartsheet.getHttpClient().releaseConnection();
×
468
        }
469

470
        return obj;
×
471
    }
472

473
    @Override
474
    public User updateUser(User user) throws SmartsheetException {
475
        return this.updateResource(USERS + "/" + user.getId(), User.class, user);
1✔
476
    }
477

478
    @Override
479
    public void deleteUser(long userId, DeleteUserParameters parameters) throws SmartsheetException {
480
        String path = USERS + "/" + userId;
1✔
481

482
        if (parameters != null) {
1✔
483
            path += parameters.toQueryString();
1✔
484
        }
485

486
        this.deleteResource(path, User.class);
1✔
487
    }
1✔
488
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc