Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@

# OneDrive SDK

## Quickstart
onedrive v1 api client.

## Usage

## Install

* [maven](https://jitpack.io/#umjammer/OneDriveJavaSDK)

## Usage

### Quickstart
Register your app. For more info see [Registration](#registration)

```java
Expand All @@ -29,19 +39,17 @@ System.out.println(rootFolder);

A console client with several command can be found in de.tuberlin.onedrivesdk.example.ConsoleClient.

## Registration
### Registration

1. Register your client application on [Mirosoft Developers](http://go.microsoft.com/fwlink/p/?LinkId=193157)
2. The drive is created at the first login. Login into your account in the web browser, otherwise you will get an authentication error if you try to run the SDK with your credentials.
- The user have to be logged in at least once to use your application.
3. (optional) Register a website if you want to use this SDK in a web app. Otherwise you can register http://localhost.
4. A development authentication token can be obtained on [OneDrive authentication](https://dev.onedrive.com/auth/msa_oauth.htm).
5. More details can be found [here](https://dev.onedrive.com/app-registration.htm)

## Installation

https://jitpack.io/#umjammer/OneDriveJavaSDK
### Recommended CodeFlow for user authentication

## Recommended CodeFlow for user authentication
The first time a new user uses your application he or she needs to go to the authentication URL. The following code gets that URL:

```java
Expand All @@ -68,10 +76,10 @@ String refreshToken = sdk.getRefreshToken();
sdk.authenticateWithRefreshToken("[refreshToken]");
```

## Featuers
### Featuers

### TextFile Configuration
#### [Credentials](http://tawalaya.github.io/OneDriveJavaSDK/docs/de/tuberlin/onedrivesdk/common/OneDriveCredentials.html)
#### TextFile Configuration
##### [Credentials](http://tawalaya.github.io/OneDriveJavaSDK/docs/de/tuberlin/onedrivesdk/common/OneDriveCredentials.html)
Instead of manually providing the clientID and clientSecret every time you create the SDK handle and store them within your source code you can use a *credentails.prperties* file. The File needs to be stored in the root folder of your application.

It should look somewhat like this:
Expand All @@ -87,16 +95,16 @@ OneDriveSDK sdk = OneDriveFactory.
createOneDriveSDK("[returnUrl]",OneDriveScope.READWRITE);
```

#### Logging
##### Logging
This library uses log4j2 to handle logging. Therefore you can specify a log4j configuration to see helpful information about the inner working of the SDK.

More information can be found [here](http://logging.apache.org/log4j/2.x/manual/configuration.html)

### Automatic Refresh
#### Automatic Refresh
The SDK provides the ability to continuously refresh the authentication token. This is useful if your application will need to use OneDrive over extended period of time.
After the application went through the authentication steps described [here](http://tawalaya.github.io/OneDriveJavaSDK/README.md#Recommended%20CodeFlow%20for%20user%20authentication) the you can call the`sdk.startSessionAutoRefresh()` method. Now keep in mind that you need to terminate that thread if you want your application to close normally. To do that you can call the `sdk.disconnect()` method.

### Offered Operations
#### Offered Operations
<!-- TODO add JDOC URLs -->
[SDK](http://tawalaya.github.io/OneDriveJavaSDK/docs/de/tuberlin/onedrivesdk/OneDriveSDK.html):
* get file or folder by id or path:
Expand All @@ -122,18 +130,19 @@ Blocking Operations:
* download file
* upload file


### Error handling
#### Error handling
There are two exceptions that can occur while using the SDK. The one that can occur most often is the [OneDriveException](http://tawalaya.github.io/OneDriveJavaSDK/docs/de/tuberlin/onedrivesdk/OneDriveException.html). This exception will be thrown in most cases if the API refused a command send by the SDK. Please look for solutions on the developer side of [Microsoft](https://dev.onedrive.com/) for explanations. The other exception that can occur is the
[OneDriveAuthenticationException](http://tawalaya.github.io/OneDriveJavaSDK/docs/de/tuberlin/onedrivesdk/networking/OneDriveAuthenticationException.html). This exception will be thrown if the session that the SDK is using is no longer valid. Ether use the `sdk.authenticateWithRefreshToken(sdk.getRefreshToken())` or make sure to enable the [automatic refresh](#automatic-refresh)

## Additional Information
## References

### Additional Information
* for more information about availible classes and methods visit the [javadoc pages](http://tawalaya.github.io/OneDriveJavaSDK/docs/)
* for more information about the underling service visit the [dev.onedrive.com](https://dev.onedrive.com/README.htm) site.

## History
### History
This project was developed at TU Berlin during the [Cloud Prototyping](http://www.ise.tu-berlin.de/menue/teaching/summer_term_2015/cloud_prototyping/parameter/en/) course in the summer term of 2015 from [Tim Hinkes](https://github.com/Timmeey), [Andreas Salzmann](https://github.com/andi3) and [Sebastian Werner](https://github.com/tawalaya) under the supervision of [Markus Klems](http://www.ise.tu-berlin.de/menue/team/markus_klems_dipl-wi-ing/parameter/en/).

## TODO

* log4j
* ~~log4j~~
40 changes: 17 additions & 23 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>de.tuberlin</groupId>
<artifactId>onedrivesdk</artifactId>
<version>0.0.9v</version>
<version>0.0.10v</version>

<scm>
<url>https://github.com/umjammer/OneDriveJavaSDK</url>
Expand All @@ -18,38 +18,44 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<version>3.12.1</version>
<configuration>
<release>17</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.2</version>
<version>3.2.5</version>
<configuration>
<trimStackTrace>false</trimStackTrace>
<argLine>
-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager
-Djava.util.logging.config.file=${project.build.testOutputDirectory}/logging.properties
--add-opens java.base/java.net=ALL-UNNAMED
--add-opens java.base/java.io=ALL-UNNAMED
--add-opens java.base/java.lang=ALL-UNNAMED
--add-opens java.base/java.lang.ref=ALL-UNNAMED
--add-opens java.base/jdk.internal.ref=ALL-UNNAMED
--add-opens java.logging/java.util.logging=ALL-UNNAMED
--add-opens java.base/java.util.concurrent.locks=ALL-UNNAMED
</argLine>
</configuration>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.10.1</version>
<version>5.14.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand All @@ -60,7 +66,7 @@
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>32.0.0-jre</version>
<version>33.5.0-jre</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
Expand All @@ -81,13 +87,7 @@
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.20.0</version>
<version>2.13.2</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -121,15 +121,9 @@
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jul</artifactId>
<version>2.17.1</version>
<groupId>com.github.umjammer</groupId>
<artifactId>vavi-commons</artifactId>
<version>1.1.16</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
Expand All @@ -15,8 +17,6 @@
import java.util.concurrent.TimeUnit;

import com.google.gson.GsonBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
Expand All @@ -43,13 +43,16 @@
import okhttp3.RequestBody;
import okhttp3.Response;

import static java.lang.System.getLogger;


/**
* This class provides the functionality to authenticate to OneDrive and handles the communication.
*/
public class ConcreteOneDriveSDK implements OneDriveSDK {

private static final Logger logger = LogManager.getLogger(OneDriveSession.class);
private static final Logger logger = getLogger(OneDriveSession.class.getName());

public static final Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();

private String baseUrl = "https://api.onedrive.com/v1.0/";
Expand Down Expand Up @@ -368,7 +371,7 @@ public OneResponse makeRequest(PreparedRequest preparedRequest) throws IOExcepti
RequestBody body = null;

if (preparedRequest.getBody() != null) {
body = RequestBody.create(null, preparedRequest.getBody());
body = RequestBody.create(preparedRequest.getBody(), null);
}

if (isCompleteURL(preparedRequest.getPath())) {
Expand All @@ -377,8 +380,8 @@ public OneResponse makeRequest(PreparedRequest preparedRequest) throws IOExcepti
url = String.format("%s%s?access_token=%s", this.baseUrl, preparedRequest.getPath(), session.getAccessToken());
}

//logger.debug(String.format("making request to %s",url));
//logger.debug(String.format("making request to %s",url), new Exception("*** DUMMY ***"));
//logger.log(Level.DEBUG, String.format("making request to %s",url));
//logger.log(Level.DEBUG, String.format("making request to %s",url), new Exception("*** DUMMY ***"));

Request.Builder builder = new Request.Builder().method(preparedRequest.getMethod(), body).url(url);

Expand Down Expand Up @@ -625,8 +628,8 @@ public Subscription subscribe(String notificationUrl, String clientState) throws
PreparedRequest request = new PreparedRequest(url, PreparedRequestMethod.POST);
request.addHeader("Content-Type", "application/json");
request.setBody(json.getBytes());
logger.info(url);
logger.info(json);
logger.log(Level.INFO, url);
logger.log(Level.INFO, json);

OneResponse response = this.makeRequest(request);
if (response.getStatusCode() != 201) {
Expand All @@ -652,8 +655,8 @@ public Subscription updateSubscription(String subscriptionId) throws IOException
PreparedRequest request = new PreparedRequest(url, PreparedRequestMethod.PATCH);
request.addHeader("Content-Type", "application/json");
request.setBody(json.getBytes());
logger.info(url);
logger.info(json);
logger.log(Level.INFO, url);
logger.log(Level.INFO, json);

OneResponse response = this.makeRequest(request);
if (response.getStatusCode() != 200) {
Expand All @@ -672,7 +675,7 @@ public Subscription updateSubscription(String subscriptionId) throws IOException
public void deleteSubscription(String subscriptionId) throws IOException {
String url = String.format("drive/root/subscriptions/%s", subscriptionId);

logger.info(url);
logger.log(Level.INFO, url);
PreparedRequest request = new PreparedRequest(url, PreparedRequestMethod.DELETE);

OneResponse response = this.makeRequest(request);
Expand Down
24 changes: 15 additions & 9 deletions src/main/java/de/tuberlin/onedrivesdk/common/OneItem.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package de.tuberlin.onedrivesdk.common;

import java.io.IOException;
import java.lang.System.Logger;
import java.lang.System.Logger.Level;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
Expand All @@ -10,8 +14,6 @@
import java.util.Map;

import com.google.gson.annotations.Expose;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
Expand All @@ -28,14 +30,15 @@
import de.tuberlin.onedrivesdk.folder.OneFolder;

import static de.tuberlin.onedrivesdk.common.ConcreteOneDriveSDK.gson;
import static java.lang.System.getLogger;


/**
* The root class of all files and folder types that can be accessed through this sdk.
*/
public abstract class OneItem {

private static final Logger logger = LogManager.getLogger(OneItem.class);
private static final Logger logger = getLogger(OneItem.class.getName());

/**
* The SDK object.
Expand Down Expand Up @@ -299,11 +302,15 @@ public Map<String, DriveUser> getCreatedBy() {
*/
public long getCreatedDateTime() {
try {
if (createdDateTime != null) {
return LocalDateTime.parse(createdDateTime.replaceFirst("Z$", "")).toEpochSecond(ZoneOffset.UTC);
if (createdDateTime != null && !createdDateTime.isEmpty()) {
logger.log(Level.TRACE, "createdDateTime: " + createdDateTime + ", " + createdDateTime.length());
if (createdDateTime.length() <= 11)
return LocalDate.parse(createdDateTime.replaceFirst("[ZT]$", "")).toEpochSecond(LocalTime.MIN, ZoneOffset.UTC);
else
return LocalDateTime.parse(createdDateTime.replaceFirst("[ZT]$", "")).toEpochSecond(ZoneOffset.UTC);
}
} catch (DateTimeParseException e) {
logger.warn(e.getMessage() + " " + createdDateTime);
logger.log(Level.WARNING, e.getMessage() + " " + createdDateTime, e);
}
return 0;
}
Expand All @@ -324,11 +331,11 @@ public Map<String, DriveUser> getLastModifiedBy() {
*/
public long getLastModifiedDateTime() {
try {
if (lastModifiedDateTime != null) {
if (lastModifiedDateTime != null && !lastModifiedDateTime.isEmpty()) {
return LocalDateTime.parse(lastModifiedDateTime.replaceFirst("Z$", "")).toEpochSecond(ZoneOffset.UTC);
}
} catch (DateTimeParseException e) {
logger.warn(e.getMessage() + " " + lastModifiedDateTime);
logger.log(Level.WARNING, e.getMessage() + " " + lastModifiedDateTime);
}
return 0;
}
Expand Down Expand Up @@ -431,7 +438,6 @@ public OneItem refreshItem() throws IOException {
*/
public abstract boolean isFolder();


/**
* Rename this file in the target folder.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,3 @@ public void delete() throws IOException, ParseException, InterruptedException {
api.deleteSubscription(id);
}
}

/* */
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,3 @@ class SubscriptionUpdateRequest {
@Expose
protected String expirationDateTime = SubscriptionRequest.getExpireTime();
}

/* */
Loading
Loading