Search in sources :

Example 1 with GitClientApplication

use of com.gitblit.models.GitClientApplication in project gitblit by gitblit.

the class GitblitManager method getClientApplications.

/**
	 * Returns the list of custom client applications to be used for the
	 * repository url panel;
	 *
	 * @return a collection of client applications
	 */
@Override
public Collection<GitClientApplication> getClientApplications() {
    // prefer user definitions, if they exist
    File userDefs = new File(runtimeManager.getBaseFolder(), "clientapps.json");
    if (userDefs.exists()) {
        Date lastModified = new Date(userDefs.lastModified());
        if (clientApplications.hasCurrent("user", lastModified)) {
            return clientApplications.getObject("user");
        } else {
            // (re)load user definitions
            try {
                InputStream is = new FileInputStream(userDefs);
                Collection<GitClientApplication> clients = readClientApplications(is);
                is.close();
                if (clients != null) {
                    clientApplications.updateObject("user", lastModified, clients);
                    return clients;
                }
            } catch (IOException e) {
                logger.error("Failed to deserialize " + userDefs.getAbsolutePath(), e);
            }
        }
    }
    // no user definitions, use system definitions
    if (!clientApplications.hasCurrent("system", new Date(0))) {
        try {
            InputStream is = GitblitManager.class.getResourceAsStream("/clientapps.json");
            Collection<GitClientApplication> clients = readClientApplications(is);
            is.close();
            if (clients != null) {
                clientApplications.updateObject("system", new Date(0), clients);
            }
        } catch (IOException e) {
            logger.error("Failed to deserialize clientapps.json resource!", e);
        }
    }
    return clientApplications.getObject("system");
}
Also used : GitClientApplication(com.gitblit.models.GitClientApplication) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JsonIOException(com.google.gson.JsonIOException) IOException(java.io.IOException) File(java.io.File) Date(java.util.Date) FileInputStream(java.io.FileInputStream)

Example 2 with GitClientApplication

use of com.gitblit.models.GitClientApplication in project gitblit by gitblit.

the class RepositoryUrlPanel method createApplicationMenus.

