ProxyUri proxied = new ProxyUri(status, uri, queryUri); proxied.setHtmlTagContext(uriIn.getQueryParameter(Param.HTML_TAG_CONTEXT.getKey())); return proxied;
@Override protected boolean removeTag(Gadget gadget, Element elem, Uri ctx) { if (!elem.getNodeName().equalsIgnoreCase("link")) { return false; } boolean hasType = false; for (Attr attr : toList(elem.getAttributes())) { if ("rel".equalsIgnoreCase(attr.getName())) { hasType |= "stylesheet".equalsIgnoreCase(attr.getValue()); } else if ("type".equalsIgnoreCase(attr.getName())) { hasType |= "text/css".equalsIgnoreCase(attr.getValue()); } else if ("href".equalsIgnoreCase(attr.getName())) { try { ProxyUriManager.ProxyUri proxiedUri = ProxyUriManager.ProxyUri.fromList(gadget, ImmutableList.of(Uri.parse(attr.getValue()))).get(0); proxiedUri.setHtmlTagContext(elem.getNodeName().toLowerCase()); attr.setValue(cssImportRewriter.make(ImmutableList.of(proxiedUri), null) .get(0).toString()); } catch (IllegalArgumentException e) { return true; } } } return !hasType; }
public List<Uri> make(List<ProxyUri> resources, Integer forcedRefresh) { if (resources.isEmpty()) { return Collections.emptyList(); } List<Uri> resourceUris = Lists.newArrayListWithCapacity(resources.size()); for (ProxyUri puc : resources) { resourceUris.add(puc.getResource()); } Map<Uri, String> versions; if (versioner == null) { versions = Collections.emptyMap(); } else { versions = Maps.newHashMapWithExpectedSize(resources.size()); List<String> versionList = versioner.version(resourceUris, resources.get(0).getContainer()); if (versionList != null && versionList.size() == resources.size()) { // This should always be the case. // Should we error if not, or just WARNING? Iterator<String> versionIt = versionList.iterator(); for (ProxyUri puc : resources) { versions.put(puc.getResource(), versionIt.next()); } } } List<Uri> result = Lists.newArrayListWithCapacity(resources.size()); for (ProxyUri puc : resources) { result.add(makeProxiedUri(puc, forcedRefresh, versions.get(puc.getResource()))); } return result; }
ProxyUri proxied = new ProxyUri(status, uri, queryUri); proxied.setHtmlTagContext(uriIn.getQueryParameter(Param.HTML_TAG_CONTEXT.getKey())); return proxied;
ProxyUri proxied = new ProxyUri(status, uri, queryUri); proxied.setHtmlTagContext(uriIn.getQueryParameter(Param.HTML_TAG_CONTEXT.getKey())); return proxied;
public HttpResponse fetch(ProxyUriManager.ProxyUri proxyUri, @Nullable String postBody) throws IOException, GadgetException { HttpRequest rcr = buildHttpRequest(proxyUri, proxyUri.getResource(), postBody); if (rcr == null) { throw new GadgetException(GadgetException.Code.INVALID_PARAMETER, Uri fallbackUri = proxyUri.getFallbackUri(); if (fallbackUri != null) { HttpRequest fallbackRcr = buildHttpRequest(proxyUri, fallbackUri, null); if (!proxyUri.shouldReturnOrigOnErr() || !isRecoverable(results)) { throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e, e.getHttpStatusCode()); ServletUtil.setCachingHeaders(response, proxyUri.translateStatusRefresh(longLivedRefreshSec, (int) (results.getCacheTtl() / 1000)), false); } catch (GadgetException gex) {
proxyUri.setSecurityToken(st); proxyUri.setUserAgent(request.getHeader("User-Agent")); if(proxyUri.getGadget() == null) { if(st != null && !st.isAnonymous()) { proxyUri.setGadget(st.getAppUrl()); AuthType authType = proxyUri.getAuthType(); if(AuthType.OAUTH.equals(authType)) { proxyUri.setOAuthArguments(new OAuthArguments(AuthType.OAUTH, request)); } else if(AuthType.OAUTH2.equals(authType)) { proxyUri.setOAuth2Arguments(new OAuth2Arguments(request)); Uri resourceUri = proxyUri.getResource(); String msg = "Embed request for url " + (resourceUri != null ? resourceUri.toString() : "n/a") + " made to wrong domain " + host;
public HttpResponse fetch(ProxyUriManager.ProxyUri proxyUri) throws IOException, GadgetException { HttpRequest rcr = buildHttpRequest(proxyUri, proxyUri.getResource()); if (rcr == null) { throw new GadgetException(GadgetException.Code.INVALID_PARAMETER, Uri fallbackUri = proxyUri.getFallbackUri(); if (fallbackUri != null) { HttpRequest fallbackRcr = buildHttpRequest(proxyUri, fallbackUri); if (!proxyUri.shouldReturnOrigOnErr() || !isRecoverable(results)) { throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e, e.getHttpStatusCode()); proxyUri.translateStatusRefresh(LONG_LIVED_REFRESH, DEFAULT_REFRESH), false); } catch (GadgetException gex) { return ServletUtil.errorResponse(gex);
public HttpResponse fetch(ProxyUriManager.ProxyUri proxyUri) throws IOException, GadgetException { HttpRequest rcr = buildHttpRequest(proxyUri, proxyUri.getResource()); if (rcr == null) { throw new GadgetException(GadgetException.Code.INVALID_PARAMETER, Uri fallbackUri = proxyUri.getFallbackUri(); if (fallbackUri != null) { HttpRequest fallbackRcr = buildHttpRequest(proxyUri, fallbackUri); if (!proxyUri.shouldReturnOrigOnErr() || !isRecoverable(results)) { throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e, e.getHttpStatusCode()); proxyUri.translateStatusRefresh(LONG_LIVED_REFRESH, DEFAULT_REFRESH), false); } catch (GadgetException gex) { return ServletUtil.errorResponse(gex);
String uriStr = element.getAttribute(resourceTags.get(nodeName)).trim(); try { ProxyUriManager.ProxyUri proxiedUri = new ProxyUriManager.ProxyUri( gadget, Uri.parse(uriStr)); proxiedUri.setHtmlTagContext(nodeName); reservedUris.add(proxiedUri); reservedNodes.add(node);
String uriStr = element.getAttribute(resourceTags.get(nodeName)).trim(); try { ProxyUriManager.ProxyUri proxiedUri = new ProxyUriManager.ProxyUri( gadget, Uri.parse(uriStr)); proxiedUri.setHtmlTagContext(nodeName); reservedUris.add(proxiedUri); reservedNodes.add(node);
ProxyUriManager.ProxyUri uriToProxyOrRewrite) throws GadgetException { Uri tgt = uriToProxyOrRewrite.getResource(); HttpRequest req = uriToProxyOrRewrite.makeHttpRequest(tgt); if (req == null) { throw new GadgetException(GadgetException.Code.INVALID_PARAMETER,
String uriStr = element.getAttribute(resourceTags.get(nodeName)).trim(); try { ProxyUriManager.ProxyUri proxiedUri = new ProxyUriManager.ProxyUri( gadget, Uri.parse(uriStr)); proxiedUri.setHtmlTagContext(nodeName); reservedUris.add(proxiedUri); reservedNodes.add(node);
/** * Build an HttpRequest object encapsulating the request details as requested * by the user. * @param request The http request. * @param uriToProxyOrRewrite The parsed uri to proxy or rewrite through * accel servlet. * @return Remote content request based on the parameters sent from the client. * @throws GadgetException In case the data could not be fetched. */ protected HttpRequest buildHttpRequest(HttpRequest request, ProxyUriManager.ProxyUri uriToProxyOrRewrite) throws GadgetException { Uri tgt = uriToProxyOrRewrite.getResource(); HttpRequest req = uriToProxyOrRewrite.makeHttpRequest(tgt); if (req == null) { throw new GadgetException(GadgetException.Code.INVALID_PARAMETER, "No url parameter in request", HttpResponse.SC_BAD_REQUEST); } // Copy the post body if it exists. UriUtils.copyRequestData(request, req); // Set and copy headers. ServletUtil.setXForwardedForHeader(request, req); UriUtils.copyRequestHeaders( request, req, UriUtils.DisallowedHeaders.POST_INCOMPATIBLE_DIRECTIVES); req.setFollowRedirects(false); return req; }
/** * Build an HttpRequest object encapsulating the request details as requested * by the user. * @param request The http request. * @param uriToProxyOrRewrite The parsed uri to proxy or rewrite through * accel servlet. * @return Remote content request based on the parameters sent from the client. * @throws GadgetException In case the data could not be fetched. */ protected HttpRequest buildHttpRequest(HttpRequest request, ProxyUriManager.ProxyUri uriToProxyOrRewrite) throws GadgetException { Uri tgt = uriToProxyOrRewrite.getResource(); HttpRequest req = uriToProxyOrRewrite.makeHttpRequest(tgt); if (req == null) { throw new GadgetException(GadgetException.Code.INVALID_PARAMETER, "No url parameter in request", HttpResponse.SC_BAD_REQUEST); } // Copy the post body if it exists. UriUtils.copyRequestData(request, req); // Set and copy headers. ServletUtil.setXForwardedForHeader(request, req); UriUtils.copyRequestHeaders( request, req, UriUtils.DisallowedHeaders.POST_INCOMPATIBLE_DIRECTIVES); req.setFollowRedirects(false); return req; }
@Override protected boolean removeTag(Gadget gadget, Element elem, Uri ctx) { if (!elem.getNodeName().equalsIgnoreCase("link")) { return false; } boolean hasType = false; for (Attr attr : toList(elem.getAttributes())) { if ("rel".equalsIgnoreCase(attr.getName())) { hasType |= "stylesheet".equalsIgnoreCase(attr.getValue()); } else if ("type".equalsIgnoreCase(attr.getName())) { hasType |= "text/css".equalsIgnoreCase(attr.getValue()); } else if ("href".equalsIgnoreCase(attr.getName())) { try { ProxyUriManager.ProxyUri proxiedUri = ProxyUriManager.ProxyUri.fromList(gadget, ImmutableList.of(Uri.parse(attr.getValue()))).get(0); proxiedUri.setHtmlTagContext(elem.getNodeName().toLowerCase()); attr.setValue(cssImportRewriter.make(ImmutableList.of(proxiedUri), null) .get(0).toString()); } catch (IllegalArgumentException e) { return true; } } } return !hasType; }
/** * Returns the proxy uri encapsulating the request uri. * @param httpRequest The http request. * @return The proxy uri encapsulating the request uri. * @throws GadgetException In case of errors. */ public ProxyUriManager.ProxyUri getProxyUri(HttpRequest httpRequest) throws GadgetException { Uri proxiedUri = uriManager.parseAndNormalize(httpRequest); String uriString = proxiedUri.getQueryParameter(UriCommon.Param.URL.getKey()); // Throw BAD_GATEWAY in case parsing of url fails. Uri requestedResource; try { requestedResource = Uri.parse(uriString); } catch (Uri.UriException e) { throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, "Failed to parse uri: " + uriString, HttpResponse.SC_BAD_GATEWAY); } Gadget gadget = DomWalker.makeGadget(httpRequest); ProxyUriManager.ProxyUri proxyUri = new ProxyUriManager.ProxyUri(gadget, requestedResource); proxyUri.setHtmlTagContext(proxiedUri.getQueryParameter( UriCommon.Param.HTML_TAG_CONTEXT.getKey())); return proxyUri; }
private void verifyChainedUri(Uri orig, Uri uri, boolean debug, boolean noCache, String version, boolean endOfPath, String host, String path) throws Exception { // Make sure the manager can parse out results. DefaultProxyUriManager manager = makeManager(host, path, null); ProxyUri proxyUri = manager.process(uri); assertEquals(orig, proxyUri.getResource()); assertEquals(debug, proxyUri.isDebug()); assertEquals(noCache, proxyUri.isNoCache()); assertEquals(noCache ? 0 : 123, (int)proxyUri.getRefresh()); assertEquals(CONTAINER, proxyUri.getContainer()); assertEquals(SPEC_URI.toString(), proxyUri.getGadget()); // URI should end with the proxied content. String uriStr = uri.toString(); assertTrue(uriStr.endsWith(orig.toString())); int proxyEnd = uriStr.indexOf("/proxy/") + "/proxy/".length(); String paramsUri = uriStr.substring( proxyEnd, (endOfPath ? uriStr.indexOf('/', proxyEnd) : uriStr.indexOf("/path"))); uri = new UriBuilder().setQuery(paramsUri).toUri(); // "Raw" query param verification. assertEquals(noCache ? null : "123", uri.getQueryParameter(Param.REFRESH.getKey())); if (version != null) { assertEquals(version, uri.getQueryParameter(Param.VERSION.getKey())); } }
private void verifyChainedUri(Uri orig, Uri uri, boolean debug, boolean noCache, String version, boolean endOfPath, String host, String path) throws Exception { // Make sure the manager can parse out results. DefaultProxyUriManager manager = makeManager(host, path, null); ProxyUri proxyUri = manager.process(uri); assertEquals(orig, proxyUri.getResource()); assertEquals(debug, proxyUri.isDebug()); assertEquals(noCache, proxyUri.isNoCache()); assertEquals(noCache ? 0 : 123, (int)proxyUri.getRefresh()); assertEquals(CONTAINER, proxyUri.getContainer()); assertEquals(SPEC_URI.toString(), proxyUri.getGadget()); // URI should end with the proxied content. String uriStr = uri.toString(); assertTrue(uriStr.endsWith(orig.toString())); int proxyEnd = uriStr.indexOf("/proxy/") + "/proxy/".length(); String paramsUri = uriStr.substring( proxyEnd, (endOfPath ? uriStr.indexOf('/', proxyEnd) : uriStr.indexOf("/path"))); uri = new UriBuilder().setQuery(paramsUri).toUri(); // "Raw" query param verification. assertEquals(noCache ? null : "123", uri.getQueryParameter(Param.REFRESH.getKey())); if (version != null) { assertEquals(version, uri.getQueryParameter(Param.VERSION.getKey())); } }
private void verifyChainedUri(Uri orig, Uri uri, boolean debug, boolean noCache, String version, boolean endOfPath, String host, String path) throws Exception { // Make sure the manager can parse out results. DefaultProxyUriManager manager = makeManager(host, path, null); ProxyUri proxyUri = manager.process(uri); assertEquals(orig, proxyUri.getResource()); assertEquals(debug, proxyUri.isDebug()); assertEquals(noCache, proxyUri.isNoCache()); assertEquals(noCache ? 0 : 123, (int)proxyUri.getRefresh()); assertEquals(CONTAINER, proxyUri.getContainer()); assertEquals(SPEC_URI.toString(), proxyUri.getGadget()); // URI should end with the proxied content. String uriStr = uri.toString(); assertTrue(uriStr.endsWith(orig.toString())); int proxyEnd = uriStr.indexOf("/proxy/") + "/proxy/".length(); String paramsUri = uriStr.substring( proxyEnd, (endOfPath ? uriStr.indexOf('/', proxyEnd) : uriStr.indexOf("/path"))); uri = new UriBuilder().setQuery(paramsUri).toUri(); // "Raw" query param verification. assertEquals(noCache ? null : "123", uri.getQueryParameter(Param.REFRESH.getKey())); if (version != null) { assertEquals(version, uri.getQueryParameter(Param.VERSION.getKey())); } }