Index: hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/main/java/org/apache/hadoop/yarn/server/webproxy/WebAppProxyServlet.java =================================================================== --- hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/main/java/org/apache/hadoop/yarn/server/webproxy/WebAppProxyServlet.java (revision 1305743) +++ hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/main/java/org/apache/hadoop/yarn/server/webproxy/WebAppProxyServlet.java (working copy) @@ -272,19 +272,14 @@ req.getQueryString(), true), runningUser, id); return; } - - URI toFetch = new URI(req.getScheme(), - trackingUri.getAuthority(), - StringHelper.ujoin(trackingUri.getPath(), rest), req.getQueryString(), - null); - - LOG.info(req.getRemoteUser()+" is accessing unchecked "+toFetch+ - " which is the app master GUI of "+appId+" owned by "+runningUser); + URI toFetch; switch(applicationReport.getYarnApplicationState()) { case KILLED: case FINISHED: case FAILED: + toFetch = logAndGetFetchURI(req, appId, trackingUri, runningUser, + trackingUri.getPath()); resp.sendRedirect(resp.encodeRedirectURL(toFetch.toString())); return; } @@ -292,10 +287,23 @@ if(userWasWarned && userApproved) { c = makeCheckCookie(id, true); } + String path = StringHelper.ujoin(trackingUri.getPath(), rest); + toFetch = logAndGetFetchURI(req, appId, trackingUri, runningUser, path); proxyLink(req, resp, toFetch, c, getProxyHost()); } catch(URISyntaxException e) { throw new IOException(e); } } + + private URI logAndGetFetchURI(HttpServletRequest req, String appId, + URI trackingUri, String runningUser, String path) + throws URISyntaxException { + URI toFetch = new URI(req.getScheme(), trackingUri.getAuthority(), path, + req.getQueryString(), null); + LOG.info(req.getRemoteUser() + " is accessing unchecked " + toFetch + + " which is the app master GUI of " + appId + " owned by " + + runningUser); + return toFetch; + } } Index: hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/test/java/org/apache/hadoop/yarn/server/webproxy/TestWebAppProxyServlet.java =================================================================== --- hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/test/java/org/apache/hadoop/yarn/server/webproxy/TestWebAppProxyServlet.java (revision 0) +++ hadoop-mapreduce-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-web-proxy/src/test/java/org/apache/hadoop/yarn/server/webproxy/TestWebAppProxyServlet.java (revision 0) @@ -0,0 +1,86 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * + * http://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. + */ +package org.apache.hadoop.yarn.server.webproxy; + +import java.io.IOException; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.hadoop.yarn.api.records.ApplicationId; +import org.apache.hadoop.yarn.api.records.ApplicationReport; +import org.apache.hadoop.yarn.api.records.YarnApplicationState; +import org.apache.hadoop.yarn.factories.RecordFactory; +import org.apache.hadoop.yarn.factory.providers.RecordFactoryProvider; +import org.junit.Test; +import org.mockito.Mockito; + +public class TestWebAppProxyServlet { + String originalTrackingUrl = "0.0.0.0:19888/jobhistory/job/job_1332742374703_0001"; + String redirectURL = "http://" + originalTrackingUrl; + String pathInfo = "/application_1332742374703_0001/jobhistory/job/job_1332742374703_0001"; + RecordFactory recordFactory = RecordFactoryProvider.getRecordFactory(null); + + @Test + public void testDoGet() throws IOException, ServletException { + WebAppProxyServlet webAppProxyServlet = new WebAppProxyServlet(); + ServletConfig config = Mockito.mock(ServletConfig.class); + ServletContext context = Mockito.mock(ServletContext.class); + Mockito.doReturn(context).when(config).getServletContext(); + AppReportFetcher appReportFetcher = Mockito.mock(AppReportFetcher.class); + Mockito.doReturn(appReportFetcher).when(context).getAttribute( + WebAppProxy.FETCHER_ATTRIBUTE); + Mockito.doReturn(getApplicationReport()).when(appReportFetcher) + .getApplicationReport(getApplicationId()); + webAppProxyServlet.init(config); + HttpServletResponse resp = mockAndGetResp(); + webAppProxyServlet.doGet(mockAndGetReq(), resp); + Mockito.verify(resp, Mockito.atLeastOnce()).sendRedirect(redirectURL); + } + + private HttpServletResponse mockAndGetResp() { + HttpServletResponse resp = Mockito.mock(HttpServletResponse.class); + Mockito.doReturn(redirectURL).when(resp).encodeRedirectURL(redirectURL); + return resp; + } + + private HttpServletRequest mockAndGetReq() { + HttpServletRequest req = Mockito.mock(HttpServletRequest.class); + Mockito.doReturn(pathInfo).when(req).getPathInfo(); + Mockito.doReturn("http").when(req).getScheme(); + return req; + } + + private ApplicationId getApplicationId() { + ApplicationId appId = recordFactory.newRecordInstance(ApplicationId.class); + appId.setClusterTimestamp(1332742374703l); + appId.setId(0001); + return appId; + } + + private ApplicationReport getApplicationReport() { + ApplicationReport applicationReport = recordFactory + .newRecordInstance(ApplicationReport.class); + applicationReport.setOriginalTrackingUrl(originalTrackingUrl); + applicationReport.setYarnApplicationState(YarnApplicationState.FINISHED); + return applicationReport; + } +}