Skip to main content
summaryrefslogtreecommitdiffstats
blob: 33eafdc7fb3c02764c7db4f93eb0584310128dde (plain) (blame)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/*
 * Copyright (c) 2015 Eike Stepper (Berlin, Germany) 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.userstorage.internal;

import org.eclipse.userstorage.IStorageService;
import org.eclipse.userstorage.internal.util.StringUtil;
import org.eclipse.userstorage.spi.ICredentialsProvider;
import org.eclipse.userstorage.util.ConflictException;

import org.eclipse.equinox.security.storage.ISecurePreferences;
import org.eclipse.equinox.security.storage.StorageException;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.Semaphore;

/**
 * @author Eike Stepper
 */
public class StorageService implements IStorageService
{
  private static final String USERNAME_KEY = "username";

  private static final String PASSWORD_KEY = "password";

  private static final String TERMS_OF_USE_AGREED_KEY = "termsOfUseAgreed";

  private final Semaphore authenticationSemaphore = new Semaphore(1);

  private final String serviceLabel;

  private final URI serviceURI;

  private final URI createAccountURI;

  private final URI editAccountURI;

  private final URI recoverPasswordURI;

  private final String termsOfUseLink;

  private ICredentialsProvider credentialsProvider;

  private Session session;

  public StorageService(String serviceLabel, URI serviceURI, URI createAccountURI, URI editAccountURI, URI recoverPasswordURI, String termsOfUseLink)
  {
    if (StringUtil.isEmpty(serviceLabel))
    {
      throw new IllegalArgumentException("Service label is null or empty");
    }

    if (serviceURI == null)
    {
      throw new IllegalArgumentException("Service URI is null");
    }

    this.serviceLabel = serviceLabel;
    this.serviceURI = serviceURI;
    this.createAccountURI = createAccountURI;
    this.editAccountURI = editAccountURI;
    this.recoverPasswordURI = recoverPasswordURI;
    this.termsOfUseLink = termsOfUseLink;
  }

  @Override
  public String getServiceLabel()
  {
    return serviceLabel;
  }

  @Override
  public URI getServiceURI()
  {
    return serviceURI;
  }

  @Override
  public URI getCreateAccountURI()
  {
    return createAccountURI;
  }

  @Override
  public URI getEditAccountURI()
  {
    return editAccountURI;
  }

  @Override
  public URI getRecoverPasswordURI()
  {
    return recoverPasswordURI;
  }

  @Override
  public String getTermsOfUseLink()
  {
    return termsOfUseLink;
  }

  @Override
  public Semaphore getAuthenticationSemaphore()
  {
    return authenticationSemaphore;
  }

  public Credentials getCredentials()
  {
    try
    {
      ISecurePreferences securePreferences = getSecurePreferences();
      if (securePreferences != null)
      {
        String username = securePreferences.get(USERNAME_KEY, null);
        String password = securePreferences.get(PASSWORD_KEY, null);

        if (StringUtil.isEmpty(username))
        {
          username = null;
        }

        if (StringUtil.isEmpty(password))
        {
          password = null;
        }

        if (username != null || password != null)
        {
          return new Credentials(username, password);
        }
      }
    }
    catch (StorageException ex)
    {
      Activator.log(ex);
    }

    return null;
  }

  public void setCredentials(Credentials credentials)
  {
    try
    {
      ISecurePreferences securePreferences = getSecurePreferences();
      if (securePreferences != null)
      {
        String username = credentials == null ? null : credentials.getUsername();
        if (StringUtil.isEmpty(username))
        {
          securePreferences.remove(USERNAME_KEY);
        }
        else
        {
          securePreferences.put(USERNAME_KEY, username, false);
        }

        String password = credentials == null ? null : credentials.getPassword();
        if (StringUtil.isEmpty(password))
        {
          securePreferences.remove(PASSWORD_KEY);
        }
        else
        {
          securePreferences.put(PASSWORD_KEY, password, true);
        }

        securePreferences.flush();
      }
    }
    catch (Exception ex)
    {
      Activator.log(ex);
    }
    finally
    {
      if (session != null)
      {
        session.reset();
      }
    }
  }

  public boolean isTermsOfUseAgreed()
  {
    try
    {
      ISecurePreferences securePreferences = getSecurePreferences();
      if (securePreferences != null)
      {
        String value = securePreferences.get(TERMS_OF_USE_AGREED_KEY, null);
        if ("true".equalsIgnoreCase(value))
        {
          return true;
        }
      }
    }
    catch (StorageException ex)
    {
      Activator.log(ex);
    }

    return false;
  }

