Skip to content

Commit

Permalink
Moving helper methods used in single places away from RestResource.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisoelkers committed Mar 23, 2015
1 parent f38c1eb commit c52d0ed
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 38 deletions.
Expand Up @@ -77,4 +77,8 @@ public Map<String, Object> all(@ApiParam(name = "page", value = "Page", required
"messages", messages, "messages", messages,
"total", systemMessageService.totalCount()); "total", systemMessageService.totalCount());
} }

private int page(int page) {
return Math.max(0, page - 1);
}
} }
Expand Up @@ -83,10 +83,6 @@ public ObjectWriter modify(EndpointConfigBase<?> endpoint, MultivaluedMap<String
} }
} }


protected int page(int page) {
return Math.max(0, page - 1);
}

protected Subject getSubject() { protected Subject getSubject() {
if (securityContext == null) { if (securityContext == null) {
LOG.error("Cannot retrieve current subject, SecurityContext isn't set."); LOG.error("Cannot retrieve current subject, SecurityContext isn't set.");
Expand Down Expand Up @@ -123,40 +119,6 @@ protected void checkPermission(String permission, String instanceId) {
} }
} }


protected Map<String, Long> bytesToValueMap(long bytes) {
final Size size = Size.bytes(bytes);
return ImmutableMap.of(
"bytes", size.toBytes(),
"kilobytes", size.toKilobytes(),
"megabytes", size.toMegabytes());
}

protected String guessContentType(final String filename) {
// A really dumb but for us good enough approach. We only need this for a very few static files we control.

if (filename.endsWith(".png")) {
return "image/png";
}

if (filename.endsWith(".gif")) {
return "image/gif";
}

if (filename.endsWith(".css")) {
return "text/css";
}

if (filename.endsWith(".js")) {
return "application/javascript";
}

if (filename.endsWith(".html")) {
return MediaType.TEXT_HTML;
}

return MediaType.TEXT_PLAIN;
}

protected void restrictToMaster() { protected void restrictToMaster() {
if (!serverStatus.hasCapability(ServerStatus.Capability.MASTER)) { if (!serverStatus.hasCapability(ServerStatus.Capability.MASTER)) {
LOG.warn("Rejected request that is only allowed against master nodes. Returning HTTP 403."); LOG.warn("Rejected request that is only allowed against master nodes. Returning HTTP 403.");
Expand Down
Expand Up @@ -24,6 +24,7 @@
import javax.ws.rs.NotFoundException; import javax.ws.rs.NotFoundException;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
Expand Down Expand Up @@ -65,4 +66,30 @@ public Response asset(@PathParam("route") String route) {
throw new NotFoundException(); throw new NotFoundException();
} }
} }

private String guessContentType(final String filename) {
// A really dumb but for us good enough approach. We only need this for a very few static files we control.

if (filename.endsWith(".png")) {
return "image/png";
}

if (filename.endsWith(".gif")) {
return "image/gif";
}

if (filename.endsWith(".css")) {
return "text/css";
}

if (filename.endsWith(".js")) {
return "application/javascript";
}

if (filename.endsWith(".html")) {
return MediaType.TEXT_HTML;
}

return MediaType.TEXT_PLAIN;
}
} }
Expand Up @@ -18,6 +18,8 @@


import com.codahale.metrics.annotation.Timed; import com.codahale.metrics.annotation.Timed;
import com.codahale.metrics.jvm.ThreadDump; import com.codahale.metrics.jvm.ThreadDump;
import com.github.joschi.jadconfig.util.Size;
import com.google.common.collect.ImmutableMap;
import com.wordnik.swagger.annotations.Api; import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation; import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication; import org.apache.shiro.authz.annotation.RequiresAuthentication;
Expand All @@ -36,6 +38,7 @@
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Map;


import static javax.ws.rs.core.MediaType.APPLICATION_JSON; import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
import static javax.ws.rs.core.MediaType.TEXT_PLAIN; import static javax.ws.rs.core.MediaType.TEXT_PLAIN;
Expand Down Expand Up @@ -110,4 +113,12 @@ public String threaddump() {
threadDump.dump(output); threadDump.dump(output);
return new String(output.toByteArray(), StandardCharsets.UTF_8); return new String(output.toByteArray(), StandardCharsets.UTF_8);
} }

private Map<String, Long> bytesToValueMap(long bytes) {
final Size size = Size.bytes(bytes);
return ImmutableMap.of(
"bytes", size.toBytes(),
"kilobytes", size.toKilobytes(),
"megabytes", size.toMegabytes());
}
} }

0 comments on commit c52d0ed

Please sign in to comment.