Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
Signed-off-by: Jan N. Klug <[email protected]>
  • Loading branch information
J-N-K committed Oct 19, 2023
1 parent 160374f commit b55fa4d
Show file tree
Hide file tree
Showing 53 changed files with 655 additions and 418 deletions.
18 changes: 18 additions & 0 deletions bundles/org.smarthomej.binding.amazonechocontrol/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@
<version>2.2.600</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.annotation</artifactId>
<version>2.2.600</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.annotation</artifactId>
<version>2.2.600</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jdt</groupId>
<artifactId>org.eclipse.jdt.annotation</artifactId>
<version>2.2.600</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
Expand Down Expand Up @@ -56,8 +57,6 @@
import org.smarthomej.binding.amazonechocontrol.internal.handler.AccountHandler;
import org.smarthomej.binding.amazonechocontrol.internal.util.HttpRequestBuilder;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;

/**
Expand All @@ -80,19 +79,16 @@ public class AccountServlet extends HttpServlet {
private static final String PROXY_URI_PART = "/PROXY/";

private final Logger logger = LoggerFactory.getLogger(AccountServlet.class);

private final HttpService httpService;
private final String servletUrl;
private final AccountHandler accountHandler;
private final String id;
private final Gson gson;
private @Nullable Connection connectionToInitialize;

public AccountServlet(HttpService httpService, String id, AccountHandler accountHandler, Gson gson) {
public AccountServlet(HttpService httpService, String id, AccountHandler accountHandler) {
this.httpService = httpService;
this.accountHandler = accountHandler;
this.id = id;
this.gson = gson;

try {
servletUrl = "/" + BINDING_ID + "/" + URLEncoder.encode(id, StandardCharsets.UTF_8);
Expand Down Expand Up @@ -164,11 +160,12 @@ private void doVerb(String verb, HttpServletRequest req, HttpServletResponse res
+ uri.substring(PROXY_URI_PART.length());

Object postData = null;
boolean isJson = false;
if ("POST".equals(verb) || "PUT".equals(verb)) {
postData = gson.fromJson(req.getReader(), JsonObject.class);
postData = req.getReader().lines().collect(Collectors.joining());
}

this.handleProxyRequest(connection, resp, verb, getUrl, null, postData, connection.getAmazonSite());
this.handleProxyRequest(connection, resp, verb, getUrl, null, postData, true, connection.getAmazonSite());
return;
}

Expand Down Expand Up @@ -220,7 +217,7 @@ private void doVerb(String verb, HttpServletRequest req, HttpServletResponse res
}
String referer = "https://www." + site;
String postData = postDataBuilder.toString();
handleProxyRequest(connection, resp, "POST", postUrl, referer, postData, site);
handleProxyRequest(connection, resp, "POST", postUrl, referer, postData, false, site);
}

@Override
Expand All @@ -243,7 +240,7 @@ protected void doGet(@NonNullByDefault({}) HttpServletRequest req, @NonNullByDef
String getUrl = "https://www." + connection.getAmazonSite() + "/"
+ uri.substring(FORWARD_URI_PART.length());

this.handleProxyRequest(connection, resp, "GET", getUrl, null, null, connection.getAmazonSite());
this.handleProxyRequest(connection, resp, "GET", getUrl, null, null, false, connection.getAmazonSite());
return;
}

Expand All @@ -258,7 +255,7 @@ protected void doGet(@NonNullByDefault({}) HttpServletRequest req, @NonNullByDef
String getUrl = "https://alexa." + connection.getAmazonSite() + "/"
+ uri.substring(PROXY_URI_PART.length());

this.handleProxyRequest(connection, resp, "GET", getUrl, null, null, connection.getAmazonSite());
this.handleProxyRequest(connection, resp, "GET", getUrl, null, null, false, connection.getAmazonSite());
return;
}

Expand Down Expand Up @@ -578,16 +575,16 @@ private void renderBluetoothMacChannel(Connection connection, DeviceTO device, S
}

private void handleProxyRequest(Connection connection, HttpServletResponse resp, String verb, String url,
@Nullable String referer, @Nullable Object postData, String site) throws IOException {
@Nullable String referer, @Nullable Object postData, boolean isJson, String site) throws IOException {
try {
Map<String, String> headers = new HashMap<>();
if (referer != null) {
headers.put(HttpHeader.REFERER.asString(), referer);
}

HttpRequestBuilder.HttpResponse response = connection.getRequestBuilder()
.builder(HttpMethod.fromString(verb), url).withContent(postData).withJson(true).withHeaders(headers)
.retry(false).redirect(false).syncSend();
.builder(HttpMethod.fromString(verb), url).withContent(postData).withJson(isJson)
.withHeaders(headers).retry(false).redirect(false).syncSend();
if (response.statusCode() == HttpStatus.FOUND_302) {
String location = response.headers().get("location");
if (location.contains("/ap/maplanding")) {
Expand Down
Loading

0 comments on commit b55fa4d

Please sign in to comment.