  public void setTermsOfUseAgreed(boolean agreed)
  {
    try
    {
      ISecurePreferences securePreferences = getSecurePreferences();
      if (securePreferences != null)
      {
        if (agreed)
        {
          securePreferences.putBoolean(TERMS_OF_USE_AGREED_KEY, true, false);
        }
        else
        {
          securePreferences.remove(TERMS_OF_USE_AGREED_KEY);
        }

        securePreferences.flush();
      }
    }
    catch (Exception ex)
    {
      Activator.log(ex);
    }
    finally
    {
      if (session != null && !agreed)
      {
        session.reset();
      }
    }
  }

  public Map<String, Map<String, Object>> retrieveProperties(ICredentialsProvider credentialsProvider, String applicationToken, int pageSize, int page)
      throws IOException
  {
    if (credentialsProvider == null)
    {
      credentialsProvider = getCredentialsProvider();
    }

    Session session = getSession();
    return session.retrieveProperties(applicationToken, credentialsProvider, pageSize, page);
  }

  public synchronized InputStream retrieveBlob(ICredentialsProvider credentialsProvider, String applicationToken, String key, Map<String, String> properties,
      boolean useETag) throws IOException
  {
    if (credentialsProvider == null)
    {
      credentialsProvider = getCredentialsProvider();
    }

    Session session = getSession();
    return session.retrieveBlob(applicationToken, key, properties, useETag, credentialsProvider);
  }

  public synchronized boolean updateBlob(ICredentialsProvider credentialsProvider, String applicationToken, String key, Map<String, String> properties,
      InputStream in) throws IOException, ConflictException
  {
    if (credentialsProvider == null)
    {
      credentialsProvider = getCredentialsProvider();
    }

    Session session = getSession();
    return session.updateBlob(applicationToken, key, properties, in, credentialsProvider);
  }

  public synchronized void deleteBlob(ICredentialsProvider credentialsProvider, String applicationToken, String key, Map<String, String> properties)
      throws IOException, ConflictException
  {
    if (credentialsProvider == null)
    {
      credentialsProvider = getCredentialsProvider();
    }

    Session session = getSession();
    session.deleteBlob(applicationToken, key, properties, credentialsProvider);
  }

  @Override
  public int hashCode()
  {
    final int prime = 31;
    int result = 1;
    result = prime * result + (serviceURI == null ? 0 : serviceURI.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj)
  {
    if (this == obj)
    {
      return true;
    }

    if (obj == null)
    {
      return false;
    }

    if (getClass() != obj.getClass())
    {
      return false;
    }

    StorageService other = (StorageService)obj;
    if (serviceURI == null)
    {
      if (other.serviceURI != null)
      {
        return false;
      }
    }
    else if (!serviceURI.equals(other.serviceURI))
    {
      return false;
    }

    return true;
  }

  @Override
  public int compareTo(IStorageService o)
  {
    return serviceLabel.compareTo(o.getServiceLabel());
  }

  @Override
  public String toString()
  {
    return serviceLabel;
  }

  public ISecurePreferences getSecurePreferences()
  {
    ISecurePreferences securePreferences = Activator.getSecurePreferences();
    if (securePreferences != null)
    {
      String serviceNode = StringUtil.encodeURI(serviceURI);
      return securePreferences.node(serviceNode);
    }

    return null;
  }

  private ICredentialsProvider getCredentialsProvider()
  {
    if (credentialsProvider == null)
    {
      String property = System.getProperty(StorageProperties.CREDENTIALS_PROVIDER, null);
      if (property != null)
      {
        try
        {
          @SuppressWarnings("unchecked")
          Class<ICredentialsProvider> c = (Class<ICredentialsProvider>)Class.forName(property);
          credentialsProvider = c.newInstance();
        }
        catch (Throwable ex)
        {
          Activator.log(ex);
        }
      }

      if (credentialsProvider == null)
      {
        credentialsProvider = Activator.getCredentialsProvider();
      }
    }

    return credentialsProvider;
  }

  private Session getSession()
  {
    if (session == null)
    {
      session = openSession();
    }

    return session;
  }

  private Session openSession()
  {
    return new Session(this);
  }

  /**
   * @author Eike Stepper
   */
  public static final class DynamicService extends StorageService implements IStorageService.Dynamic
  {
    public DynamicService(String serviceLabel, URI serviceURI, URI createAccountURI, URI editAccountURI, URI recoverPasswordURI, String termsOfUseLink)
    {
      super(serviceLabel, serviceURI, createAccountURI, editAccountURI, recoverPasswordURI, termsOfUseLink);
    }

    @Override
    public void remove()
    {
      StorageServiceRegistry.INSTANCE.removeService(this);

      ISecurePreferences securePreferences = getSecurePreferences();
      if (securePreferences != null)
      {
        try
        {
          ISecurePreferences parent = securePreferences.parent();
          securePreferences.removeNode();
          parent.flush();
        }
        catch (Exception ex)
        {
          Activator.log(ex);
        }
      }
    }
  }
}

Back to the top