Github user venkat1m commented on a diff in the pull request:
https://github.com/apache/incubator-trafodion/pull/911#discussion_r95642534
--- Diff: core/rest/src/main/java/org/trafodion/rest/TransactionsResource.java ---
@@ -57,176 +57,197 @@
import org.trafodion.rest.zookeeper.ZkClient;
import org.trafodion.rest.RestConstants;
+import javax.ws.rs.HeaderParam;
+import javax.ws.rs.core.HttpHeaders;
+
public class TransactionsResource extends ResourceBase {
- private static final Log LOG =
- LogFactory.getLog(TransactionsResource.class);
-
- static CacheControl cacheControl;
- static {
- cacheControl = new CacheControl();
- cacheControl.setNoCache(true);
- cacheControl.setNoTransform(false);
- }
-
-
- public TransactionsResource() throws IOException {
- super();
- }
-
- private String stats() throws IOException {
- ScriptContext scriptContext = new ScriptContext();
- scriptContext.setScriptName(Constants.SYS_SHELL_SCRIPT_NAME);
- scriptContext.setCommand("dtmci stats -j");
-
- try {
- ScriptManager.getInstance().runScript(scriptContext);//This will block while
script is running
- } catch (Exception e) {
- e.printStackTrace();
- throw new IOException(e);
- }
-
- if(LOG.isDebugEnabled()) {
+ private static final Log LOG = LogFactory.getLog(TransactionsResource.class);
+
+ static CacheControl cacheControl;
+ static {
+ cacheControl = new CacheControl();
+ cacheControl.setNoCache(true);
+ cacheControl.setNoTransform(false);
+ }
+
+ public TransactionsResource() throws IOException {
+ super();
+ }
+
+ private String stats() throws IOException {
+ ScriptContext scriptContext = new ScriptContext();
+ scriptContext.setScriptName(Constants.SYS_SHELL_SCRIPT_NAME);
+ scriptContext.setCommand("dtmci stats -j");
+
+ try {
+ ScriptManager.getInstance().runScript(scriptContext);// This will
+ // block
+ // while
+ // script is
+ // running
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new IOException(e);
+ }
+
+ if (LOG.isDebugEnabled()) {
StringBuilder sb = new StringBuilder();
sb.append("exit code [" + scriptContext.getExitCode() + "]");
- if(! scriptContext.getStdOut().toString().isEmpty())
+ if (!scriptContext.getStdOut().toString().isEmpty())
sb.append(", stdout [" + scriptContext.getStdOut().toString() + "]");
- if(! scriptContext.getStdErr().toString().isEmpty())
+ if (!scriptContext.getStdErr().toString().isEmpty())
sb.append(", stderr [" + scriptContext.getStdErr().toString() + "]");
LOG.debug(sb.toString());
}
return scriptContext.getStdOut().toString();
- }
-
- private String tm() throws IOException {
- ScriptContext scriptContext = new ScriptContext();
- scriptContext.setScriptName(Constants.SYS_SHELL_SCRIPT_NAME);
- scriptContext.setCommand("dtmci status tm -j");
-
- try {
- ScriptManager.getInstance().runScript(scriptContext);//This will block while
script is running
- } catch (Exception e) {
- e.printStackTrace();
- throw new IOException(e);
- }
-
- if(LOG.isDebugEnabled()) {
- StringBuilder sb = new StringBuilder();
- sb.append("exit code [" + scriptContext.getExitCode() + "]");
- if(! scriptContext.getStdOut().toString().isEmpty())
- sb.append(", stdout [" + scriptContext.getStdOut().toString() + "]");
- if(! scriptContext.getStdErr().toString().isEmpty())
- sb.append(", stderr [" + scriptContext.getStdErr().toString() + "]");
- LOG.debug(sb.toString());
- }
-
- return scriptContext.getStdOut().toString();
- }
-
- @GET
- @Produces({MIMETYPE_JSON})
- public Response getAll(final @Context UriInfo uriInfo,@Context Request request) {
- try {
- if (LOG.isDebugEnabled()) {
- LOG.debug("GET " + uriInfo.getAbsolutePath());
-
- MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
- String output = " Query Parameters :\n";
- for (String key : queryParams.keySet()) {
- output += key + " : " + queryParams.getFirst(key) +"\n";
- }
- LOG.debug(output);
-
- MultivaluedMap<String, String> pathParams = uriInfo.getPathParameters();
- output = " Path Parameters :\n";
- for (String key : pathParams.keySet()) {
- output += key + " : " + pathParams.getFirst(key) +"\n";
- }
- LOG.debug(output);
- }
-
- String result = tm();
-
- ResponseBuilder response = Response.ok(result);
- response.cacheControl(cacheControl);
- return response.build();
- } catch (IOException e) {
- e.printStackTrace();
- return Response.status(Response.Status.SERVICE_UNAVAILABLE)
- .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF)
- .build();
- }
- }
-
- @GET
+ }
+
+ private String tm() throws IOException {
+ ScriptContext scriptContext = new ScriptContext();
+ scriptContext.setScriptName(Constants.SYS_SHELL_SCRIPT_NAME);
+ scriptContext.setCommand("dtmci status tm -j");
+
+ try {
+ ScriptManager.getInstance().runScript(scriptContext);// This will
+ // block
+ // while
+ // script is
+ // running
+ } catch (Exception e) {
+ e.printStackTrace();
+ throw new IOException(e);
+ }
+
+ if (LOG.isDebugEnabled()) {
+ StringBuilder sb = new StringBuilder();
+ sb.append("exit code [" + scriptContext.getExitCode() + "]");
+ if (!scriptContext.getStdOut().toString().isEmpty())
+ sb.append(", stdout [" + scriptContext.getStdOut().toString() + "]");
+ if (!scriptContext.getStdErr().toString().isEmpty())
+ sb.append(", stderr [" + scriptContext.getStdErr().toString() + "]");
+ LOG.debug(sb.toString());
+ }
+
+ return scriptContext.getStdOut().toString();
+ }
+
+ @GET
+ @Produces({ MIMETYPE_JSON })
+ public Response getAll(final @Context UriInfo uriInfo, @Context Request request,
+ @HeaderParam(HttpHeaders.AUTHORIZATION) final String auth) {
+ try {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("GET " + uriInfo.getAbsolutePath());
+
+ MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
+ String output = " Query Parameters :\n";
+ for (String key : queryParams.keySet()) {
+ output += key + " : " + queryParams.getFirst(key) + "\n";
+ }
+ LOG.debug(output);
+
+ MultivaluedMap<String, String> pathParams = uriInfo.getPathParameters();
+ output = " Path Parameters :\n";
+ for (String key : pathParams.keySet()) {
+ output += key + " : " + pathParams.getFirst(key) + "\n";
+ }
+ LOG.debug(output);
+ }
+
+ Response rs = TokenTool.getResponse(auth);
--- End diff --
Instead of having to call TokenTool.getResponse(auth) on every REST method, it would be
better to implement a servlet filter (javax.servlet.Filter) and have this done in one place.
Plus it will guarantee that no REST request can come through without passing the authentication
enforced by the filter.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---
|