Search in sources :

Example 1 with AccountApi

use of com.google.gerrit.extensions.api.accounts.AccountApi in project gerrit by GerritCodeReview.

the class IndexHtmlUtil method dynamicTemplateData.

/**
 * Returns dynamic parameters of {@code index.html}.
 */
public static ImmutableMap<String, Object> dynamicTemplateData(GerritApi gerritApi, String requestedURL) throws RestApiException, URISyntaxException {
    ImmutableMap.Builder<String, Object> data = ImmutableMap.builder();
    Map<String, SanitizedContent> initialData = new HashMap<>();
    Server serverApi = gerritApi.config().server();
    initialData.put("\"/config/server/info\"", serializeObject(GSON, serverApi.getInfo()));
    initialData.put("\"/config/server/version\"", serializeObject(GSON, serverApi.getVersion()));
    initialData.put("\"/config/server/top-menus\"", serializeObject(GSON, serverApi.topMenus()));
    String requestedPath = IndexPreloadingUtil.getPath(requestedURL);
    IndexPreloadingUtil.RequestedPage page = IndexPreloadingUtil.parseRequestedPage(requestedPath);
    switch(page) {
        case CHANGE:
            data.put("defaultChangeDetailHex", ListOption.toHex(IndexPreloadingUtil.CHANGE_DETAIL_OPTIONS));
            data.put("changeRequestsPath", IndexPreloadingUtil.computeChangeRequestsPath(requestedPath, page).get());
            break;
        case DIFF:
            data.put("defaultChangeDetailHex", ListOption.toHex(IndexPreloadingUtil.CHANGE_DETAIL_OPTIONS));
            data.put("changeRequestsPath", IndexPreloadingUtil.computeChangeRequestsPath(requestedPath, page).get());
            break;
        case DASHBOARD:
        // Dashboard is preloaded queries are added later when we check user is authenticated.
        case PAGE_WITHOUT_PRELOADING:
            break;
    }
    try {
        AccountApi accountApi = gerritApi.accounts().self();
        initialData.put("\"/accounts/self/detail\"", serializeObject(GSON, accountApi.get()));
        initialData.put("\"/accounts/self/preferences\"", serializeObject(GSON, accountApi.getPreferences()));
        initialData.put("\"/accounts/self/preferences.diff\"", serializeObject(GSON, accountApi.getDiffPreferences()));
        initialData.put("\"/accounts/self/preferences.edit\"", serializeObject(GSON, accountApi.getEditPreferences()));
        data.put("userIsAuthenticated", true);
        if (page == RequestedPage.DASHBOARD) {
            data.put("defaultDashboardHex", ListOption.toHex(IndexPreloadingUtil.DASHBOARD_OPTIONS));
            data.put("dashboardQuery", IndexPreloadingUtil.computeDashboardQueryList(serverApi));
        }
    } catch (AuthException e) {
        logger.atFine().log("Can't inline account-related data because user is unauthenticated");
    // Don't render data
    }
    data.put("gerritInitialData", initialData);
    return data.build();
}
Also used : AccountApi(com.google.gerrit.extensions.api.accounts.AccountApi) Server(com.google.gerrit.extensions.api.config.Server) RequestedPage(com.google.gerrit.httpd.raw.IndexPreloadingUtil.RequestedPage) HashMap(java.util.HashMap) SanitizedContent(com.google.template.soy.data.SanitizedContent) AuthException(com.google.gerrit.extensions.restapi.AuthException) GsonOrdainer.serializeObject(com.google.template.soy.data.ordainers.GsonOrdainer.serializeObject) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 AccountApi (com.google.gerrit.extensions.api.accounts.AccountApi)1 Server (com.google.gerrit.extensions.api.config.Server)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1 RequestedPage (com.google.gerrit.httpd.raw.IndexPreloadingUtil.RequestedPage)1 SanitizedContent (com.google.template.soy.data.SanitizedContent)1 GsonOrdainer.serializeObject (com.google.template.soy.data.ordainers.GsonOrdainer.serializeObject)1 HashMap (java.util.HashMap)1