-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
64 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.mangopay.core; | ||
|
||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
/** | ||
* File token storage strategy implementation. | ||
*/ | ||
public class FileStorageStrategy implements IStorageStrategy { | ||
|
||
private String _tempDir = null; | ||
|
||
public FileStorageStrategy(String tempDir) { | ||
_tempDir = tempDir; | ||
} | ||
|
||
/** | ||
* Gets the currently stored token. | ||
* @return Currently stored token instance or null. | ||
*/ | ||
@Override | ||
public OAuthToken get() { | ||
try | ||
{ | ||
FileInputStream fileIn = new FileInputStream(getFilePath()); | ||
ObjectInputStream in = new ObjectInputStream(fileIn); | ||
OAuthToken token = (OAuthToken) in.readObject(); | ||
in.close(); | ||
fileIn.close(); | ||
return token; | ||
} catch (Exception ex) | ||
{ | ||
return null; // it's not an error: e.g. file not found cause not stored yet | ||
} | ||
} | ||
|
||
/** | ||
* Stores authorization token passed as an argument. | ||
* @param token Token instance to be stored. | ||
*/ | ||
@Override | ||
public void store(OAuthToken token) { | ||
FileOutputStream fileOut; | ||
try { | ||
fileOut = new FileOutputStream(getFilePath()); | ||
ObjectOutputStream out = new ObjectOutputStream(fileOut); | ||
out.writeObject(token); | ||
out.close(); | ||
fileOut.close(); | ||
} catch (Exception ex) { | ||
Logger.getLogger(FileStorageStrategy.class.getName()).log(Level.SEVERE, null, ex); | ||
} | ||
} | ||
|
||
private String getFilePath() { return _tempDir + getClass().getName() + ".tmp"; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters