Last active
August 29, 2015 14:19
-
-
Save kaiinui/5c034fdfed9a633dbaf4 to your computer and use it in GitHub Desktop.
おれおれ Slim3 BaseController
This file contains hidden or 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
import com.google.appengine.repackaged.org.apache.commons.codec.binary.Base64; | |
import com.google.appengine.repackaged.org.apache.commons.codec.binary.StringUtils; | |
import com.google.gson.Gson; | |
import org.slim3.controller.Controller; | |
import org.slim3.controller.Navigation; | |
import java.io.IOException; | |
abstract public class BaseController extends Controller { | |
// Working with JSON | |
static final String APPLICATION_JSON = "application/json"; | |
protected Navigation json(Object object) throws IOException { | |
writeAsJson(object); | |
return null; | |
} | |
protected void writeAsJson(Object object) throws IOException { | |
response.setContentType(APPLICATION_JSON); | |
response.getWriter().write(getGson().toJson(object)); | |
} | |
// Should override if you want to override GSON | |
protected Gson getGson() { | |
return new Gson(); | |
} | |
// Header Manipulation | |
protected void setCacheMaxAgeHeader(long maxAge) { | |
response.setHeader("Cache-Control", "max-age=" + maxAge + ", must-revalidate"); | |
} | |
protected void setVaryUserAgentHeader(boolean varyUserAgent) { | |
if (varyUserAgent) { | |
response.setHeader("Vary", "User-Agent"); | |
} | |
} | |
protected void setContentTypeHeader(String contentType) { | |
response.setContentType(contentType); | |
} | |
// User Agent Detection | |
protected boolean isMobile() { | |
// @see http://stackoverflow.com/questions/11381673/detecting-a-mobile-browser | |
final String userAgent = request.getHeader("User-Agent"); | |
if (userAgent.contains("Android")) { return true; } | |
if (userAgent.contains("webOS")) { return true; } | |
if (userAgent.contains("iPhone")) { return true; } | |
if (userAgent.contains("iPad")) { return true; } | |
if (userAgent.contains("iPod")) { return true; } | |
if (userAgent.contains("BlackBerry")) { return true; } | |
if (userAgent.contains("Windows Phone")) { return true; } | |
return false; | |
} | |
// Content Negotiation | |
protected String[] getPreferredLanguages() { | |
final String acceptLanguage = request.getHeader("Accept-Language"); | |
final String[] languages = acceptLanguage.split(","); | |
final String[] preferredLanguages = new String[languages.length]; | |
for (int i = 0; i < languages.length; i++) { | |
preferredLanguages[i] = languages[i].split(";")[0]; | |
} | |
return preferredLanguages; | |
} | |
// Basic Authentication | |
static final String HEADER_AUTHORIZATION = "Authorization"; | |
protected String getBasicAuthUsername() { | |
final String auth = request.getHeader(HEADER_AUTHORIZATION).substring(6); // - "Basic " | |
return decode(auth).split(":")[0]; | |
} | |
protected String getBasicAuthPassword() { | |
final String auth = request.getHeader(HEADER_AUTHORIZATION).substring(6); // - "Basic " | |
return decode(auth).split(":")[0]; | |
} | |
private static String decode(String s) { | |
return StringUtils.newStringUtf8(Base64.decodeBase64(s)); | |
} | |
// Template Attributes | |
protected void setAttribute(String key, Object object) { | |
request.setAttribute(key, object); | |
} | |
} |
This file contains hidden or 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
public class SampleController extends BaseController { | |
@Override | |
protected Navigation run() throws Exception { | |
final Book book = new Book("Alice's Adventures in Wonderland"); | |
return json(book); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment