Skip to content

Commit

Permalink
#8 Destination class
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed Apr 17, 2016
1 parent 9ad33af commit d16b377
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 49 deletions.
107 changes: 107 additions & 0 deletions src/main/java/io/jare/tk/Destination.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2016 jare.io
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions: the above copyright notice and this
* permission notice shall be included in all copies or substantial
* portions of the Software. The software is provided "as is", without
* warranty of any kind, express or implied, including but not limited to
* the warranties of merchantability, fitness for a particular purpose
* and non-infringement. In no event shall the authors or copyright
* holders be liable for any claim, damages or other liability, whether
* in an action of contract, tort or otherwise, arising from, out of or
* in connection with the software or the use or other dealings in the
* software.
*/
package io.jare.tk;

import io.jare.model.Base;
import io.jare.model.Domain;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.Iterator;
import java.util.Locale;
import org.takes.HttpException;

/**
* Destination for relay.
*
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 0.4
*/
final class Destination {

/**
* Base.
*/
private final transient Base base;

/**
* Destination URI.
*/
private final transient URI uri;

/**
* Ctor.
* @param bse Base
* @param dst Destination URI (full)
*/
Destination(final Base bse, final URI dst) {
this.base = bse;
this.uri = dst;
}

/**
* Build destination path.
* @return Destination path
* @throws HttpException If fails
*/
public String path() throws HttpException {
if (!this.uri.isAbsolute()) {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
String.format("URI \"%s\" is not absolute", this.uri)
);
}
final String protocol = this.uri.getScheme();
if (!"https".equals(protocol) && !"http".equals(protocol)) {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
String.format(
"protocol must be either HTTP or HTTPS at \"%s\"",
this.uri
)
);
}
if (this.uri.getHost() == null) {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
String.format("URI \"%s\" doesn't have a host", this.uri)
);
}
final String host = this.uri.getHost().toLowerCase(Locale.ENGLISH);
final Iterator<Domain> domains = this.base.domain(host);
if (!domains.hasNext()) {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
String.format("domain \"%s\" is not registered", host)
);
}
final String path;
if (this.uri.getPath().isEmpty()) {
path = "/";
} else {
path = this.uri.getPath();
}
return path;
}

}
50 changes: 1 addition & 49 deletions src/main/java/io/jare/tk/TkRelay.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@

import com.google.common.collect.Iterables;
import io.jare.model.Base;
import io.jare.model.Domain;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.util.Collections;
import java.util.Iterator;
import java.util.Locale;
import org.takes.HttpException;
import org.takes.Request;
import org.takes.Response;
Expand Down Expand Up @@ -75,58 +73,12 @@ public Response act(final Request req) throws IOException {
final URI uri = URI.create(param.next().trim());
return new RsWithHeader(
new TkProxy(uri.toString()).act(
TkRelay.request(req, this.path(uri))
TkRelay.request(req, new Destination(this.base, uri).path())
),
String.format("X-Jare-Target: %s", uri)
);
}

/**
* Build destination path.
* @param uri URI of destination
* @return Destination path
* @throws HttpException If fails
*/
private String path(final URI uri) throws HttpException {
if (!uri.isAbsolute()) {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
String.format("URI \"%s\" is not absolute", uri)
);
}
final String protocol = uri.getScheme();
if (!"https".equals(protocol) && !"http".equals(protocol)) {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
String.format(
"protocol must be either HTTP or HTTPS at \"%s\"",
uri
)
);
}
if (uri.getHost() == null) {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
String.format("URI \"%s\" doesn't have a host", uri)
);
}
final String host = uri.getHost().toLowerCase(Locale.ENGLISH);
final Iterator<Domain> domains = this.base.domain(host);
if (!domains.hasNext()) {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
String.format("domain \"%s\" is not registered", host)
);
}
final String path;
if (uri.getPath().isEmpty()) {
path = "/";
} else {
path = uri.getPath();
}
return path;
}

/**
* The request to send.
* @param req Original request
Expand Down

0 comments on commit d16b377

Please sign in to comment.