protected Fragment createApplicationMenus(String wicketId, final UserModel user, final RepositoryModel repository, final List<RepositoryUrl> repositoryUrls) {
    final List<GitClientApplication> displayedApps = new ArrayList<GitClientApplication>();
    final String userAgent = ((WebClientInfo) GitBlitWebSession.get().getClientInfo()).getUserAgent();
    if (user.canClone(repository)) {
        for (GitClientApplication app : app().gitblit().getClientApplications()) {
            if (app.isActive && app.allowsPlatform(userAgent)) {
                displayedApps.add(app);
            }
        }
    }
    final String baseURL = WicketUtils.getGitblitURL(RequestCycle.get().getRequest());
    ListDataProvider<GitClientApplication> displayedAppsDp = new ListDataProvider<GitClientApplication>(displayedApps);
    DataView<GitClientApplication> appMenus = new DataView<GitClientApplication>("appMenus", displayedAppsDp) {

        private static final long serialVersionUID = 1L;

        @Override
        public void populateItem(final Item<GitClientApplication> item) {
            final GitClientApplication clientApp = item.getModelObject();
            // filter the urls for the client app
            List<RepositoryUrl> urls = new ArrayList<RepositoryUrl>();
            for (RepositoryUrl repoUrl : repositoryUrls) {
                if (clientApp.minimumPermission == null || !repoUrl.hasPermission()) {
                    // no minimum permission or untracked permissions, assume it is satisfactory
                    if (clientApp.supportsTransport(repoUrl.url)) {
                        urls.add(repoUrl);
                    }
                } else if (repoUrl.permission.atLeast(clientApp.minimumPermission)) {
                    // repo url meets minimum permission requirement
                    if (clientApp.supportsTransport(repoUrl.url)) {
                        urls.add(repoUrl);
                    }
                }
            }
            if (urls.size() == 0) {
                // do not show this app menu because there are no urls
                item.add(new Label("appMenu").setVisible(false));
                return;
            }
            Fragment appMenu = new Fragment("appMenu", "appMenuFragment", this);
            appMenu.setRenderBodyOnly(true);
            item.add(appMenu);
            // menu button
            appMenu.add(new Label("applicationName", clientApp.name));
            // application icon
            Component img;
            if (StringUtils.isEmpty(clientApp.icon)) {
                img = WicketUtils.newClearPixel("applicationIcon").setVisible(false);
            } else {
                if (clientApp.icon.contains("://")) {
                    // external image
                    img = new ExternalImage("applicationIcon", clientApp.icon);
                } else {
                    // context image
                    img = WicketUtils.newImage("applicationIcon", clientApp.icon);
                }
            }
            appMenu.add(img);
            // application menu title, may be a link
            if (StringUtils.isEmpty(clientApp.productUrl)) {
                appMenu.add(new Label("applicationTitle", clientApp.toString()));
            } else {
                appMenu.add(new LinkPanel("applicationTitle", null, clientApp.toString(), clientApp.productUrl, true));
            }
            // brief application description
            if (StringUtils.isEmpty(clientApp.description)) {
                appMenu.add(new Label("applicationDescription").setVisible(false));
            } else {
                appMenu.add(new Label("applicationDescription", clientApp.description));
            }
            // brief application legal info, copyright, license, etc
            if (StringUtils.isEmpty(clientApp.legal)) {
                appMenu.add(new Label("applicationLegal").setVisible(false));
            } else {
                appMenu.add(new Label("applicationLegal", clientApp.legal));
            }
            // a nested repeater for all action items
            ListDataProvider<RepositoryUrl> urlsDp = new ListDataProvider<RepositoryUrl>(urls);
            DataView<RepositoryUrl> actionItems = new DataView<RepositoryUrl>("actionItems", urlsDp) {

                private static final long serialVersionUID = 1L;

                @Override
                public void populateItem(final Item<RepositoryUrl> repoLinkItem) {
                    RepositoryUrl repoUrl = repoLinkItem.getModelObject();
                    Fragment fragment = new Fragment("actionItem", "actionFragment", this);
                    fragment.add(createPermissionBadge("permission", repoUrl));
                    if (!StringUtils.isEmpty(clientApp.cloneUrl)) {
                        // custom registered url
                        String url = substitute(clientApp.cloneUrl, repoUrl.url, baseURL, user.username, repository.name);
                        fragment.add(new LinkPanel("content", "applicationMenuItem", getString("gb.clone") + " " + repoUrl.url, url));
                        repoLinkItem.add(fragment);
                        fragment.add(new Label("copyFunction").setVisible(false));
                    } else if (!StringUtils.isEmpty(clientApp.command)) {
                        // command-line
                        String command = substitute(clientApp.command, repoUrl.url, baseURL, user.username, repository.name);
                        Label content = new Label("content", command);
                        WicketUtils.setCssClass(content, "commandMenuItem");
                        fragment.add(content);
                        repoLinkItem.add(fragment);
                        // copy function for command
                        fragment.add(createCopyFragment(command));
                    }
                }
            };
            appMenu.add(actionItems);
        }
    };
    Fragment applicationMenus = new Fragment(wicketId, "applicationMenusFragment", this);
    applicationMenus.add(appMenus);
    return applicationMenus;
}
Also used : WebClientInfo(org.apache.wicket.protocol.http.request.WebClientInfo) ListDataProvider(org.apache.wicket.markup.repeater.data.ListDataProvider) GitClientApplication(com.gitblit.models.GitClientApplication) ArrayList(java.util.ArrayList) Label(org.apache.wicket.markup.html.basic.Label) RepositoryUrl(com.gitblit.models.RepositoryUrl) Fragment(org.apache.wicket.markup.html.panel.Fragment) DataView(org.apache.wicket.markup.repeater.data.DataView) Item(org.apache.wicket.markup.repeater.Item) ExternalImage(com.gitblit.wicket.ExternalImage) Component(org.apache.wicket.Component)

Example 3 with GitClientApplication

use of com.gitblit.models.GitClientApplication in project gitblit by gitblit.

the class GitblitManager method readClientApplications.

private Collection<GitClientApplication> readClientApplications(InputStream is) {
    try {
        Type type = new TypeToken<Collection<GitClientApplication>>() {
        }.getType();
        InputStreamReader reader = new InputStreamReader(is);
        Gson gson = JsonUtils.gson();
        Collection<GitClientApplication> links = gson.fromJson(reader, type);
        return links;
    } catch (JsonIOException e) {
        logger.error("Error deserializing client applications!", e);
    } catch (JsonSyntaxException e) {
        logger.error("Error deserializing client applications!", e);
    }
    return null;
}
Also used : Type(java.lang.reflect.Type) JsonSyntaxException(com.google.gson.JsonSyntaxException) InputStreamReader(java.io.InputStreamReader) JsonIOException(com.google.gson.JsonIOException) GitClientApplication(com.gitblit.models.GitClientApplication) Collection(java.util.Collection) Gson(com.google.gson.Gson)

Aggregations

GitClientApplication (com.gitblit.models.GitClientApplication)3 JsonIOException (com.google.gson.JsonIOException)2 RepositoryUrl (com.gitblit.models.RepositoryUrl)1 ExternalImage (com.gitblit.wicket.ExternalImage)1 Gson (com.google.gson.Gson)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Date (java.util.Date)1 Component (org.apache.wicket.Component)1 Label (org.apache.wicket.markup.html.basic.Label)1 Fragment (org.apache.wicket.markup.html.panel.Fragment)1 Item (org.apache.wicket.markup.repeater.Item)1 DataView (org.apache.wicket.markup.repeater.data.DataView)1