Skip to content

Commit

Permalink
Rework Navigate to File feature to avoid using Everrest based Websock…
Browse files Browse the repository at this point in the history
…et calls (#5561)
  • Loading branch information
Dmytro Kulieshov committed Jul 11, 2017
1 parent 599bd6b commit 4e63b84
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,20 @@
import com.google.gwt.http.client.URL;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.web.bindery.event.shared.EventBus;

import org.eclipse.che.api.project.shared.dto.ItemReference;
import org.eclipse.che.api.core.jsonrpc.commons.RequestTransmitter;
import org.eclipse.che.api.project.shared.dto.ProjectSearchRequestDto;
import org.eclipse.che.api.project.shared.dto.ProjectSearchResponseDto;
import org.eclipse.che.api.promises.client.Operation;
import org.eclipse.che.api.promises.client.OperationException;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.editor.EditorAgent;
import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent;
import org.eclipse.che.ide.api.machine.events.WsAgentStateHandler;
import org.eclipse.che.ide.api.resources.File;
import org.eclipse.che.ide.dto.DtoFactory;
import org.eclipse.che.ide.resource.Path;
import org.eclipse.che.ide.rest.DtoUnmarshallerFactory;
import org.eclipse.che.ide.util.loging.Log;
import org.eclipse.che.ide.websocket.Message;
import org.eclipse.che.ide.websocket.MessageBuilder;
import org.eclipse.che.ide.websocket.MessageBus;
import org.eclipse.che.ide.websocket.MessageBusProvider;
import org.eclipse.che.ide.websocket.WebSocketException;
import org.eclipse.che.ide.websocket.rest.RequestCallback;
import org.eclipse.che.ide.websocket.rest.Unmarshallable;

import java.util.ArrayList;
import java.util.List;

import static com.google.gwt.http.client.RequestBuilder.GET;
import static org.eclipse.che.ide.MimeType.APPLICATION_JSON;
import static org.eclipse.che.ide.rest.HTTPHeader.ACCEPT;
import static java.util.Collections.emptyList;

/**
* Presenter for file navigation (find file by name and open it).
Expand All @@ -50,43 +37,29 @@
* @author Vlad Zhukovskyi
*/
@Singleton
public class NavigateToFilePresenter implements NavigateToFileView.ActionDelegate, WsAgentStateHandler {
public class NavigateToFilePresenter implements NavigateToFileView.ActionDelegate {

private final MessageBusProvider messageBusProvider;
private final EditorAgent editorAgent;
private final DtoUnmarshallerFactory dtoUnmarshallerFactory;
private final NavigateToFileView view;
private final AppContext appContext;
private final EditorAgent editorAgent;
private final RequestTransmitter requestTransmitter;
private final DtoFactory dtoFactory;
private final NavigateToFileView view;
private final AppContext appContext;

private String SEARCH_URL;
private MessageBus wsMessageBus;

@Inject
public NavigateToFilePresenter(NavigateToFileView view,
EventBus eventBus,
DtoUnmarshallerFactory dtoUnmarshallerFactory,
MessageBusProvider messageBusProvider,
AppContext appContext,
EditorAgent editorAgent) {
EditorAgent editorAgent,
RequestTransmitter requestTransmitter,
DtoFactory dtoFactory) {
this.view = view;
this.appContext = appContext;
this.dtoUnmarshallerFactory = dtoUnmarshallerFactory;
this.messageBusProvider = messageBusProvider;
this.editorAgent = editorAgent;
this.requestTransmitter = requestTransmitter;
this.dtoFactory = dtoFactory;

this.view.setDelegate(this);

eventBus.addHandler(WsAgentStateEvent.TYPE, this);
}

@Override
public void onWsAgentStarted(WsAgentStateEvent event) {
wsMessageBus = messageBusProvider.getMachineMessageBus();
SEARCH_URL = "/project/search";
}

@Override
public void onWsAgentStopped(WsAgentStateEvent event) {
}

/** Show dialog with view for navigation. */
Expand All @@ -111,29 +84,23 @@ public void apply(Optional<File> optFile) throws OperationException {
@Override
public void onFileNameChanged(String fileName) {
if (fileName.isEmpty()) {
view.showItems(new ArrayList<ItemReference>());
view.showItems(emptyList());
return;
}

// add '*' to allow search files by first letters
final String url = SEARCH_URL + "/?name=" + URL.encodePathSegment(fileName + "*");
final Message message = new MessageBuilder(GET, url).header(ACCEPT, APPLICATION_JSON).build();
final Unmarshallable<List<ItemReference>> unmarshaller = dtoUnmarshallerFactory.newWSListUnmarshaller(ItemReference.class);
try {
wsMessageBus.send(message, new RequestCallback<List<ItemReference>>(unmarshaller) {
@Override
protected void onSuccess(List<ItemReference> result) {
view.showItems(result);
}
ProjectSearchRequestDto requestParams = dtoFactory.createDto(ProjectSearchRequestDto.class)
.withPath("")
.withName(URL.encodePathSegment(fileName + "*"));

requestTransmitter.newRequest()
.endpointId("ws-agent")
.methodName("project/search")
.paramsAsDto(requestParams)
.sendAndReceiveResultAsDto(ProjectSearchResponseDto.class, 20_000)
.onSuccess(response -> view.showItems(response.getItemReferences()))
.onFailure(error -> Log.error(getClass(), error.getMessage()))
.onTimeout(() -> Log.error(getClass(), "Project search request failed due timeout"));

@Override
protected void onFailure(Throwable exception) {
Log.error(getClass(), exception);
}
});
} catch (WebSocketException e) {
Log.error(getClass(), e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import com.google.common.base.Optional;
import com.google.web.bindery.event.shared.EventBus;

import org.eclipse.che.api.core.jsonrpc.commons.RequestTransmitter;
import org.eclipse.che.api.promises.client.Promise;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.editor.EditorAgent;
import org.eclipse.che.ide.api.machine.DevMachine;
import org.eclipse.che.ide.api.machine.events.WsAgentStateEvent;
import org.eclipse.che.api.promises.client.Promise;
import org.eclipse.che.ide.api.resources.Container;
import org.eclipse.che.ide.api.resources.File;
import org.eclipse.che.ide.dto.DtoFactory;
import org.eclipse.che.ide.resource.Path;
import org.eclipse.che.ide.rest.DtoUnmarshallerFactory;
import org.eclipse.che.ide.websocket.MessageBus;
Expand Down Expand Up @@ -66,6 +68,10 @@ public class NavigateToFilePresenterTest {
private AppContext appContext;
@Mock
private EditorAgent editorAgent;
@Mock
private DtoFactory dtoFactory;
@Mock
private RequestTransmitter requestTransmitter;

private NavigateToFilePresenter presenter;

Expand All @@ -79,13 +85,10 @@ public void setUp() {
when(messageBusProvider.getMachineMessageBus()).thenReturn(messageBus);

presenter = new NavigateToFilePresenter(view,
eventBus,
dtoUnmarshallerFactory,
messageBusProvider,
appContext,
editorAgent);

presenter.onWsAgentStarted(wsAgentStateEvent);
editorAgent,
requestTransmitter,
dtoFactory);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*******************************************************************************
* Copyright (c) 2012-2017 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.api.project.shared.dto;

import org.eclipse.che.dto.shared.DTO;

@DTO
public interface ProjectSearchRequestDto {
String getPath();

ProjectSearchRequestDto withPath(String path);

String getName();

ProjectSearchRequestDto withName(String name);

String getText();

ProjectSearchRequestDto withText(String text);

int getMaxItems();

ProjectSearchRequestDto withMaxItems(int maxItems);

int getSkipCount();

ProjectSearchRequestDto withSkipCount(int skipCount);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*******************************************************************************
* Copyright (c) 2012-2017 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.api.project.shared.dto;

import org.eclipse.che.dto.shared.DTO;

import java.util.List;

@DTO
public interface ProjectSearchResponseDto {
List<ItemReference> getItemReferences();

ProjectSearchResponseDto withItemReferences(List<ItemReference> itemReferences);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import org.eclipse.che.api.core.NotFoundException;
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.core.UnauthorizedException;
import org.eclipse.che.api.core.jsonrpc.commons.JsonRpcException;
import org.eclipse.che.api.core.jsonrpc.commons.RequestHandlerConfigurator;
import org.eclipse.che.api.core.jsonrpc.commons.RequestTransmitter;
import org.eclipse.che.api.core.model.project.type.Value;
import org.eclipse.che.api.core.notification.EventService;
Expand All @@ -40,6 +42,8 @@
import org.eclipse.che.api.project.shared.dto.CopyOptions;
import org.eclipse.che.api.project.shared.dto.ItemReference;
import org.eclipse.che.api.project.shared.dto.MoveOptions;
import org.eclipse.che.api.project.shared.dto.ProjectSearchRequestDto;
import org.eclipse.che.api.project.shared.dto.ProjectSearchResponseDto;
import org.eclipse.che.api.project.shared.dto.SourceEstimation;
import org.eclipse.che.api.project.shared.dto.TreeElement;
import org.eclipse.che.api.vfs.VirtualFile;
Expand Down Expand Up @@ -922,6 +926,29 @@ public List<ItemReference> search(@ApiParam(value = "Path to resource, i.e. wher
return items;
}

@Inject
private void configureProjectSearchRequestHandler(RequestHandlerConfigurator requestHandlerConfigurator){
requestHandlerConfigurator.newConfiguration()
.methodName("project/search")
.paramsAsDto(ProjectSearchRequestDto.class)
.resultAsDto(ProjectSearchResponseDto.class)
.withFunction(this::search);
}

public ProjectSearchResponseDto search(ProjectSearchRequestDto request) {
String path = request.getPath();
String name = request.getName();
String text = request.getText();
int maxItems = request.getMaxItems();
int skipCount = request.getSkipCount();

try {
return newDto(ProjectSearchResponseDto.class).withItemReferences(search(path, name, text, maxItems, skipCount));
} catch (ServerException | ConflictException | NotFoundException | ForbiddenException e) {
throw new JsonRpcException(-27000, e.getMessage());
}
}

private void logProjectCreatedEvent(@NotNull String projectName, @NotNull String projectType) {
LOG.info("EVENT#project-created# PROJECT#{}# TYPE#{}# WS#{}# USER#{}# PAAS#default#",
projectName,
Expand Down

0 comments on commit 4e63b84

Please sign in to comment.