Commit b9c21569 authored by Feng Xue's avatar Feng Xue

Improve the code quality

Fixes #2048
Refs to #2053
parent ef771342
#IDE's create lots of files that we don't want in git
.idea
.checkstyle
.pmd
.ruleset
.settings
.classpath
.project
target
tags
out/
reports/
*.iml
*.swp
config.yaml
......@@ -28,24 +28,25 @@ import com.yammer.dropwizard.config.Environment;
import com.yammer.dropwizard.config.FilterBuilder;
public final class NesstarDropService extends Service<NesstarDropConfiguration> {
private static final int SECONDSINONEDAY = 60 * 60 * 24;
public static void main(String[] args) throws Exception {
new NesstarDropService().run(args);
}
@Override
public void initialize(Bootstrap<NesstarDropConfiguration> bootstrap) {
public void initialize(final Bootstrap<NesstarDropConfiguration> bootstrap) {
bootstrap.setName("Nesstar-REST_API");
bootstrap.addBundle(new AssetsBundle("/assets/", "/"));
}
@Override
public void run(NesstarDropConfiguration configuration, Environment environment) {
public void run(final NesstarDropConfiguration configuration, final Environment environment) {
final String serverURI = configuration.getServerURI();
final String userId = configuration.getUserId();
final String password = configuration.getPassword();
environment.setSessionHandler(new SessionHandler());
ServerHandler serverHandler = new ServerHandler(serverURI, userId, password);
final ServerHandler serverHandler = new ServerHandler(serverURI, userId, password);
environment.addResource(new CorrelationResource(serverHandler));
environment.addResource(new RegressionResource(serverHandler));
......@@ -62,14 +63,14 @@ public final class NesstarDropService extends Service<NesstarDropConfiguration>
environment.addHealthCheck(new NesstarHealthCheck(serverHandler));
FilterBuilder filterConfig = environment.addFilter(CrossOriginFilter.class, "*");
filterConfig.setInitParam(CrossOriginFilter.PREFLIGHT_MAX_AGE_PARAM, String.valueOf(60 * 60 * 24)); // 1 day
final FilterBuilder filterConfig = environment.addFilter(CrossOriginFilter.class, "*");
filterConfig.setInitParam(CrossOriginFilter.PREFLIGHT_MAX_AGE_PARAM, String.valueOf(SECONDSINONEDAY)); // 1 day
filterConfig.setInitParam(CrossOriginFilter.ALLOWED_ORIGINS_PARAM, "*");
TabulationEntityTagFilter tabulationETagFilter = new TabulationEntityTagFilter(serverHandler);
final TabulationEntityTagFilter tabulationETagFilter = new TabulationEntityTagFilter(serverHandler);
environment.addFilter(tabulationETagFilter, "/study/*");
EntityTagFilter eTagFilter = new CommonEntityTagFilter(serverHandler);
final EntityTagFilter eTagFilter = new CommonEntityTagFilter(serverHandler);
environment.addFilter(eTagFilter, "/*");
environment.addFilter(LanguageFilter.class, "*");
......
......@@ -11,13 +11,13 @@ import java.util.jar.Manifest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Version {
private static String versionNumber;
public final class Version {
private static final Logger LOG = LoggerFactory.getLogger(Version.class);
private static Version instance = new Version();
private static String versionNumber = readVersionNumber();
private Version() {
LOG.debug("Initializing version");
versionNumber = readVersionNumber();
}
public static String version() {
......@@ -25,18 +25,18 @@ public class Version {
}
private static Version getInstance() {
return VersionHolder.INSTANCE;
return instance;
}
private String getVersion() {
return versionNumber;
}
private String readVersionNumber() {
private static String readVersionNumber() {
LOG.debug("Reading version number");
String version;
try {
Manifest manifest = getManifest();
final Manifest manifest = getManifest();
version = getVersionFromManifest(manifest);
} catch (IOException e) {
LOG.warn("Exception occurred while reading manifest: {}, {}", e.getMessage(), e.getCause());
......@@ -45,34 +45,32 @@ public class Version {
return version;
}
private Manifest getManifest() throws IOException {
private static Manifest getManifest() throws IOException {
LOG.debug("Getting manifest");
Enumeration manifestSources = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME);
final Enumeration manifestSources = Thread.currentThread().getContextClassLoader().getResources(JarFile.MANIFEST_NAME);
final Manifest manifest = new Manifest();
while (manifestSources.hasMoreElements()) {
URL url = (URL) manifestSources.nextElement();
InputStream stream = url.openStream();
if (stream != null) {
Manifest manifest = new Manifest(stream);
Attributes attributes = manifest.getMainAttributes();
String mainClassName = attributes.getValue("Main-Class");
if ("com.nesstar.rest.NesstarDropService".equals(mainClassName)) {
return manifest;
}
final URL url = (URL) manifestSources.nextElement();
final InputStream stream = url.openStream();
if (stream == null) {
continue;
}
manifest.read(stream);
final Attributes attributes = manifest.getMainAttributes();
final String mainClassName = attributes.getValue("Main-Class");
if ("com.nesstar.rest.NesstarDropService".equals(mainClassName)) {
return manifest;
}
}
return null;
}
private String getVersionFromManifest(Manifest mf) throws IOException {
private static String getVersionFromManifest(final Manifest manifest) throws IOException {
String version = null;
if (mf != null) {
Attributes attributes = mf.getMainAttributes();
if (manifest != null) {
final Attributes attributes = manifest.getMainAttributes();
version = attributes.getValue("Implementation-Version");
}
return version;
}
private static class VersionHolder {
private static final Version INSTANCE = new Version();
}
}
package com.nesstar.rest.common;
import com.nesstar.api.Server;
import com.nesstar.api.cube.Cube;
import com.nesstar.api.cube.Dimension;
import com.nesstar.api.cube.Measure;
import com.nesstar.api.cube.Member;
import java.util.List;
import java.util.Map;
/**
* Created by feng on 1/9/14.
*/
public class CubeClassVariables {
private Server server;
private Cube cube;
private List<Dimension> dimensions;
private List<Measure> measures;
private List<Map<String, Object>> values;
private List<Dimension> slicedimensions;
private List<Measure> slicemeasures;
private Map<Dimension, List<Member>> dimensionMemberList;
public Server getServer() {
return server;
}
public void setServer(Server server) {
this.server = server;
}
public Cube getCube() {
return cube;
}
public void setCube(Cube cube) {
this.cube = cube;
}
public List<Dimension> getDimensions() {
return dimensions;
}
public void setDimensions(List<Dimension> dimensions) {
this.dimensions = dimensions;
}
public List<Measure> getMeasures() {
return measures;
}
public void setMeasures(List<Measure> measures) {
this.measures = measures;
}
public List<Map<String, Object>> getValues() {
return values;
}
public void setValues(List<Map<String, Object>> values) {
this.values = values;
}
public List<Dimension> getSlicedimensions() {
return slicedimensions;
}
public void setSlicedimensions(List<Dimension> slicedimensions) {
this.slicedimensions = slicedimensions;
}
public List<Measure> getSlicemeasures() {
return slicemeasures;
}
public void setSlicemeasures(List<Measure> slicemeasures) {
this.slicemeasures = slicemeasures;
}
public Map<Dimension, List<Member>> getDimensionMemberList() {
return dimensionMemberList;
}
public void setDimensionMemberList(Map<Dimension, List<Member>> dimensionMemberList) {
this.dimensionMemberList = dimensionMemberList;
}
}
......@@ -12,7 +12,7 @@ public final class DdiUtils {
}
public static String getDdiString(DdiElement element) {
public static String getDdiString(final DdiElement element) {
if (element == null) {
return null;
}
......@@ -21,12 +21,12 @@ public final class DdiUtils {
}
}
public static List<DdiElement> getDdiList(DdiElement element) {
List<DdiElement> ddiElements = new ArrayList<DdiElement>();
public static List<DdiElement> getDdiList(final DdiElement element) {
final List<DdiElement> ddiElements = new ArrayList<DdiElement>();
if (element instanceof DdiList) {
DdiList<?> ddiList = (DdiList<?>) element;
for (Object o : ddiList) {
ddiElements.add((DdiElement) o);
final DdiList<?> ddiList = (DdiList<?>) element;
for (final Object object : ddiList) {
ddiElements.add((DdiElement) object);
}
}
else {
......
package com.nesstar.rest.common;
import com.nesstar.api.Category;
import com.nesstar.api.NesstarObject;
import com.nesstar.api.Variable;
import com.nesstar.api.analysis.Statistic;
import com.nesstar.api.cube.Cube;
import com.nesstar.rest.request_objects.CubeRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
public final class ETag {
public static final String HEADER_NAME = "ETag";
private String hash;
private final String hash;
private ETag(String tag) {
private ETag(final String tag) {
hash = tag;
}
......@@ -12,15 +22,16 @@ public final class ETag {
return hash;
}
public boolean equals(Object someObject) {
if (someObject instanceof ETag) {
String otherValue = ((ETag) someObject).getValue();
if (otherValue != null && !otherValue.isEmpty()) {
return otherValue.equalsIgnoreCase(getValue());
}
public boolean equals(final Object someObject) {
boolean result = false;
if (!(someObject instanceof ETag)) {
return result;
}
final String otherValue = ((ETag) someObject).getValue();
if (otherValue != null && !otherValue.isEmpty()) {
result = otherValue.equalsIgnoreCase(getValue());
}
return false;
return result;
}
@Override
......@@ -28,8 +39,49 @@ public final class ETag {
return hash.hashCode();
}
public static ETag createETag(ETagGenerator generator) {
String checksum = generator.generateETagFromData();
public static ETag createETag(final ETagGenerator generator) {
final String checksum = generator.generateETagFromData();
return new ETag(checksum);
}
public static ETag generateEtag(NesstarObject resource, final HttpServletRequest request) {
String url = request.getRequestURL().toString();
ETagData data = new ETagData();
data.add(url).add(resource);
return createETag(new ETagGenerator(data));
}
public static ETag generateEtagForTabulation(final TabulationClassVariables classVariables, final HttpServletRequest request) {
ETagData data = new ETagData();
String url = request.getRequestURL().toString();
data.add(url).add(classVariables.getStudy());
for (Variable variable : classVariables.getBreakVariables()) {
data.add(variable);
}
for (List<Category> categories : classVariables.getCategories()) {
data.add(categories);
}
data.add(classVariables.getMeasureVariable());
for (Statistic measureType : classVariables.getMeasureTypes()) {
data.add(measureType);
}
for (Variable weightVariable : classVariables.getWeightVariables()) {
data.add(weightVariable);
}
data.add(classVariables.getCaseSubset());
return ETag.createETag(new ETagGenerator(data));
}
public static ETag generateEtagForCube(final Cube cube, final CubeRequest cubeRequest, final HttpServletRequest request) {
ETagData data = new ETagData();
String url = request.getRequestURL().toString();
data.add(url).add(cube).add(cubeRequest);
return createETag(new ETagGenerator(data));
}
}
......@@ -15,28 +15,28 @@ import com.nesstar.api.NotAuthorizedException;
import com.nesstar.rest.request_objects.CubeRequest;
public class ETagData {
protected static final Date ZERODATE = new Date(0L);
private static final Logger LOG = LoggerFactory.getLogger(ETagData.class);
private final List<String> data;
protected static final Date zeroDate = new Date(0l);
private final static Logger LOG = LoggerFactory.getLogger(ETagData.class);
public ETagData() {
data = new LinkedList<String>();
}
public ETagData add(Object object) {
String objectAsString = convertObjectToString(object);
public ETagData add(final Object object) {
final String objectAsString = convertObjectToString(object);
data.add(objectAsString);
return this;
}
public ETagData addNesstarObject(NesstarObject object) {
public ETagData addNesstarObject(final NesstarObject object) {
data.add(getObjectId(object));
data.add(getTimestamp(object));
return this;
}
public ETagData addCubeRequest(CubeRequest request) {
public ETagData addCubeRequest(final CubeRequest request) {
addDimensionsFromCubeRequest(request);
addMeasuresFromCubeRequest(request);
......@@ -44,19 +44,19 @@ public class ETagData {
}
public String getString() {
StringBuilder sb = new StringBuilder();
for (String str : data) {
sb.append(str);
final StringBuilder stringBuilder = new StringBuilder();
for (final String str : data) {
stringBuilder.append(str);
}
return sb.toString();
return stringBuilder.toString();
}
private String convertObjectToString(Object object) {
private String convertObjectToString(final Object object) {
String result = null;
if (object instanceof String) {
result = (String) object;
} else if (object instanceof Date) {
Date date = (Date) object;
final Date date = (Date) object;
result = Integer.toString(date.hashCode());
} else if (object instanceof NesstarObject) {
addNesstarObject((NesstarObject) object);
......@@ -66,28 +66,28 @@ public class ETagData {
return result;
}
private String getObjectId(NesstarObject object) {
String id;
private String getObjectId(final NesstarObject object) {
String objectId;
if (object instanceof NesstarList) {
id = getIdsFromList((NesstarList) object);
objectId = getIdsFromList((NesstarList<NesstarObject>) object);
} else {
id = object.getId();
objectId = object.getId();
}
return id;
return objectId;
}
private String getIdsFromList(NesstarList list) {
StringBuilder sb = new StringBuilder();
for (Object object : list) {
sb.append(((NesstarObject) object).getId());
private String getIdsFromList(final NesstarList<NesstarObject> list) {
final StringBuilder stringBuilder = new StringBuilder();
for (final NesstarObject object : list) {
stringBuilder.append(object.getId());
}
return sb.toString();
return stringBuilder.toString();
}
private String getTimestamp(NesstarObject object) {
private String getTimestamp(final NesstarObject object) {
String timestamp = null;
try {
Date date = getTimestampFromObject(object);
final Date date = getTimestampFromObject(object);
timestamp = Integer.toString(date.hashCode());
} catch (NotAuthorizedException e) {
LOG.error("Not authorized");
......@@ -97,7 +97,7 @@ public class ETagData {
return timestamp;
}
protected Date getTimestampFromObject(NesstarObject object) throws NotAuthorizedException, IOException {
protected Date getTimestampFromObject(final NesstarObject object) throws NotAuthorizedException, IOException {
Date timestamp;
if (object instanceof NesstarList) {
timestamp = getLatestTimestampFromList((NesstarList<NesstarObject>) object);
......@@ -107,35 +107,36 @@ public class ETagData {
return timestamp;
}
protected Date getLatestTimestampFromList(NesstarList<NesstarObject> list) throws NotAuthorizedException, IOException {
Date latest = zeroDate;
for (NesstarObject object : list) {
if (object.getTimeStamp().after(latest)) {
protected Date getLatestTimestampFromList(final NesstarList<NesstarObject> list) throws NotAuthorizedException, IOException {
Date latest = ZERODATE;
for (final NesstarObject object : list) {
final Date date = object.getTimeStamp();
if (date.after(latest)) {
latest = object.getTimeStamp();
}
}
return latest;
}
private void addDimensionsFromCubeRequest(CubeRequest request) {
String[] dimensions = getDimensionIDs(request);
private void addDimensionsFromCubeRequest(final CubeRequest request) {
final String[] dimensions = getDimensionIDs(request);
for (String dimension : dimensions) {
for (final String dimension : dimensions) {
data.add(dimension);
}
}
private void addMeasuresFromCubeRequest(CubeRequest request) {
for (String measure : request.getMeasures()) {
private void addMeasuresFromCubeRequest(final CubeRequest request) {
for (final String measure : request.getMeasures()) {
data.add(measure);
}
}
private String[] getDimensionIDs(CubeRequest request) {
private String[] getDimensionIDs(final CubeRequest request) {
String[] dimensionIDs = new String[request.getDimensions().size()];
for (int i = 0; i < dimensionIDs.length; i++) {
Map<String, Object> map = request.getDimensions().get(i);
final Map<String, Object> map = request.getDimensions().get(i);
dimensionIDs[i] = (String) map.get("id");
}
......
......@@ -7,8 +7,9 @@ import java.security.NoSuchAlgorithmException;
import com.nesstar.rest.Version;
public class ETagGenerator {
protected static final String ALGORITHM = "MD5";
protected static final int RADIX = 16;
protected final String salt;
protected final static String ALGORITHM = "MD5";
protected final ETagData data;
public ETagGenerator(ETagData data, String salt) {
......@@ -23,14 +24,11 @@ public class ETagGenerator {
public String generateETagFromData() {
String input = data.getString();
String etag = generateChecksum(input);
return etag;
return generateChecksum(data.getString());
}
protected String generateChecksum(final String input) {
String checksum = digest(input + salt);
return checksum;
return digest(input + salt);
}
protected String digest(String input) {
......@@ -46,6 +44,6 @@ public class ETagGenerator {
}
protected String bytesToString(byte[] bytes) {
return new BigInteger(1, bytes).toString(16);
return new BigInteger(1, bytes).toString(RADIX);
}
}
......@@ -3,8 +3,6 @@ package com.nesstar.rest.common;
import com.nesstar.api.NotAuthorizedException;
import com.nesstar.api.Server;
import javax.servlet.http.HttpServletRequest;
public abstract class Operation {
public abstract Object run() throws Exception;
......
......@@ -2,10 +2,8 @@ package com.nesstar.rest.common;
import com.nesstar.rest.result_object_makers.ErrorMaker;
import javax.servlet.http.HttpServletRequest;
public final class OperationRunner {
private static final int maxAttempts = 2;
private static final int MAXATTEMPTS = 2;
private OperationRunner() {
}
......@@ -17,7 +15,7 @@ public final class OperationRunner {
return operation.run();
} catch (Exception e) {
attempts++;
if (attempts >= maxAttempts || !operation.tryToRecover(e, serverHandler)) {
if (attempts >= MAXATTEMPTS || !operation.tryToRecover(e, serverHandler)) {
return ErrorMaker.getErrorFromException(e);
}
}
......
......@@ -23,11 +23,13 @@ public final class ServerHandler {
public Server getServer() throws IOException {
NesstarDB db = NesstarDBFactory.getInstance();
if (server == null) {
if (server != null) {
return server;
}
if (userId != null && password != null) {
server = db.getServer(serverURI);
if (userId != null && password != null) {
server.login(userId, password);
}
server.login(userId, password);
}
return server;
}
......
package com.nesstar.rest.common;
import com.nesstar.api.Study;
import com.nesstar.api.Variable;
import com.nesstar.api.Category;
import com.nesstar.api.DdiList;
import com.nesstar.api.Server;
import com.nesstar.api.analysis.Statistic;
import java.util.Arrays;
import java.util.List;
public class TabulationClassVariables {
private Study study;
private Server server;
private List<Variable> breakVariables;
private List<DdiList<Category>> categories;
private Variable measureVariable;
private Statistic[] measureTypes;
private List<Variable> weightVariables;
private String caseSubset;
public String getCaseSubset() {
return caseSubset;
}
public void setCaseSubset(String caseSubset) {
this.caseSubset = caseSubset;
}
public Study getStudy() {
return study;
}
public void setStudy(Study study) {
this.study = study;
}
public Server getServer() {
return server;
}
public void setServer(Server server) {
this.server = server;
}
public List<Variable> getBreakVariables() {
return breakVariables;
}
public void setBreakVariables(List<Variable> breakVariables) {
this.breakVariables = breakVariables;
}
public List<DdiList<Category>> getCategories() {
return categories;
}
public void setCategories(List<DdiList<Category>> categories) {
this.categories = categories;
}
public Variable getMeasureVariable() {
return measureVariable;
}
public void setMeasureVariable(Variable measureVariable) {
this.measureVariable = measureVariable;
}