Skip to content

Commit

Permalink
Replace client side HTTP requests using the hostname that is in the a…
Browse files Browse the repository at this point in the history
…ddress bar

Signed-off-by: Mario Loriedo <mloriedo@redhat.com>
  • Loading branch information
l0rd committed Aug 1, 2016
1 parent 3022234 commit bd3daa6
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 23 deletions.
15 changes: 14 additions & 1 deletion dashboard/src/app/ide/ide.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ class IdeSvc {
while (i < links.length) {
let link = links[i];
if (link.rel === name) {
return link.href;
return this.fixHostName(link.href);
}
i++;
}
Expand All @@ -320,6 +320,19 @@ class IdeSvc {
updateRecentWorkspace(workspaceId) {
this.$rootScope.$broadcast('recent-workspace:set', workspaceId);
}

/**
* Replace the hostname in url with the hostname from the browser address bar
* @param url {String} URL to fix
* @returns {String}
*/
fixHostName(url) {
if (this.$location) {
return url.replace(new RegExp("(\://).+(?=\:)"), "$1" + this.$location.host());
} else {
return url;
}
}
}

export default IdeSvc;
37 changes: 27 additions & 10 deletions dashboard/src/components/api/che-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class CheProject {
/**
* Default constructor that is using resource
*/
constructor($resource, $q, cheWebsocket, wsagentPath) {
constructor($resource, $q, cheWebsocket, wsagentUrl, $location) {

// keep resource
this.$resource = $resource;
Expand All @@ -30,6 +30,8 @@ export class CheProject {

this.$q = $q;

this.$location = $location;

// project details map with key projectPath
this.projectDetailsMap = new Map();

Expand All @@ -39,16 +41,18 @@ export class CheProject {
// map of resolve per project <project:projectType> --> Source Estimation
this.resolveMap = new Map();

this.wsagentUrl = this.fixHostName(wsagentUrl);

// remote call
this.remoteProjectsAPI = this.$resource(wsagentPath + '/project', {}, {
import: {method: 'POST', url: wsagentPath + '/project/import/:path'},
create: {method: 'POST', url: wsagentPath + '/project?name=:path'},
details: {method: 'GET', url: wsagentPath + '/project/:path'},
estimate: {method: 'GET', url: wsagentPath + '/project/estimate/:path?type=:type'},
rename: {method: 'POST', url: wsagentPath + '/project/rename/:path?name=:name'},
remove: {method: 'DELETE', url: wsagentPath + '/project/:path'},
resolve: {method: 'GET', url: wsagentPath + '/project/resolve/:path', isArray: true},
update: {method: 'PUT', url: wsagentPath + '/project/:path'}
this.remoteProjectsAPI = this.$resource(this.wsagentUrl + '/project', {}, {
import: {method: 'POST', url: this.wsagentUrl + '/project/import/:path'},
create: {method: 'POST', url: this.wsagentUrl + '/project?name=:path'},
details: {method: 'GET', url: this.wsagentUrl + '/project/:path'},
estimate: {method: 'GET', url: this.wsagentUrl + '/project/estimate/:path?type=:type'},
rename: {method: 'POST', url: this.wsagentUrl + '/project/rename/:path?name=:name'},
remove: {method: 'DELETE', url: this.wsagentUrl + '/project/:path'},
resolve: {method: 'GET', url: this.wsagentUrl + '/project/resolve/:path', isArray: true},
update: {method: 'PUT', url: this.wsagentUrl + '/project/:path'}
});
}

Expand Down Expand Up @@ -201,4 +205,17 @@ export class CheProject {
return this.resolveMap.get(projectName);
}

/**
* Replace the hostname in url with the hostname from the browser address bar
* @param url {String} URL to fix
* @returns {String}
*/
fixHostName(url) {
if (this.$location) {
return url.replace(new RegExp("(\://).+(?=\:)"), "$1" + this.$location.host());
} else {
return url;
}
}

}
22 changes: 18 additions & 4 deletions dashboard/src/components/api/che-workspace-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ export class CheWorkspaceAgent {
/**
* Default constructor that is using resource
*/
constructor($resource, $q, cheWebsocket, workspaceAgentData) {
constructor($resource, $q, cheWebsocket, workspaceAgentData, $location) {
this.$resource = $resource;
this.$location = $location;
this.workspaceAgentData = workspaceAgentData;

this.project = new CheProject($resource, $q, cheWebsocket, this.workspaceAgentData.path);
this.git = new CheGit($resource, this.workspaceAgentData.path);
this.projectType = new CheProjectType($resource, $q, this.workspaceAgentData.path);
this.project = new CheProject($resource, $q, cheWebsocket, this.fixHostName(this.workspaceAgentData.path), this.$location);
this.git = new CheGit($resource, this.fixHostName(this.workspaceAgentData.path));
this.projectType = new CheProjectType($resource, $q, this.fixHostName(this.workspaceAgentData.path));
}

getProject() {
Expand All @@ -43,4 +44,17 @@ export class CheWorkspaceAgent {
getProjectType() {
return this.projectType;
}

/**
* Replace the hostname in url with the hostname from the browser address bar
* @param url {String} URL to fix
* @returns {String}
*/
fixHostName(url) {
if (this.$location) {
return url.replace(new RegExp("(\://).+(?=\:)"), "$1" + this.$location.host());
} else {
return url;
}
}
}
20 changes: 17 additions & 3 deletions dashboard/src/components/api/che-workspace.factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ export class CheWorkspace {
* Default constructor that is using resource
* @ngInject for Dependency injection
*/
constructor ($resource, $q, cheWebsocket, lodash) {
constructor ($resource, $q, $location, cheWebsocket, lodash) {
// keep resource
this.$resource = $resource;
this.$q = $q;
this.$location = $location;
this.lodash = lodash;
this.cheWebsocket = cheWebsocket;

Expand Down Expand Up @@ -78,7 +79,7 @@ export class CheWorkspace {
}

let workspaceAgentData = {path : wsAgentLink.href};
let wsagent = new CheWorkspaceAgent(this.$resource, this.$q, this.cheWebsocket, workspaceAgentData);
let wsagent = new CheWorkspaceAgent(this.$resource, this.$q, this.cheWebsocket, workspaceAgentData, this.$location);
this.workspaceAgents.set(workspaceId, wsagent);
return wsagent;
}
Expand Down Expand Up @@ -409,7 +410,7 @@ export class CheWorkspace {
return '';
}
let websocketLink = this.lodash.find(workspace.runtime.devMachine.links, l => l.rel === "wsagent.websocket");
return websocketLink ? websocketLink.href : '';
return websocketLink ? this.fixHostName(websocketLink.href) : '';
}

getIdeUrl(namespace, workspaceName) {
Expand Down Expand Up @@ -470,4 +471,17 @@ export class CheWorkspace {
return this.remoteWorkspaceAPI.createSnapshot({workspaceId : workspaceId}, {}).$promise;
}

/**
* Replace the hostname in url with the hostname from the browser address bar
* @param url {String} URL to fix
* @returns {String}
*/
fixHostName(url) {
if (this.$location) {
return url.replace(new RegExp("(\://).+(?=\:)"), "$1" + this.$location.host());
} else {
return url;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.eclipse.che.api.core.model.machine.Machine;
import org.eclipse.che.api.core.model.machine.Server;
import org.eclipse.che.api.machine.shared.Constants;
import org.eclipse.che.ide.util.UrlUtils;
import org.eclipse.che.ide.util.loging.Log;

import javax.validation.constraints.NotNull;
Expand Down Expand Up @@ -68,7 +69,7 @@ public String getType() {
public String getWsAgentWebSocketUrl() {
for (Link link : devMachineLinks) {
if (Constants.WSAGENT_WEBSOCKET_REFERENCE.equals(link.getRel())) {
return link.getHref();
return UrlUtils.fixHostName(link.getHref());
}
}
//should not be
Expand All @@ -80,7 +81,7 @@ public String getWsAgentWebSocketUrl() {
public String getTerminalUrl() {
for (Link link : devMachineLinks) {
if (Constants.TERMINAL_REFERENCE.equals(link.getRel())) {
return link.getHref();
return UrlUtils.fixHostName(link.getHref());
}
}
//should not be
Expand All @@ -100,7 +101,7 @@ public String getWsAgentBaseUrl() {
if (url.endsWith("/")) {
url = url.substring(0, url.length() - 1);
}
return url;
return UrlUtils.fixHostName(url);
} else {
//should not be
String message = "Reference " + Constants.WSAGENT_REFERENCE + " not found in DevMachine description";
Expand Down Expand Up @@ -139,7 +140,7 @@ public List<Link> getDevMachineLinks() {
/** Returns address (protocol://host:port) of the Workspace Agent. */
public String getAddress() {
final DevMachineServer server = getServer(Constants.WSAGENT_REFERENCE);
return server.getProtocol() + "://" + server.getAddress();
return UrlUtils.fixHostName(server.getProtocol() + "://" + server.getAddress());
}

/** Returns {@link Machine descriptor} of the Workspace Agent. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*******************************************************************************
* Copyright (c) 2012-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.ide.util;

import com.google.gwt.core.client.GWT;
import com.google.gwt.regexp.shared.RegExp;
import com.google.gwt.user.client.Window;

public class UrlUtils {

public static String fixHostName(String internalUrl) {
if (GWT.isScript()) {
String hostnameInBrowser = Window.Location.getHostName();
return replaceHostNameInUrl(internalUrl, hostnameInBrowser);
} else {
return internalUrl;
}
}

protected static String replaceHostNameInUrl(String url, String newHostName) {
RegExp re = RegExp.compile("(\\://).+(?=[\\:])");
return re.replace(url, "$1" + newHostName);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*******************************************************************************
* Copyright (c) 2012-2016 Codenvy, S.A.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Codenvy, S.A. - initial API and implementation
*******************************************************************************/
package org.eclipse.che.ide.util;

import org.eclipse.che.ide.ui.loaders.initialization.LoaderPresenter;
import org.eclipse.che.ide.ui.loaders.initialization.LoaderView;
import org.eclipse.che.ide.ui.loaders.initialization.LoadingInfo;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.eclipse.che.ide.ui.loaders.initialization.LoaderPresenter.State.WORKING;
import static org.mockito.Matchers.anyList;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class UrlUtilsTest {

@Test
public void should_replaceHostName_when_hostname_is_an_IP_address() {
// Given
String hostnameInBrowser = "localhost";
String inputUrl = "http://192.58.16.2:8080/api/";

// When
String fixedUrl = UrlUtils.replaceHostNameInUrl(inputUrl, hostnameInBrowser);

// Then
Assert.assertEquals("http://localhost:8080/api/", fixedUrl);
}

@Test
public void should_replaceHostName_when_hostname_is_fqdn() {
// Given
String hostnameInBrowser = "localhost";
String inputUrl = "http://myhost.example.com:8080/api/";

// When
String fixedUrl = UrlUtils.replaceHostNameInUrl(inputUrl, hostnameInBrowser);

// Then
Assert.assertEquals("http://localhost:8080/api/", fixedUrl);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.che.ide.extension.machine.client.MachineLocalizationConstant;
import org.eclipse.che.ide.extension.machine.client.inject.factories.EntityFactory;
import org.eclipse.che.ide.extension.machine.client.perspective.widgets.machine.appliance.server.Server;
import org.eclipse.che.ide.util.UrlUtils;
import org.eclipse.che.ide.util.loging.Log;

import java.util.ArrayList;
Expand Down Expand Up @@ -112,7 +113,7 @@ public String getActiveTabName() {
public String getTerminalUrl() {
for (Link link : machineLinks) {
if (Constants.TERMINAL_REFERENCE.equals(link.getRel())) {
return link.getHref();
return UrlUtils.fixHostName(link.getHref());
}
}

Expand Down

0 comments on commit bd3daa6

Please sign in to comment.