-
Notifications
You must be signed in to change notification settings - Fork 0
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
9 changed files
with
237 additions
and
146 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,47 @@ | ||
/** | ||
* Copyright (c) 2016, cache-key-generator for fotbal-cz-api.cz | ||
* All rights reserved. | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: no conditions. | ||
*/ | ||
package cz.mikealdo.cache.key; | ||
|
||
import java.nio.charset.Charset; | ||
|
||
/** | ||
* Interface with common constants used for encoding/decoding keys. | ||
* | ||
* @author Michal Davidek (mdavidek1@gmail.com) | ||
* @version $Id$ | ||
* @since 1.0 | ||
*/ | ||
public interface CacheKey { | ||
|
||
/** | ||
* Default charset for given encoded keys. | ||
*/ | ||
Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); | ||
/** | ||
* Desired date format of string passed for key generation. | ||
*/ | ||
String DEFAULT_DATE_FMT = "YYYYMMdHmm"; | ||
/** | ||
* Delimiter of values used for key generation. | ||
*/ | ||
String DELIMITER = "##"; | ||
|
||
/** | ||
* Returns current encoded key. | ||
* | ||
* @return Encoded key. | ||
*/ | ||
String getKey(); | ||
|
||
/** | ||
* Returns current key description. | ||
* | ||
* @return Current key description. | ||
*/ | ||
KeyDescription getDescription(); | ||
} |
108 changes: 0 additions & 108 deletions
108
src/main/java/cz/mikealdo/cache/key/CacheKeyGenerator.java
This file was deleted.
Oops, something went wrong.
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,65 @@ | ||
/** | ||
* Copyright (c) 2016, cache-key-generator for fotbal-cz-api.cz | ||
* All rights reserved. | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: no conditions. | ||
*/ | ||
package cz.mikealdo.cache.key; | ||
|
||
import org.joda.time.DateTime; | ||
import org.joda.time.format.DateTimeFormat; | ||
import org.joda.time.format.DateTimeFormatter; | ||
import org.springframework.security.crypto.codec.Base64; | ||
|
||
/** | ||
* Return key description from given encoded hash. | ||
* | ||
* @author Michal Davidek (mdavidek1@gmail.com) | ||
* @version $Id$ | ||
* @since 1.0 | ||
*/ | ||
public final class DecodedKey implements CacheKey { | ||
|
||
/** | ||
* Formatter for parsing date used by decoding generated key. | ||
*/ | ||
private final DateTimeFormatter formatter; | ||
|
||
/** | ||
* Description of the key. | ||
*/ | ||
private final String key; | ||
|
||
/** | ||
* Constructor with sensible defaults for date format. | ||
* | ||
* @param key Given encoded key. | ||
*/ | ||
public DecodedKey(final String key) { | ||
this.formatter = DateTimeFormat.forPattern(CacheKey.DEFAULT_DATE_FMT); | ||
this.key = key; | ||
} | ||
|
||
@Override | ||
public KeyDescription getDescription() { | ||
final byte[] bytes = Base64.decode( | ||
this.key.getBytes(DecodedKey.DEFAULT_CHARSET) | ||
); | ||
final String decoded = new String( | ||
bytes, | ||
CacheKey.DEFAULT_CHARSET | ||
); | ||
final String[] split = decoded.split(CacheKey.DELIMITER); | ||
return | ||
new KeyDescription( | ||
DateTime.parse(split[0], this.formatter), | ||
split[1] | ||
); | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return this.key; | ||
} | ||
} |
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 @@ | ||
/** | ||
* Copyright (c) 2016, cache-key-generator for fotbal-cz-api.cz | ||
* All rights reserved. | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: no conditions. | ||
*/ | ||
package cz.mikealdo.cache.key; | ||
|
||
import org.springframework.security.crypto.codec.Base64; | ||
|
||
/** | ||
* Make a hash from given key description. | ||
* | ||
* @author Michal Davidek (mdavidek1@gmail.com) | ||
* @version $Id$ | ||
* @since 1.0 | ||
*/ | ||
public final class EncodedKey implements CacheKey { | ||
|
||
/** | ||
* Format of date provided in cache key descriptor. | ||
*/ | ||
private final String format; | ||
|
||
/** | ||
* Description of the key. | ||
*/ | ||
private final KeyDescription description; | ||
|
||
/** | ||
* Constructor with sensible defaults for date format. | ||
* | ||
* @param description Given key description. | ||
*/ | ||
public EncodedKey(final KeyDescription description) { | ||
this.format = CacheKey.DEFAULT_DATE_FMT; | ||
this.description = description; | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
final StringBuilder hash = new StringBuilder(); | ||
hash | ||
.append(this.description.getTime().toString(this.format)) | ||
.append(CacheKey.DELIMITER) | ||
.append(this.description.getHash()); | ||
return | ||
new String( | ||
Base64.encode( | ||
hash.toString().getBytes(CacheKey.DEFAULT_CHARSET) | ||
), | ||
CacheKey.DEFAULT_CHARSET | ||
); | ||
} | ||
|
||
@Override | ||
public KeyDescription getDescription() { | ||
return this.description; | ||
} | ||
} |
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
33 changes: 0 additions & 33 deletions
33
src/test/java/cz/mikealdo/cache/key/CacheKeyGeneratorTest.java
This file was deleted.
Oops, something went wrong.
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,23 @@ | ||
package cz.mikealdo.cache.key; | ||
|
||
import org.assertj.core.api.Assertions; | ||
import org.junit.Test; | ||
|
||
public class DecodedKeyTest { | ||
|
||
private static final String ENCODED_KEY = "MjAxNjEwMjgxNTMxIyMzNDUyMzQ1LTI0NS0yNDM1MjM0NTIzNDU="; | ||
private static final String INPUT_HASH = "3452345-245-243523452345"; | ||
|
||
|
||
@Test | ||
public void shouldDecodeCacheKey() throws Exception { | ||
final KeyDescription description = | ||
new DecodedKey( | ||
DecodedKeyTest.ENCODED_KEY | ||
).getDescription(); | ||
|
||
Assertions.assertThat(description.getHash()).isEqualTo | ||
(DecodedKeyTest | ||
.INPUT_HASH); | ||
} | ||
} |
Oops, something went wrong.