Browse Source

First tests of new protocol finished

master
Janis 9 months ago
parent
commit
17c091626d
15 changed files with 110 additions and 79 deletions
  1. +10
    -0
      .idea/statistic.xml
  2. +7
    -9
      app/src/main/java/app/pott/kaffeepott/androidclient/Constants.java
  3. +4
    -1
      app/src/main/java/app/pott/kaffeepott/androidclient/content/InteractionManager.java
  4. +1
    -1
      app/src/main/java/app/pott/kaffeepott/androidclient/content/LocalDBAdapter.java
  5. +20
    -14
      app/src/main/java/app/pott/kaffeepott/androidclient/content/RemoteDBAdapter.java
  6. +1
    -1
      app/src/main/java/app/pott/kaffeepott/androidclient/content/URLQueryGenerator.java
  7. +1
    -1
      app/src/main/java/app/pott/kaffeepott/androidclient/content/constants/SQLStatements.java
  8. +4
    -0
      app/src/main/java/app/pott/kaffeepott/androidclient/json/JSONAccessException.java
  9. +3
    -14
      app/src/main/java/app/pott/kaffeepott/androidclient/json/JSONGenerator.java
  10. +2
    -6
      app/src/main/java/app/pott/kaffeepott/androidclient/json/JSONParser.java
  11. +8
    -8
      app/src/main/java/app/pott/kaffeepott/androidclient/json/SimpleJSONObject.java
  12. BIN
      gradle/wrapper/gradle-wrapper.jar
  13. +1
    -3
      gradle/wrapper/gradle-wrapper.properties
  14. +31
    -20
      gradlew
  15. +17
    -1
      gradlew.bat

+ 10
- 0
.idea/statistic.xml View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Statistic">
<option name="excludedDirectories">
<list>
<option value="$PROJECT_DIR$/app/src/main/java/app/pott/kaffeepott/androidclient/ui" />
</list>
</option>
</component>
</project>

+ 7
- 9
app/src/main/java/app/pott/kaffeepott/androidclient/Constants.java View File

