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

box / box-java-sdk / #4076

12 Nov 2024 10:48AM UTC coverage: 71.771% (+0.03%) from 71.737%
#4076

Pull #1272

github

web-flow
Merge 8c67b7acb into 6ea70f79a
Pull Request #1272: feat: allow to modify the underlying client from the Box API connection subclasses

3 of 4 new or added lines in 4 files covered. (75.0%)

98 existing lines in 4 files now uncovered.

8052 of 11219 relevant lines covered (71.77%)

0.72 hits per line

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

81.82
/src/main/java/com/box/sdk/BoxCCGAPIConnection.java
1
package com.box.sdk;
2

3
import static com.box.sdk.http.ContentType.APPLICATION_FORM_URLENCODED;
4

5
import com.eclipsesource.json.Json;
6
import com.eclipsesource.json.JsonObject;
7
import java.net.URL;
8
import okhttp3.OkHttpClient;
9

10
/**
11
 * Creates and manages Client Credentials Grant API connection.
12
 */
13
public final class BoxCCGAPIConnection extends BoxAPIConnection {
14

15
    static final String ENTERPRISE_SUBJECT_TYPE = "enterprise";
16
    static final String USER_SUBJECT_TYPE = "user";
17
    private String subjectType;
18
    private String subjectId;
19

20
    // Hiding constructor
21
    private BoxCCGAPIConnection(String accessToken) {
22
        super(accessToken);
×
UNCOV
23
    }
×
24

25
    // Hiding constructor
26
    private BoxCCGAPIConnection(String clientID, String clientSecret, String accessToken, String refreshToken) {
27
        super(clientID, clientSecret, accessToken, refreshToken);
×
UNCOV
28
    }
×
29

30
    // Hiding constructor
31
    private BoxCCGAPIConnection(String clientID, String clientSecret, String authCode) {
32
        super(clientID, clientSecret, authCode);
×
UNCOV
33
    }
×
34

35
    // Hiding constructor
36
    private BoxCCGAPIConnection(String clientID, String clientSecret) {
37
        super(clientID, clientSecret);
1✔
38
    }
1✔
39

40
    // Hiding constructor
41
    private BoxCCGAPIConnection(BoxConfig boxConfig) {
42
        super(boxConfig);
×
UNCOV
43
    }
×
44

45
    /**
46
     * {@inheritDoc}
47
     */
48
    @Override
49
    protected OkHttpClient.Builder modifyHttpClientBuilder(OkHttpClient.Builder httpClientBuilder) {
50
        return super.modifyHttpClientBuilder(httpClientBuilder);
1✔
51
    }
52

53
    /**
54
     * Creates connection that authenticates as a Service Account
55
     *
56
     * @param clientId     the client ID to use when getting the access token.
57
     * @param clientSecret the client secret to use when getting the access token.
58
     * @param enterpriseId the enterprise ID to use when getting the access token.
59
     * @return Client Credentials Grant API connection.
60
     */
61
    public static BoxCCGAPIConnection applicationServiceAccountConnection(
62
        String clientId, String clientSecret, String enterpriseId
63
    ) {
64
        BoxCCGAPIConnection api = new BoxCCGAPIConnection(clientId, clientSecret);
1✔
65
        api.subjectType = ENTERPRISE_SUBJECT_TYPE;
1✔
66
        api.subjectId = enterpriseId;
1✔
67
        return api;
1✔
68
    }
69

70
    /**
71
     * Creates connection that authenticates as a User
72
     *
73
     * @param clientId     the client ID to use when getting the access token.
74
     * @param clientSecret the client secret to use when getting the access token.
75
     * @param userId       the user ID to use when getting the access token.
76
     * @return Client Credentials Grant API connection.
77
     */
78
    public static BoxCCGAPIConnection userConnection(String clientId, String clientSecret, String userId) {
79
        BoxCCGAPIConnection api = new BoxCCGAPIConnection(clientId, clientSecret);
1✔
80
        api.subjectType = USER_SUBJECT_TYPE;
1✔
81
        api.subjectId = userId;
1✔
82
        return api;
1✔
83
    }
84

85
    /**
86
     * Restores a BoxAPIConnection from a saved state.
87
     *
88
     * @param clientID     the client ID to use with the connection.
89
     * @param clientSecret the client secret to use with the connection.
90
     * @param state        the saved state that was created with {@link #save}.
91
     * @return a restored API connection.
92
     * @see #save
93
     */
94
    public static BoxCCGAPIConnection restore(String clientID, String clientSecret, String state) {
95
        BoxCCGAPIConnection api = new BoxCCGAPIConnection(clientID, clientSecret);
1✔
96
        api.restore(state);
1✔
97
        return api;
1✔
98
    }
99

100
    @Override
101
    protected BoxAPIRequest createTokenRequest(URL url) {
102
        String urlParameters = String.format(
1✔
103
            "grant_type=client_credentials&client_id=%s&client_secret=%s&box_subject_type=%s&box_subject_id=%s",
104
            this.getClientID(), this.getClientSecret(), this.subjectType, this.subjectId);
1✔
105
        BoxAPIRequest request = new BoxAPIRequest(this, url, "POST");
1✔
106
        request.shouldAuthenticate(false);
1✔
107
        request.setBody(urlParameters);
1✔
108
        request.addHeader("Content-Type", APPLICATION_FORM_URLENCODED);
1✔
109
        return request;
1✔
110
    }
111

112
    @Override
113
    protected void extractTokens(JsonObject jsonObject) {
114
        this.setAccessToken(jsonObject.get("access_token").asString());
1✔
115
        this.setLastRefresh(System.currentTimeMillis());
1✔
116
        this.setExpires(jsonObject.get("expires_in").asLong() * 1000);
1✔
117
    }
1✔
118

119
    @Override
120
    public boolean canRefresh() {
121
        return true;
1✔
122
    }
123

124
    public boolean isUserConnection() {
125
        return subjectType.equals(USER_SUBJECT_TYPE);
1✔
126
    }
127

128
    @Override
129
    public String save() {
130
        JsonObject state = Json.parse(super.save()).asObject();
1✔
131
        state.add("subjectType", this.subjectType);
1✔
132
        state.add("subjectId", this.subjectId);
1✔
133
        return state.toString();
1✔
134
    }
135

136
    @Override
137
    public void restore(String state) {
138
        super.restore(state);
1✔
139

140
        JsonObject json = Json.parse(state).asObject();
1✔
141
        this.subjectType = json.get("subjectType").asString();
1✔
142
        this.subjectId = json.get("subjectId").asString();
1✔
143
    }
1✔
144
}
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