Skip to content

Commit

Permalink
Improved architecture
Browse files Browse the repository at this point in the history
  • Loading branch information
mikealdo committed Dec 15, 2016
1 parent e3f8f89 commit ea01eb9
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 146 deletions.
11 changes: 9 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.5.2</version>
<version>3.6.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -44,6 +44,13 @@
</dependency>
</dependencies>

<pluginRepositories>
<pluginRepository>
<id>oss.sonatype.org</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</pluginRepository>
</pluginRepositories>

<build>
<pluginManagement>
<plugins>
Expand Down Expand Up @@ -71,7 +78,7 @@
<plugin>
<groupId>com.qulice</groupId>
<artifactId>qulice-maven-plugin</artifactId>
<version>0.17.1</version>
<version>1.0-SNAPSHOT</version>
<configuration>
<license>
file:${basedir}/src/main/resources/LICENSE.txt
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/cz/mikealdo/cache/key/CacheKey.java
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 src/main/java/cz/mikealdo/cache/key/CacheKeyGenerator.java

This file was deleted.

65 changes: 65 additions & 0 deletions src/main/java/cz/mikealdo/cache/key/DecodedKey.java
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;
}
}
61 changes: 61 additions & 0 deletions src/main/java/cz/mikealdo/cache/key/EncodedKey.java
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
import org.joda.time.DateTime;

/**
* Descriptor used for cache key generator.
* Description used for generation of cache key.
*
* @author Michal Davidek (mdavidek1@gmail.com)
* @version $Id$
* @since 1.0
*/
public final class CacheKeyDescriptor {
public final class KeyDescription {
/**
* Time is used for checking if the cache should be invalidated or not.
*/
Expand All @@ -31,7 +31,7 @@ public final class CacheKeyDescriptor {
* @param time Current time
* @param hash String with hash under the value will be stored.
*/
public CacheKeyDescriptor(final DateTime time, final String hash) {
public KeyDescription(final DateTime time, final String hash) {
this.time = time;
this.hash = hash;
}
Expand Down
33 changes: 0 additions & 33 deletions src/test/java/cz/mikealdo/cache/key/CacheKeyGeneratorTest.java

This file was deleted.

23 changes: 23 additions & 0 deletions src/test/java/cz/mikealdo/cache/key/DecodedKeyTest.java
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);
}
}
Loading

0 comments on commit ea01eb9

Please sign in to comment.