@@ -134,10 +134,9 @@ public final class Constants {
public static final String SCHEME = "https";
public static final String DOMAIN = "barista.pott.app";
public static final int PORT = 443;
public static final String PATH = "/barista/";
public static final String PATH_PREVIEW = "/barista_preview/";
public static final String PATH_COMPONENT_API = "api";
public static final String PATH_COMPONENT_WHISPER = "whisper";
private static final String PATH_COMPONENT_API = "api";
public static final String PATH = "/barista/" + PATH_COMPONENT_API;
public static final String PATH_PREVIEW = "/barista_preview/" + PATH_COMPONENT_API;
public static final String QUERY = "version=" + VERSION + "&function=";

public static final String GET_CAFES = "listcafes";
@@ -184,10 +183,10 @@ public final class Constants {
public static final String BADGES = "badges";

public static final String PRODUCTS = "products";
public static final String PRODUCT_PRICE = "price";
public static final String PRODUCT_LOCALIZED = "type_localized";
public static final String PRODUCT_TYPE = "type";
public static final String PRODUCT_SIZE = "size";
public static final String PRODUCT_PRICE = "product_price";
public static final String PRODUCT_LOCALIZED = "product_type_name_localized";
public static final String PRODUCT_TYPE = "product_type_name";
public static final String PRODUCT_SIZE = "product_size";

public static final String OPENING_RANGES = "opening_ranges";
public static final String DAY_OF_WEEK = "day_of_week";
@@ -206,7 +205,6 @@ public final class Constants {
public static final String TICKET_SELECT = "select";
public static final String TICKET_CHANGE = "change";
public static final String TICKET_BADGE_TYPE = "badge_type_name";
public static final String TICKET_PRODUCT_NAME = "name";

public static final String RESPONSE_ERROR = "err";
public static final String RESPONSE_OK = "ok";


+ 4
- 1
app/src/main/java/app/pott/kaffeepott/androidclient/content/InteractionManager.java View File

@@ -177,7 +177,10 @@ public class InteractionManager {
private JSONParser.SubmissionResponse sendTicket(JSONObject ticketObj, Context context) throws JSONAccessException {
try {
long ticketId = remoteDBAdapter.submitTicket(ticketObj.toString(), context);
localDBAdapter.insertTicketId(ticketId, ticketObj.getLong(Constants.JSONConstants.CAFE_ID));
Ticket.Mode ticketMode = Ticket.Mode.valueOf(ticketObj.getString(Constants.JSONConstants.TICKET_OPERATION));
if (ticketMode != Ticket.Mode.ADD_CAFE && ticketMode != Ticket.Mode.REMOVE_CAFE)
localDBAdapter.insertTicketId(ticketId,
ticketObj.getSubObject(Constants.JSONConstants.TICKET_SELECT).getLong(Constants.JSONConstants.CAFE_ID));
return JSONParser.SubmissionResponse.OK;
} catch (IOException ioexception) {
return JSONParser.SubmissionResponse.ERR_TRY_AGAIN_LATER;


+ 1
- 1
app/src/main/java/app/pott/kaffeepott/androidclient/content/LocalDBAdapter.java View File

@@ -223,7 +223,7 @@ public class LocalDBAdapter implements Closeable {
cache(cafes, bounds, context, false);
}

public synchronized void insertTicketId(long cafeId, long ticketId) {
public synchronized void insertTicketId(long ticketId, long cafeId) {
query(SQLStatements.UpdateStatements.UPDATE_CAFE_TICKET_SENT, cafeId);
query(SQLStatements.InsertStatements.INSERT_TICKET, ticketId, cafeId);
}


+ 20
- 14
app/src/main/java/app/pott/kaffeepott/androidclient/content/RemoteDBAdapter.java View File

@@ -48,7 +48,7 @@ public class RemoteDBAdapter {
try {
String cafeQuery = queryGenerator.getCafeQuery(location, distance);
String cafeJSON = doRequest(null, URLConstants.GET_CAFES, cafeQuery,
getPath(context) + URLConstants.PATH_COMPONENT_API, URLConstants.METHOD_GET);
getPath(context), URLConstants.METHOD_GET);
return jsonParser.getCafes(cafeJSON);
} catch (JSONAccessException | IOException exception) {
Log.e("REMOTEDB", exception.toString());
@@ -63,7 +63,7 @@ public class RemoteDBAdapter {
*/
public String registerUser(Context context) throws IOException {
String response = doRequest(null, URLConstants.REGISTER_USER, null,
getPath(context) + URLConstants.PATH_COMPONENT_WHISPER, URLConstants.METHOD_GET);
getPath(context), URLConstants.METHOD_GET);
try {
return jsonParser.getUserId(response);
} catch (JSONAccessException accessException) {
@@ -75,7 +75,7 @@ public class RemoteDBAdapter {
public Ticket.ResolutionStatus getTicketStatus(long ticketId, Context context) {
try {
String response = doRequest(null, URLConstants.GET_TICKET_STATUS, queryGenerator.getTicketStatusQuery(ticketId),
getPath(context) + URLConstants.PATH_COMPONENT_API, URLConstants.METHOD_GET);
getPath(context), URLConstants.METHOD_GET);
return jsonParser.getResolutionStatus(response);
} catch (IOException | JSONAccessException exception) {
Log.e("REMOTEDB", exception.toString() + exception.getMessage());
@@ -94,7 +94,7 @@ public class RemoteDBAdapter {
public Long addCafe(String json, Context context) {
try {
String response = doRequest(json, URLConstants.OPEN_TICKET, null,
getPath(context) + URLConstants.PATH_COMPONENT_API, URLConstants.METHOD_POST);
getPath(context), URLConstants.METHOD_POST);
return jsonParser.getVirtualCafeId(response);
} catch (IOException | JSONAccessException exception) {
Log.e("REMOTEDB", exception.toString());
@@ -109,10 +109,10 @@ public class RemoteDBAdapter {
* @return All {@link ProductType} available or <code>null</code> is something went wrong.
*/
public ProductType[] getProducts(Context context) {
String request = jsonGenerator.generateProductRequest();
String request = queryGenerator.getProductQuery();
try {
String response = doRequest(request, URLConstants.GET_PRODUCT_TYPES, null,
getPath(context) + URLConstants.PATH_COMPONENT_API, URLConstants.METHOD_GET);
String response = doRequest(null, URLConstants.GET_PRODUCT_TYPES, request,
getPath(context), URLConstants.METHOD_GET);
return jsonParser.getProductTypes(response);
} catch (IOException | JSONAccessException exception) {
Log.e("REMOTEDB", exception.toString());
@@ -128,8 +128,8 @@ public class RemoteDBAdapter {
*/
public String getProductHash(Context context) {
try {
String response = doRequest(null, URLConstants.GET_PRODUCT_TYPE_HASH, queryGenerator.getProductHashQuery(),
getPath(context) + URLConstants.PATH_COMPONENT_API, URLConstants.METHOD_GET);
String response = doRequest(null, URLConstants.GET_PRODUCT_TYPE_HASH, queryGenerator.getProductQuery(),
getPath(context), URLConstants.METHOD_GET);
return jsonParser.getProductTypeHash(response);
} catch (IOException | JSONAccessException exception) {
return null;
@@ -163,9 +163,10 @@ public class RemoteDBAdapter {
throw new IllegalArgumentException("Illegal type: " + type);
}
try {
return jsonParser.isSubmissionOk(doRequest(json, operation, null,
getPath(context) + URLConstants.PATH_COMPONENT_API, type == InteractionManager.InteractiveContentType.RATING ?
URLConstants.METHOD_PUT : URLConstants.METHOD_POST));
doRequest(json, operation, null,
getPath(context), type == InteractionManager.InteractiveContentType.RATING ?
URLConstants.METHOD_PUT : URLConstants.METHOD_POST);
return JSONParser.SubmissionResponse.OK;
} catch (IOException ioexception) {
return JSONParser.SubmissionResponse.ERR_TRY_AGAIN_LATER;
}
@@ -183,7 +184,7 @@ public class RemoteDBAdapter {
public long submitTicket(String ticketJson, Context context) throws IOException {
try {
return jsonParser.getTicketId(doRequest(ticketJson, URLConstants.OPEN_TICKET, null,
getPath(context) + URLConstants.PATH_COMPONENT_API, URLConstants.METHOD_POST));
getPath(context), URLConstants.METHOD_POST));
} catch (JSONAccessException jsonAccessException) {
throw new IOException(jsonAccessException);
}
@@ -194,7 +195,9 @@ public class RemoteDBAdapter {
*
* @param jsonContent The body of the request or <code>null</code> if no body exists
* @param function The function used for this request, e.g. <code>listcafes</code>
* @param query Additional query string (starting with <code>&</code>, e.g. <code>&k=v</code>) or <code>null</code> if no additional queries are needed.
* @param query Additional query string (starting with <code>&</code>, e.g.
* <code>&k=v</code>) or <code>null</code> if no additional queries are
* needed.
* @param path The server URL's path.
* @param reqMethod The HTTP request method.
* @return The response body.
@@ -212,6 +215,9 @@ public class RemoteDBAdapter {
}

HttpsURLConnection baristaConnection = (HttpsURLConnection) uri.toURL().openConnection();
// if (baristaConnection.getResponseCode() != 400)
// throw new IOException("Error code: " + baristaConnection.getResponseCode());

baristaConnection.setRequestMethod(reqMethod);
baristaConnection.setRequestProperty(URLConstants.HEADER_CONTENT_TYPE, URLConstants.CONTENT_TYPE);



+ 1
- 1
app/src/main/java/app/pott/kaffeepott/androidclient/content/URLQueryGenerator.java View File

@@ -18,7 +18,7 @@ public class URLQueryGenerator {
"&" + Constants.JSONConstants.CAFE_DISTANCE + "=" + distance;
}

public String getProductHashQuery() {
public String getProductQuery() {
return "&" + Constants.JSONConstants.LOCALIZATION + "=" + Locales.getUserLanguage();
}



+ 1
- 1
app/src/main/java/app/pott/kaffeepott/androidclient/content/constants/SQLStatements.java View File

@@ -259,7 +259,7 @@ public class SQLStatements {
CafeDBContract.ProductTypeEntry.TYPE_NAME + " = ?";

public static final String UPDATE_CAFE_TICKET_SENT = "UPDATE " + CafeDBContract.CafeEntry.TABLE_NAME +
" SET " + CafeDBContract.CafeEntry.CHANGED_BY_USER + " = 0 WHERE" + CafeDBContract.CafeEntry._ID +
" SET " + CafeDBContract.CafeEntry.CHANGED_BY_USER + " = 0 WHERE " + CafeDBContract.CafeEntry._ID +
" = ?";

}


+ 4
- 0
app/src/main/java/app/pott/kaffeepott/androidclient/json/JSONAccessException.java View File

@@ -6,6 +6,10 @@ public class JSONAccessException extends Exception {
super(message);
}

public JSONAccessException(Throwable cause) {
super(cause);
}

public JSONAccessException() {
super();
}


+ 3
- 14
app/src/main/java/app/pott/kaffeepott/androidclient/json/JSONGenerator.java View File

@@ -37,7 +37,7 @@ public class JSONGenerator {
JSONObject rootObject = new SimpleJSONObject();
rootObject.put(Constants.JSONConstants.USER_ID, userId);
rootObject.put(Constants.JSONConstants.CAFE_ID, cafeId);
rootObject.put(Constants.JSONConstants.RATING_SET, rating);
rootObject.put(Constants.JSONConstants.CAFE_RATING_VALUE, rating);
rootObject.put(Constants.JSONConstants.CACHING_TYPE,
InteractionManager.InteractiveContentType.RATING.toString());
return rootObject;
@@ -58,17 +58,6 @@ public class JSONGenerator {
return rootObject.toString();
}

/**
* Generates a request that can be used to query the product types or the product type hash
* using the users language.
*
* @return The generated {@link JSONObject}.
*/
public String generateProductRequest() {
JSONObject request = new SimpleJSONObject();
request.put(Constants.JSONConstants.LOCALIZATION, Locales.getUserLanguage());
return request.toString();
}

/**
* Generates a request that can be used to open a ticket.
@@ -106,7 +95,7 @@ public class JSONGenerator {
switch (ticket.getMode()) {
case ADD_BADGE:
case REMOVE_BADGE:
changeObject.put(Constants.JSONConstants.TICKET_BADGE_TYPE,
selectObject.put(Constants.JSONConstants.TICKET_BADGE_TYPE,
ticket.getBadge().toString());
break;
case ADD_PRODUCT:
@@ -114,7 +103,7 @@ public class JSONGenerator {
changeObject.put(Constants.JSONConstants.PRODUCT_PRICE,
ticket.getProduct().getPrice());
case DELETE_PRODUCT:
selectObject.put(Constants.JSONConstants.TICKET_PRODUCT_NAME,
selectObject.put(Constants.JSONConstants.PRODUCT_TYPE,
ticket.getProduct().getProductType().getNameKey());
selectObject.put(Constants.JSONConstants.PRODUCT_SIZE,
ticket.getProduct().getSize().toString());


+ 2
- 6
app/src/main/java/app/pott/kaffeepott/androidclient/json/JSONParser.java View File

@@ -58,12 +58,8 @@ public class JSONParser {
*/
public String getUserId(String json) throws JSONAccessException, IOException {
JSONObject userObject = getContent(json);
if (userObject.has(Constants.JSONConstants.RESPONSE_OK))
return userObject.getString(Constants.JSONConstants.RESPONSE_OK);
else if (userObject.has(Constants.JSONConstants.RESPONSE_ERROR))
throw new IOException(userObject.getString(Constants.JSONConstants.RESPONSE_ERROR));
else if (userObject.has(Constants.JSONConstants.RESPONSE_RATE_LIMIT))
throw new IOException("Rate limit exceeded: " + userObject.getString(Constants.JSONConstants.RESPONSE_RATE_LIMIT));
if (userObject.has(Constants.JSONConstants.USER_ID))
return userObject.getString(Constants.JSONConstants.USER_ID);
throw new IOException("Communication failed, unknown reason");
}



+ 8
- 8
app/src/main/java/app/pott/kaffeepott/androidclient/json/SimpleJSONObject.java View File

@@ -20,7 +20,7 @@ public class SimpleJSONObject implements JSONObject {
try {
jsonObject = new org.json.JSONObject(json);
} catch (JSONException jsonException) {
throw new JSONAccessException();
throw new JSONAccessException(jsonException);
}
}

@@ -42,7 +42,7 @@ public class SimpleJSONObject implements JSONObject {
try {
return new SimpleJSONObject(jsonObject.getJSONObject(key));
} catch (JSONException jsonException) {
throw new JSONAccessException();
throw new JSONAccessException(jsonException);
}
}

@@ -51,7 +51,7 @@ public class SimpleJSONObject implements JSONObject {
try {
return new SimpleJSONArray(jsonObject.getJSONArray(key));
} catch (JSONException jsonException) {
throw new JSONAccessException();
throw new JSONAccessException(jsonException);
}
}

@@ -60,7 +60,7 @@ public class SimpleJSONObject implements JSONObject {
try {
return jsonObject.getString(key);
} catch (JSONException jsonException) {
throw new JSONAccessException();
throw new JSONAccessException(jsonException);
}
}

@@ -69,7 +69,7 @@ public class SimpleJSONObject implements JSONObject {
try {
return jsonObject.getInt(key);
} catch (JSONException jsonException) {
throw new JSONAccessException();
throw new JSONAccessException(jsonException);
}
}

@@ -78,7 +78,7 @@ public class SimpleJSONObject implements JSONObject {
try {
return jsonObject.getLong(key);
} catch (JSONException jsonException) {
throw new JSONAccessException();
throw new JSONAccessException(jsonException);
}
}

@@ -88,7 +88,7 @@ public class SimpleJSONObject implements JSONObject {
try {
return jsonObject.getDouble(key);
} catch (JSONException jsonException) {
throw new JSONAccessException();
throw new JSONAccessException(jsonException);
}
}

@@ -97,7 +97,7 @@ public class SimpleJSONObject implements JSONObject {
try {
return jsonObject.getBoolean(key);
} catch (JSONException jsonException) {
throw new JSONAccessException();
throw new JSONAccessException(jsonException);
}
}



BIN
gradle/wrapper/gradle-wrapper.jar View File


+ 1
- 3
gradle/wrapper/gradle-wrapper.properties View File

@@ -1,7 +1,5 @@
#Sat May 30 14:46:56 CEST 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
distributionSha256Sum=10065868c78f1207afb3a92176f99a37d753a513dff453abb6b5cceda4058cda

+ 31
- 20
gradlew View File

@@ -1,5 +1,21 @@
#!/usr/bin/env sh

#
# Copyright 2015 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

##############################################################################
##
## Gradle start up script for UN*X
@@ -28,7 +44,7 @@ APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"
@@ -109,8 +125,8 @@ if $darwin; then
GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
fi

# For Cygwin, switch paths to Windows format before running java
if $cygwin ; then
# For Cygwin or MSYS, switch paths to Windows format before running java
if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`
@@ -138,19 +154,19 @@ if $cygwin ; then
else
eval `echo args$i`="\"$arg\""
fi
i=$((i+1))
i=`expr $i + 1`
done
case $i in
(0) set -- ;;
(1) set -- "$args0" ;;
(2) set -- "$args0" "$args1" ;;
(3) set -- "$args0" "$args1" "$args2" ;;
(4) set -- "$args0" "$args1" "$args2" "$args3" ;;
(5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
(6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
(7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
(8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
(9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
0) set -- ;;
1) set -- "$args0" ;;
2) set -- "$args0" "$args1" ;;
3) set -- "$args0" "$args1" "$args2" ;;
4) set -- "$args0" "$args1" "$args2" "$args3" ;;
5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
esac
fi

@@ -159,14 +175,9 @@ save () {
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
echo " "
}
APP_ARGS=$(save "$@")
APP_ARGS=`save "$@"`

# Collect all arguments for the java command, following the shell quoting and substitution rules
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"

# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
cd "$(dirname "$0")"
fi

exec "$JAVACMD" "$@"

+ 17
- 1
gradlew.bat View File

@@ -1,3 +1,19 @@
@rem
@rem Copyright 2015 the original author or authors.
@rem
@rem Licensed under the Apache License, Version 2.0 (the "License");
@rem you may not use this file except in compliance with the License.
@rem You may obtain a copy of the License at
@rem
@rem https://www.apache.org/licenses/LICENSE-2.0
@rem
@rem Unless required by applicable law or agreed to in writing, software
@rem distributed under the License is distributed on an "AS IS" BASIS,
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@@ -14,7 +30,7 @@ set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome


Loading…
Cancel
Save