Skip to content
This repository was archived by the owner on Mar 3, 2023. It is now read-only.

Commit e63f35d

Browse files
committed
initial commit
0 parents  commit e63f35d

24 files changed

+474
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
*.iml
3+
tmp/**/*

Procfile

Whitespace-only changes.

app/controllers/Mapper.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package controllers;
2+
3+
import play.Logger;
4+
import play.Play;
5+
import play.cache.Cache;
6+
import play.modules.spring.Spring;
7+
import play.mvc.Controller;
8+
import service.api.Counter;
9+
10+
public class Mapper extends Controller {
11+
12+
private static final int INSTANCE_ENCODING_RADIX = 32;
13+
private static final String KEY_PREFIX = "url-";
14+
15+
public static void index() {
16+
// render
17+
render();
18+
}
19+
20+
public static void use(final String id) {
21+
Logger.info(String.format("Received id: [%s]", id));
22+
final Long count = Long.valueOf(id, INSTANCE_ENCODING_RADIX);
23+
final String key = String.format("%s%s", KEY_PREFIX, count);
24+
final String url = (String) Cache.get(key);
25+
if (url == null) {
26+
flash.error(String.format("No url mapping found for index: [%s]", id));
27+
index();
28+
}
29+
redirect(url);
30+
}
31+
32+
public static void captureUrl(final String url) {
33+
final Counter counter = (Counter) Spring.getBean("counter");
34+
final long count = counter.next();
35+
36+
// set the mapping in cache
37+
Cache.set(String.format("%s%s", KEY_PREFIX, count), url);
38+
39+
// create short url
40+
// TODO: read the base url from configuration once DNS is in place
41+
// final String baseUrl = (String) Play.configuration.get("application.base.url");
42+
final String baseUrl = request.getBase();
43+
final String shortened = String.format("%s/%s",
44+
baseUrl,
45+
Long.toString(count, INSTANCE_ENCODING_RADIX));
46+
47+
render(shortened);
48+
}
49+
}

app/service/api/Counter.java

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package service.api;
2+
3+
public interface Counter {
4+
5+
public long next();
6+
}

app/service/impl/CounterImpl.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package service.impl;
2+
3+
import play.cache.Cache;
4+
import service.api.Counter;
5+
6+
import org.springframework.stereotype.Service;
7+
8+
@Service("counter")
9+
public class CounterImpl implements Counter {
10+
private static final long initial = 10000000L; // (ten million)
11+
private static final String COUNTER_KEY = "play:counter";
12+
13+
public long next() {
14+
Long current = (Long) Cache.get(COUNTER_KEY);
15+
if (current == null) {
16+
current = initial;
17+
} else {
18+
current++;
19+
}
20+
Cache.set(COUNTER_KEY, current);
21+
22+
return current;
23+
}
24+
}

app/views/Mapper/captureUrl.html

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#{extends 'main.html' /}
2+
#{set title:'Home' /}
3+
4+
<h1>smal.ly url: ${shortened ?: 'NONE'}</h1>
5+
6+
<a href="@{Mapper.index()}">Back to form</a>

app/views/Mapper/index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#{extends 'main.html' /}
2+
#{set title:'Home' /}
3+
4+
#{if flash.error}
5+
<p style="color:#c00">
6+
${flash.error}
7+
</p>
8+
#{/if}
9+
10+
<form action="@{Mapper.captureUrl()}" method="GET">
11+
<input maxlength="2000" size="100" type="text" name="url" />
12+
<input type="submit" value="Shorten Url" />
13+
</form>

app/views/errors/404.html

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
3+
<html>
4+
<head>
5+
<title>Not found</title>
6+
<meta http-equiv="Content-Type" content="text/html; charset=${_response_encoding}"/>
7+
</head>
8+
<body>
9+
#{if play.mode.name() == 'DEV'}
10+
#{404 result /}
11+
#{/if}
12+
#{else}
13+
<h1>Not found</h1>
14+
<p>
15+
${result.message}
16+
</p>
17+
#{/else}
18+
</body>
19+
</html>

app/views/errors/500.html

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
3+
<html>
4+
<head>
5+
<title>Application error</title>
6+
<meta http-equiv="Content-Type" content="text/html; charset=${_response_encoding}"/>
7+
</head>
8+
<body>
9+
#{if play.mode.name() == 'DEV'}
10+
#{500 exception /}
11+
#{/if}
12+
#{else}
13+
<h1>Oops, an error occured</h1>
14+
#{if exception instanceof play.exceptions.PlayException}
15+
<p>
16+
This exception has been logged with id <strong>${exception.id}</strong>.
17+
</p>
18+
#{/if}
19+
#{/else}
20+
</body>
21+
</html>

app/views/main.html

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
3+
<html>
4+
<head>
5+
<title>#{get 'title' /}</title>
6+
<meta charset="${_response_encoding}">
7+
<link rel="stylesheet" media="screen" href="@{'/public/stylesheets/main.css'}">
8+
#{get 'moreStyles' /}
9+
<link rel="shortcut icon" type="image/png" href="@{'/public/images/favicon.png'}">
10+
<script src="@{'/public/javascripts/jquery-1.5.2.min.js'}" type="text/javascript" charset="${_response_encoding}"></script>
11+
#{get 'moreScripts' /}
12+
</head>
13+
<body>
14+
#{doLayout /}
15+
</body>
16+
</html>

conf/application-context.xml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans
6+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
7+
http://www.springframework.org/schema/context
8+
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
9+
</beans>

0 commit comments

Comments
 (0)