Added better logging with timestamps and removed verbose http responses.

This commit is contained in:
Winston Li 2015-01-10 11:27:57 +00:00
parent ae3808e5ad
commit ca936d6630
27 changed files with 116 additions and 82 deletions

View file

@ -4,6 +4,7 @@ import org.eclipse.jetty.http.HttpURI;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.util.MultiMap;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.writelatex.WriteLatexAPI;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.InvalidPostbackKeyException;
@ -28,7 +29,7 @@ public class AttsResourceHandler extends ResourceHandler {
String method = baseRequest.getMethod();
if (method.equals("GET")) {
HttpURI uri = baseRequest.getUri();
System.out.println(method + " " + uri);
Util.sout(method + " " + uri);
MultiMap<String> multimap = new MultiMap<String>();
uri.decodeQueryTo(multimap);
String[] pathSections = uri.getPath().split("/");

View file

@ -35,7 +35,6 @@ public class SnapshotPushPostbackContents implements JSONSource {
@Override
public void fromJSON(JsonElement json) {
System.out.println(json);
JsonObject responseObject = json.getAsJsonObject();
String code = responseObject.get("code").getAsString();
setResult(responseObject, code);

View file

@ -33,12 +33,11 @@ public class SnapshotPushPostbackHandler extends AbstractHandler {
String contents = Util.getContentsOfReader(request.getReader());
String[] parts = request.getRequestURI().split("/");
if (parts.length < 4) {
System.out.println("Invalid postback url");
throw new ServletException();
}
String projectName = parts[1];
String postbackKey = parts[2];
System.out.println("Postback received for project: " + projectName);
Util.sout(baseRequest.getMethod() + " " + baseRequest.getUri());
SnapshotPushPostbackContents postbackContents = new SnapshotPushPostbackContents(writeLatexDataSource, projectName, postbackKey, contents);
JsonObject body = new JsonObject();
@ -57,13 +56,13 @@ public class SnapshotPushPostbackHandler extends AbstractHandler {
baseRequest.setHandled(true);
}
} catch (IOException e) {
e.printStackTrace();
Util.printStackTrace(e);
throw e;
} catch (ServletException e) {
e.printStackTrace();
Util.printStackTrace(e);
throw e;
} catch (RuntimeException e) {
e.printStackTrace();
Util.printStackTrace(e);
throw e;
}
}

View file

@ -3,6 +3,7 @@ package uk.ac.ic.wlgitbridge.application;
import uk.ac.ic.wlgitbridge.application.exception.InvalidConfigFileException;
import uk.ac.ic.wlgitbridge.application.exception.InvalidProgramArgumentsException;
import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException;
import uk.ac.ic.wlgitbridge.util.Util;
import javax.servlet.ServletException;
import java.io.IOException;
@ -35,16 +36,16 @@ public class WLGitBridgeApplication {
printUsage();
System.exit(EXIT_CODE_FAILED);
} catch (InvalidConfigFileException e) {
System.out.println("The property for " + e.getMissingMember() + " is invalid. Check your config file.");
System.err.println("The property for " + e.getMissingMember() + " is invalid. Check your config file.");
System.exit(EXIT_CODE_FAILED);
} catch (IOException e) {
System.out.println("Invalid config file. Check the file path.");
System.err.println("Invalid config file. Check the file path.");
System.exit(EXIT_CODE_FAILED);
}
try {
server = new WLGitBridgeServer(config);
} catch (ServletException e) {
e.printStackTrace();
Util.printStackTrace(e);
} catch (InvalidRootDirectoryPathException e) {
System.out.println("Invalid root git directory path. Check your config file.");
System.exit(EXIT_CODE_FAILED);

View file

@ -64,16 +64,16 @@ public class WLGitBridgeServer {
public void start() {
try {
jettyServer.start();
System.out.println();
System.out.println(Util.getServiceName() + "-Git Bridge server started");
System.out.println("Listening on port: " + port);
System.out.println("Bridged to: " + writeLatexHostname);
System.out.println("Postback base URL: " + Util.getPostbackURL());
System.out.println("Root git directory path: " + rootGitDirectoryPath);
Util.sout();
Util.sout(Util.getServiceName() + "-Git Bridge server started");
Util.sout("Listening on port: " + port);
Util.sout("Bridged to: " + writeLatexHostname);
Util.sout("Postback base URL: " + Util.getPostbackURL());
Util.sout("Root git directory path: " + rootGitDirectoryPath);
} catch (BindException e) {
e.printStackTrace();
Util.printStackTrace(e);
} catch (Exception e) {
e.printStackTrace();
Util.printStackTrace(e);
}
}
@ -81,7 +81,7 @@ public class WLGitBridgeServer {
try {
jettyServer.stop();
} catch (Exception e) {
e.printStackTrace();
Util.printStackTrace(e);
}
}

View file

@ -32,7 +32,7 @@ public class WLRepositoryResolver implements RepositoryResolver<HttpServletReque
try {
return repositorySource.getRepositoryWithNameAtRootDirectory(Util.removeAllSuffixes(name, "/", ".git"), rootGitDirectory);
} catch (RepositoryNotFoundException e) {
e.printStackTrace();
Util.printStackTrace(e);
throw e;
/*
} catch (ServiceNotAuthorizedException e) {
@ -41,10 +41,10 @@ public class WLRepositoryResolver implements RepositoryResolver<HttpServletReque
cannot occur
*/
} catch (ServiceMayNotContinueException e) { /* Such as FailedConnectionException */
e.printStackTrace();
Util.printStackTrace(e);
throw e;
} catch (RuntimeException e) {
e.printStackTrace();
Util.printStackTrace(e);
throw new ServiceMayNotContinueException(e);
}
}

View file

@ -10,6 +10,7 @@ import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource;
import uk.ac.ic.wlgitbridge.git.handler.hook.exception.ForcedPushException;
import uk.ac.ic.wlgitbridge.git.handler.hook.exception.WrongBranchException;
import uk.ac.ic.wlgitbridge.git.util.RepositoryObjectTreeWalker;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.InternalErrorException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.OutOfDateException;
@ -44,7 +45,7 @@ public class WriteLatexPutHook implements PreReceiveHook {
} catch (SnapshotPostException e) {
handleSnapshotPostException(receivePack, receiveCommand, e);
} catch (Throwable t) {
t.printStackTrace();
Util.printStackTrace(t);
handleSnapshotPostException(receivePack, receiveCommand, new InternalErrorException());
}
}

View file

@ -5,6 +5,7 @@ import org.eclipse.jetty.server.handler.AbstractHandler;
import uk.ac.ic.wlgitbridge.test.exception.InvalidAPICallException;
import uk.ac.ic.wlgitbridge.test.response.SnapshotResponse;
import uk.ac.ic.wlgitbridge.test.response.SnapshotResponseBuilder;
import uk.ac.ic.wlgitbridge.util.Util;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
@ -30,9 +31,9 @@ public class MockSnapshotRequestHandler extends AbstractHandler {
response.getWriter().println(snapshotResponse.respond());
new PostbackThread(baseRequest.getReader(), snapshotResponse.postback()).startIfNotNull();
} catch (InvalidAPICallException e) {
e.printStackTrace();
Util.printStackTrace(e);
} catch (RuntimeException e) {
e.printStackTrace();
Util.printStackTrace(e);
}
baseRequest.setHandled(true);
}

View file

@ -4,6 +4,7 @@ import org.eclipse.jetty.server.NetworkConnector;
import org.eclipse.jetty.server.Server;
import uk.ac.ic.wlgitbridge.test.response.SnapshotResponseBuilder;
import uk.ac.ic.wlgitbridge.test.state.SnapshotAPIState;
import uk.ac.ic.wlgitbridge.util.Util;
/**
* Created by Winston on 09/01/15.
@ -24,10 +25,9 @@ public class MockSnapshotServer {
try {
server.start();
} catch (Exception e) {
e.printStackTrace();
Util.printStackTrace(e);
}
port = ((NetworkConnector) server.getConnectors()[0]).getLocalPort();
System.out.println(port);
}
public void setState(SnapshotAPIState state) {

View file

@ -3,6 +3,7 @@ package uk.ac.ic.wlgitbridge.test.server;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.ning.http.client.AsyncHttpClient;
import uk.ac.ic.wlgitbridge.util.Util;
import java.io.IOException;
import java.io.Reader;
@ -28,11 +29,11 @@ public class PostbackThread extends Thread {
try {
new AsyncHttpClient().preparePost(url).setBody(postback).execute().get().getResponseBody();
} catch (IOException e) {
e.printStackTrace();
Util.printStackTrace(e);
} catch (InterruptedException e) {
e.printStackTrace();
Util.printStackTrace(e);
} catch (ExecutionException e) {
e.printStackTrace();
Util.printStackTrace(e);
}
}

View file

@ -4,6 +4,10 @@ import uk.ac.ic.wlgitbridge.application.SSLConfig;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by Winston on 19/11/14.
@ -15,6 +19,7 @@ public class Util {
private static SSLConfig SSL_CONFIG;
private static int PORT;
private static String POSTBACK_URL;
private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSSSS");
public static String entries(int entries) {
if (entries == 1) {
@ -85,4 +90,41 @@ public class Util {
public static String getPostbackURL() {
return POSTBACK_URL;
}
private static void println(PrintStream ps, String ln) {
ps.println(getStringBuilder().append(ln));
}
public static void sout(String ln) {
println(System.out, ln);
}
public static void serr(String ln) {
println(System.err, ln);
}
private static StringBuilder getStringBuilder() {
StringBuilder sb = new StringBuilder("[");
sb.append(dateFormat.format(new Date()));
sb.append("] ");
return sb;
}
public static void sout() {
sout("");
}
public static void serr() {
serr("");
}
public static void serr(Object obj) {
serr(obj.toString());
}
public static void printStackTrace(Throwable t) {
serr();
t.printStackTrace();
}
}

View file

@ -1,5 +1,6 @@
package uk.ac.ic.wlgitbridge.writelatex;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.SnapshotGetDocRequest;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.SnapshotGetDocResult;
@ -38,7 +39,7 @@ public class SnapshotFetcher implements PersistentStoreSource {
for (Snapshot snapshot : newSnapshots) {
persistentStore.addSnapshot(projectName, snapshot.getVersionID());
}
System.out.println("Fetched snapshots: " + newSnapshots);
Util.sout("Fetched snapshots: " + newSnapshots);
return newSnapshots;
}

View file

@ -38,7 +38,6 @@ public class WLDirectoryNodeSnapshot implements CandidateSnapshot {
jsonObject.addProperty("latestVerId", previousVersionID);
jsonObject.add("files", getFilesAsJson());
jsonObject.addProperty("postbackUrl", projectURL + "/" + postbackKey + "/postback");
System.out.println(jsonObject);
return jsonObject;
}

View file

@ -4,16 +4,14 @@ import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshot;
import uk.ac.ic.wlgitbridge.bridge.RawDirectoryContents;
import uk.ac.ic.wlgitbridge.bridge.WritableRepositoryContents;
import uk.ac.ic.wlgitbridge.bridge.WriteLatexDataSource;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.SnapshotGetDocRequest;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.InvalidPostbackKeyException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.PostbackManager;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.SnapshotPushRequest;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.SnapshotPushRequestResult;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.UnexpectedPostbackException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.OutOfDateException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.*;
import uk.ac.ic.wlgitbridge.writelatex.model.WLDataModel;
import java.io.IOException;
@ -63,7 +61,7 @@ public class WriteLatexAPI implements WriteLatexDataSource {
@Override
public List<WritableRepositoryContents> getWritableRepositories(String projectName) throws FailedConnectionException, InvalidProjectException {
System.out.println("Fetching project: " + projectName);
Util.sout("Fetching project: " + projectName);
List<WritableRepositoryContents> writableRepositoryContents = dataModel.updateProjectWithName(projectName);
return writableRepositoryContents;
}
@ -72,7 +70,7 @@ public class WriteLatexAPI implements WriteLatexDataSource {
public void putDirectoryContentsToProjectWithName(String projectName, RawDirectoryContents directoryContents, String hostname) throws SnapshotPostException, IOException {
mainProjectLock.lockForProject(projectName);
try {
System.out.println("Pushing project: " + projectName);
Util.sout("Pushing project: " + projectName);
String postbackKey = postbackManager.makeKeyForProject(projectName);
CandidateSnapshot candidate = dataModel.createCandidateSnapshotFromProjectWithContents(projectName, directoryContents, hostname, postbackKey);
SnapshotPushRequest snapshotPushRequest = new SnapshotPushRequest(candidate);
@ -83,6 +81,9 @@ public class WriteLatexAPI implements WriteLatexDataSource {
} else {
throw new OutOfDateException();
}
} catch (SevereSnapshotPostException e) {
e.printStackTrace();
throw e;
} catch (SnapshotPostException e) {
throw e;
} catch (IOException e) {

View file

@ -7,6 +7,7 @@ import com.ning.http.client.AsyncHttpClient;
import com.ning.http.client.AsyncHttpClient.BoundRequestBuilder;
import com.ning.http.client.Realm;
import com.ning.http.client.Response;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import java.util.concurrent.ExecutionException;
@ -62,12 +63,12 @@ public abstract class Request<T extends Result> {
}
private void performGetRequest() {
System.out.println("GET -> " + url);
Util.sout("GET -> " + url);
request(new AsyncHttpClient().prepareGet(url));
}
private void performPostRequest() {
System.out.println("POST -> " + url);
Util.sout("POST -> " + url);
request(new AsyncHttpClient().preparePost(url).setBody(getPostBody()).setHeader("Content-Type", "application/json"));
}
@ -77,13 +78,13 @@ public abstract class Request<T extends Result> {
@Override
public T onCompleted(Response response) throws Exception {
String body = response.getResponseBody();
System.out.println(response.getStatusText() + " (" + body.length() + " data bytes) -> " + url);
Util.sout(response.getStatusCode() + " " + response.getStatusText() + " (" + body.length() + "B) -> " + url);
return parseResponse(new Gson().fromJson(body, JsonElement.class));
}
@Override
public void onThrowable(Throwable t) {
t.printStackTrace();
Util.printStackTrace(t);
error = true;
}

View file

@ -38,7 +38,6 @@ public class InvalidProjectException extends SnapshotPostException {
public void fromJSON(JsonElement json) {
errors = new LinkedList<String>();
JsonArray errors = json.getAsJsonObject().get("errors").getAsJsonArray();
System.out.println(errors);
for (JsonElement error : errors) {
this.errors.add(error.getAsString());
}

View file

@ -39,7 +39,6 @@ public class PostbackContents {
throw new PostbackTimeoutException();
}
} catch (InterruptedException e) {
e.printStackTrace();
throw new InternalErrorException();
}
}

View file

@ -9,7 +9,7 @@ import java.util.List;
/**
* Created by Winston on 09/01/15.
*/
public class InternalErrorException extends SnapshotPostException {
public class InternalErrorException extends SevereSnapshotPostException {
@Override
public String getMessage() {

View file

@ -9,7 +9,7 @@ import java.util.List;
/**
* Created by Winston on 09/01/15.
*/
public class PostbackTimeoutException extends SnapshotPostException {
public class PostbackTimeoutException extends SevereSnapshotPostException {
@Override
public String getMessage() {

View file

@ -0,0 +1,18 @@
package uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception;
import com.google.gson.JsonElement;
/**
* Created by Winston on 10/01/15.
*/
public abstract class SevereSnapshotPostException extends SnapshotPostException {
public SevereSnapshotPostException() {
super();
}
public SevereSnapshotPostException(JsonElement json) {
super(json);
}
}

View file

@ -10,7 +10,7 @@ import java.util.List;
/**
* Created by Winston on 16/11/14.
*/
public class UnexpectedErrorException extends SnapshotPostException {
public class UnexpectedErrorException extends SevereSnapshotPostException {
private static final String[] DESCRIPTION_LINES = {
"There was an internal error with the " + Util.getServiceName() + " server.",

View file

@ -16,7 +16,6 @@ public class BlobHash {
try {
md = MessageDigest.getInstance("SHA-256");
hash = md.digest(blob);
System.out.println(Arrays.toString(hash));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}

View file

@ -4,6 +4,7 @@ import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshot;
import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshotCallback;
import uk.ac.ic.wlgitbridge.bridge.RawDirectoryContents;
import uk.ac.ic.wlgitbridge.bridge.WritableRepositoryContents;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.writelatex.WLDirectoryNodeSnapshot;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException;
@ -29,9 +30,9 @@ public class WLDataModel implements CandidateSnapshotCallback {
File rootGitDirectory = initRootGitDirectory(rootGitDirectoryPath);
persistentStore = new WLGBPersistentStore(rootGitDirectory);
projectStore = persistentStore.loadProjectStore();
System.out.println("Loaded projects: " + projectStore.getProjectNames().size() + ".");
Util.sout("Loaded projects: " + projectStore.getProjectNames().size() + ".");
fileStore = persistentStore.loadFileStore();
System.out.println("Loaded file store and index tables.");
Util.sout("Loaded file store and index tables.");
List<String> excludedFromDeletion = projectStore.getProjectNames();
excludedFromDeletion.add(".wlgb");
WLFileStore.deleteInDirectoryApartFrom(rootGitDirectory, excludedFromDeletion.toArray(new String[] {}));

View file

@ -1,5 +1,6 @@
package uk.ac.ic.wlgitbridge.writelatex.model.db.sql;
import uk.ac.ic.wlgitbridge.util.Util;
import uk.ac.ic.wlgitbridge.writelatex.filestore.node.FileNode;
import uk.ac.ic.wlgitbridge.writelatex.filestore.store.FileIndexStore;
import uk.ac.ic.wlgitbridge.writelatex.model.db.sql.query.GetFileNodesForProjectNameSQLQuery;
@ -29,7 +30,7 @@ public class SQLiteWLDatabase {
public SQLiteWLDatabase(File rootGitDirectory) throws SQLException, ClassNotFoundException {
File databaseFile = new File(rootGitDirectory, "/.wlgb/wlgb.db");
databaseFile.getParentFile().mkdirs();
System.out.println("Loading data...");
Util.sout("Loading data...");
Class.forName("org.sqlite.JDBC");
connection = DriverManager.getConnection("jdbc:sqlite:" + databaseFile.getAbsolutePath());
createTables();

View file

@ -9,22 +9,6 @@ public class RequestTest {
@Test
public void nothingToTest() {
// String projectName = "1826rqgsdb";
// Request getDoc = new SnapshotGetDocRequest(projectName);
// Request getSavedVers = new SnapshotGetSavedVersRequest(projectName);
// Request getForVersion = new SnapshotGetForVersionRequest(projectName, 76);
//
// getDoc.request();
// getSavedVers.request();
// getForVersion.request();
//
// try {
// System.out.println(getDoc.getResult());
// System.out.println(getSavedVers.getResult());
// System.out.println(getForVersion.getResult());
// } catch (Throwable e) {
// e.printStackTrace();
// }
}
}

View file

@ -9,14 +9,6 @@ public class SnapshotGetForVersionRequestTest {
@Test
public void nothingToTest() {
// SnapshotGetForVersionRequest request = new SnapshotGetForVersionRequest("1826rqgsdb", 76);
// request.request();
// try {
// SnapshotGetForVersionResult result = request.getResult();
// } catch (Throwable throwable) {
// throwable.printStackTrace();
// }
}

View file

@ -9,13 +9,7 @@ public class SnapshotGetSavedVersRequestTest {
@Test
public void nothingToTest() {
// SnapshotGetSavedVersRequest request = new SnapshotGetSavedVersRequest("1826rqgsdb");
// request.request();
// try {
// System.out.println(request.getResult());
// } catch (Throwable throwable) {
// throwable.printStackTrace();
// }
}
}