Skip to content

Commit

Permalink
Step 6 - Proxy URL
Browse files Browse the repository at this point in the history
Simple check to see if client is running in a proxy, and to call the
back-end servlet rather than the one running in angular. Also, we need
to let the server know we are running in a proxy.
  • Loading branch information
kabir committed Oct 28, 2019
1 parent ba43722 commit f113607
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
17 changes: 16 additions & 1 deletion src/main/java/org/kabir/quarkus/ui/SampleServlet.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.kabir.quarkus.ui;

import java.io.IOException;
import java.net.Socket;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
Expand All @@ -23,9 +24,23 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
} else if (path.equals("/callback")) {
// Redirect back to a path controlled by the Angular client
String redirectPath = "/clientCallback";

boolean proxy = Boolean.getBoolean("ui.proxy");
if (proxy && checkProxyIsRunning()) {
redirectPath = "http://localhost:4200" + redirectPath;
}
resp.sendRedirect(redirectPath);
} else {
resp.sendError(404);
}
}
}

private boolean checkProxyIsRunning() {
try (Socket s = new Socket("localhost", 4200)) {
return true;
} catch (IOException e) {
return false;
}
}
}

10 changes: 9 additions & 1 deletion webapp/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ export class AppComponent {
In <b>default</b> component.
<a [routerLink]="['/other']">Other</a> |
<a [routerLink]="['/rest']">Rest</a> |
<a href="/servlet/make-external-call">External</a>
<a href="{{externalUrl}}">External</a>
`,
styles: []
})
export class DefaultComponent {
externalUrl = '/servlet/make-external-call';

constructor() {
if (window.location.port === "4200") {
this.externalUrl = "http://localhost:8080" + this.externalUrl;
}
}

}

@Component({
Expand Down

0 comments on commit f113607

Please sign in to comment.