Search in sources :

Example 1 with AbortWithHttpErrorCodeException

use of org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException in project hale by halestudio.

the class EditTemplatePage method addControls.

@Override
protected void addControls() {
    StringValue idParam = getPageParameters().get(0);
    if (!idParam.isNull() && !idParam.isEmpty()) {
        String templateId = idParam.toString();
        OrientGraph graph = DatabaseHelper.getGraph();
        try {
            Template template = null;
            try {
                template = Template.getByTemplateId(graph, templateId);
            } catch (NonUniqueResultException e) {
                log.error("Duplicate template representation: " + templateId, e);
            }
            if (template != null) {
                // get associated user
                Vertex v = template.getV();
                Iterator<Vertex> owners = v.getVertices(Direction.OUT, "owner").iterator();
                if (// user is admin
                UserUtil.isAdmin() || // or user is owner
                (owners.hasNext() && UserUtil.getLogin().equals(new User(owners.next(), graph).getLogin()))) {
                    add(new TemplateForm("edit-form", false, templateId));
                } else {
                    throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_FORBIDDEN);
                }
            } else {
                throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, "Template not found.");
            }
        } finally {
            graph.shutdown();
        }
    } else
        throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_BAD_REQUEST, "Template identifier must be specified.");
}
Also used : NonUniqueResultException(eu.esdihumboldt.util.blueprints.entities.NonUniqueResultException) Vertex(com.tinkerpop.blueprints.Vertex) User(eu.esdihumboldt.hale.server.model.User) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) TemplateForm(eu.esdihumboldt.hale.server.templates.war.components.TemplateForm) AbortWithHttpErrorCodeException(org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException) StringValue(org.apache.wicket.util.string.StringValue) Template(eu.esdihumboldt.hale.server.model.Template)

Example 2 with AbortWithHttpErrorCodeException

use of org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException in project wicket by apache.

the class ServletWebRequestTest method getClientUrlAjaxWithoutBaseUrl.

/**
 * Assert that ServletWebRequest#getClientUrl() will throw an AbortWithHttpErrorCodeException
 * with error code 400 (Bad Request) when an Ajax request doesn't provide the base url.
 *
 * https://issues.apache.org/jira/browse/WICKET-4841
 */
@Test
public void getClientUrlAjaxWithoutBaseUrl() {
    MockHttpServletRequest httpRequest = new MockHttpServletRequest(null, null, null);
    httpRequest.setHeader(ServletWebRequest.HEADER_AJAX, "true");
    ServletWebRequest webRequest = new ServletWebRequest(httpRequest, "");
    try {
        webRequest.getClientUrl();
        fail("Should not be possible to get the request client url in Ajax request without base url");
    } catch (AbortWithHttpErrorCodeException awhex) {
        assertEquals(HttpServletResponse.SC_BAD_REQUEST, awhex.getErrorCode());
    }
}
Also used : MockHttpServletRequest(org.apache.wicket.protocol.http.mock.MockHttpServletRequest) AbortWithHttpErrorCodeException(org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException) Test(org.junit.Test)

Example 3 with AbortWithHttpErrorCodeException

use of org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException in project hale by halestudio.

the class TemplatePage method addControls.

@SuppressWarnings("serial")
@Override
protected void addControls(boolean loggedIn) {
    super.addControls(loggedIn);
    StringValue idParam = getPageParameters().get(0);
    if (!idParam.isNull() && !idParam.isEmpty()) {
        String templateId = idParam.toString();
        OrientGraph graph = DatabaseHelper.getGraph();
        try {
            Template template = null;
            try {
                template = Template.getByTemplateId(graph, templateId);
            } catch (NonUniqueResultException e) {
                log.error("Duplicate template representation: " + templateId, e);
            }
            if (template != null) {
                // name
                Label name = new Label("name", template.getName());
                add(name);
                // download
                String href = TemplateLocations.getTemplateDownloadUrl(templateId);
                ExternalLink download = new ExternalLink("download", href);
                add(download);
                // project location
                WebMarkupContainer project = new WebMarkupContainer("project");
                project.add(AttributeModifier.replace("value", TemplateLocations.getTemplateProjectUrl(scavenger, templateId)));
                add(project);
                // author
                Label author = new Label("author", template.getAuthor());
                add(author);
                // edit-buttons container
                WebMarkupContainer editButtons = new WebMarkupContainer("edit-buttons");
                editButtons.setVisible(false);
                add(editButtons);
                // deleteDialog container
                WebMarkupContainer deleteDialog = new WebMarkupContainer("deleteDialog");
                deleteDialog.setVisible(false);
                add(deleteDialog);
                // user
                String userName;
                Vertex v = template.getV();
                boolean showEditPart = UserUtil.isAdmin();
                Iterator<Vertex> owners = v.getVertices(Direction.OUT, "owner").iterator();
                if (owners.hasNext()) {
                    User user = new User(owners.next(), graph);
                    userName = UserUtil.getDisplayName(user);
                    showEditPart = loggedIn && UserUtil.getLogin().equals(user.getLogin());
                } else {
                    userName = "Unregistered user";
                }
                // edit buttons
                if (showEditPart) {
                    editButtons.setVisible(true);
                    deleteDialog.setVisible(true);
                    // edit
                    editButtons.add(new BookmarkablePageLink<>("edit", EditTemplatePage.class, new PageParameters().set(0, templateId)));
                    // update
                    editButtons.add(new BookmarkablePageLink<>("update", UpdateTemplatePage.class, new PageParameters().set(0, templateId)));
                    // delete
                    deleteDialog.add(new DeleteTemplateLink("delete", templateId));
                }
                Label user = new Label("user", userName);
                add(user);
                // description
                String descr = template.getDescription();
                String htmlDescr = null;
                if (descr == null || descr.isEmpty()) {
                    descr = "No description for the project template available.";
                } else {
                    // convert markdown to
                    htmlDescr = new PegDownProcessor(Extensions.AUTOLINKS | Extensions.SUPPRESS_ALL_HTML | Extensions.HARDWRAPS | Extensions.SMARTYPANTS).markdownToHtml(descr);
                }
                // description in pre
                Label description = new Label("description", descr);
                description.setVisible(htmlDescr == null);
                add(description);
                // description in div
                Label htmlDescription = new Label("html-description", htmlDescr);
                htmlDescription.setVisible(htmlDescr != null);
                htmlDescription.setEscapeModelStrings(false);
                add(htmlDescription);
                // invalid
                WebMarkupContainer statusInvalid = new WebMarkupContainer("invalid");
                statusInvalid.setVisible(!template.isValid());
                add(statusInvalid);
                // resources
                ResourcesPanel resources = new ResourcesPanel("resources", templateId);
                add(resources);
                // project popover
                WebMarkupContainer projectPopover = new WebMarkupContainer("project-popover");
                projectPopover.add(new HTMLPopoverBehavior(Model.of("Load template in HALE"), new PopoverConfig().withPlacement(Placement.bottom)) {

                    @Override
                    public Component newBodyComponent(String markupId) {
                        return new ProjectURLPopover(markupId);
                    }
                });
                add(projectPopover);
            } else {
                throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, "Template not found.");
            }
        } finally {
            graph.shutdown();
        }
    } else
        throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_BAD_REQUEST, "Template identifier must be specified.");
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) User(eu.esdihumboldt.hale.server.model.User) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) ResourcesPanel(eu.esdihumboldt.hale.server.templates.war.components.ResourcesPanel) Label(org.apache.wicket.markup.html.basic.Label) AbortWithHttpErrorCodeException(org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException) PopoverConfig(de.agilecoders.wicket.core.markup.html.bootstrap.components.PopoverConfig) WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer) Template(eu.esdihumboldt.hale.server.model.Template) DeleteTemplateLink(eu.esdihumboldt.hale.server.templates.war.components.DeleteTemplateLink) StringValue(org.apache.wicket.util.string.StringValue) Component(org.apache.wicket.Component) NonUniqueResultException(eu.esdihumboldt.util.blueprints.entities.NonUniqueResultException) ProjectURLPopover(eu.esdihumboldt.hale.server.templates.war.components.ProjectURLPopover) HTMLPopoverBehavior(eu.esdihumboldt.hale.server.webapp.components.bootstrap.HTMLPopoverBehavior) PageParameters(org.apache.wicket.request.mapper.parameter.PageParameters) ExternalLink(org.apache.wicket.markup.html.link.ExternalLink) PegDownProcessor(org.pegdown.PegDownProcessor)

Example 4 with AbortWithHttpErrorCodeException

use of org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException in project hale by halestudio.

the class UpdateTemplatePage method addControls.

@Override
protected void addControls() {
    StringValue idParam = getPageParameters().get(0);
    if (!idParam.isNull() && !idParam.isEmpty()) {
        String templateId = idParam.toString();
        OrientGraph graph = DatabaseHelper.getGraph();
        try {
            Template template = null;
            try {
                template = Template.getByTemplateId(graph, templateId);
            } catch (NonUniqueResultException e) {
                log.error("Duplicate template representation: " + templateId, e);
            }
            if (template != null) {
                // get associated user
                Vertex v = template.getV();
                Iterator<Vertex> owners = v.getVertices(Direction.OUT, "owner").iterator();
                if (// user is admin
                UserUtil.isAdmin() || // or user is owner
                (owners.hasNext() && UserUtil.getLogin().equals(new User(owners.next(), graph).getLogin()))) {
                    // template name
                    add(new Label("name", template.getName()));
                    // upload form
                    add(new TemplateUploadForm("upload-form", templateId));
                } else {
                    throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_FORBIDDEN);
                }
            } else {
                throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, "Template not found.");
            }
        } finally {
            graph.shutdown();
        }
    } else
        throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_BAD_REQUEST, "Template identifier must be specified.");
}
Also used : NonUniqueResultException(eu.esdihumboldt.util.blueprints.entities.NonUniqueResultException) Vertex(com.tinkerpop.blueprints.Vertex) User(eu.esdihumboldt.hale.server.model.User) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) TemplateUploadForm(eu.esdihumboldt.hale.server.templates.war.components.TemplateUploadForm) Label(org.apache.wicket.markup.html.basic.Label) AbortWithHttpErrorCodeException(org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException) StringValue(org.apache.wicket.util.string.StringValue) Template(eu.esdihumboldt.hale.server.model.Template)

Example 5 with AbortWithHttpErrorCodeException

use of org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException in project hale by halestudio.

the class StatusPage method addControls.

@Override
protected void addControls(boolean loggedIn) {
    super.addControls(loggedIn);
    final String workspaceId = getPageParameters().get(PARAMETER_WORKSPACE).toOptionalString();
    if (workspaceId == null || workspaceId.isEmpty()) {
        throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, "Workspace ID not specified.");
    }
    try {
        workspaces.getWorkspaceFolder(workspaceId);
    } catch (FileNotFoundException e) {
        throw new AbortWithHttpErrorCodeException(HttpServletResponse.SC_NOT_FOUND, "Workspace does not exist.");
    }
    final IModel<TransformationWorkspace> workspace = new LoadableDetachableModel<TransformationWorkspace>() {

        private static final long serialVersionUID = 2600444242247550094L;

        @Override
        protected TransformationWorkspace load() {
            return new TransformationWorkspace(workspaceId);
        }
    };
    // job panel
    final Serializable family = AbstractTransformationJob.createFamily(workspaceId);
    final JobPanel jobs = new JobPanel("jobs", family, true);
    add(jobs);
    // status
    final Label status = new Label("status", new LoadableDetachableModel<String>() {

        private static final long serialVersionUID = -4351763182104835300L;

        @Override
        protected String load() {
            if (workspace.getObject().isTransformationFinished()) {
                if (workspace.getObject().isTransformationSuccessful()) {
                    return "Transformation completed.";
                } else {
                    return "Transformation failed.";
                }
            } else {
                if (Job.getJobManager().find(family).length > 0) {
                    return "Transformation is running:";
                } else {
                    return "No transformation running.";
                }
            }
        }
    });
    status.setOutputMarkupId(true);
    add(status);
    // result
    final WebMarkupContainer result = new WebMarkupContainer("result");
    result.setOutputMarkupId(true);
    add(result);
    final WebMarkupContainer update = new WebMarkupContainer("update") {

        private static final long serialVersionUID = -2591480922683644827L;

        @Override
        public boolean isVisible() {
            return workspace.getObject().isTransformationFinished();
        }
    };
    result.add(update);
    // result : report
    File reportFile = workspace.getObject().getReportFile();
    DownloadLink report = new DownloadLink("log", reportFile, reportFile.getName());
    update.add(report);
    // result : file list
    IModel<? extends List<File>> resultFilesModel = new LoadableDetachableModel<List<File>>() {

        private static final long serialVersionUID = -7971872898614031331L;

        @Override
        protected List<File> load() {
            return Arrays.asList(workspace.getObject().getTargetFolder().listFiles());
        }
    };
    final ListView<File> fileList = new ListView<File>("file", resultFilesModel) {

        private static final long serialVersionUID = -8045643864251639540L;

        @Override
        protected void populateItem(ListItem<File> item) {
            // download link
            DownloadLink download = new DownloadLink("download", item.getModelObject(), item.getModelObject().getName());
            item.add(download);
            // file name
            download.add(new Label("name", item.getModelObject().getName()));
        }
    };
    update.add(fileList);
    // leaseEnd
    String leaseEnd;
    try {
        leaseEnd = workspaces.getLeaseEnd(workspaceId).toString(DateTimeFormat.mediumDateTime());
    } catch (IOException e) {
        leaseEnd = "unknown";
    }
    add(new Label("leaseEnd", leaseEnd));
    boolean transformationFinished = workspace.getObject().isTransformationFinished();
    if (transformationFinished) {
        // disable job timer
        jobs.getTimer().stopOnNextUpdate();
    } else {
        // timer
        add(new AbstractAjaxTimerBehavior(Duration.milliseconds(1500)) {

            private static final long serialVersionUID = -3726349470723024150L;

            @Override
            protected void onTimer(AjaxRequestTarget target) {
                if (workspace.getObject().isTransformationFinished()) {
                    // update status and result
                    target.add(status);
                    target.add(result);
                    // stop timers
                    stop(target);
                    jobs.getTimer().stopOnNextUpdate();
                }
            }
        });
    }
}
Also used : TransformationWorkspace(eu.esdihumboldt.hale.common.headless.transform.TransformationWorkspace) DownloadLink(org.apache.wicket.markup.html.link.DownloadLink) Serializable(java.io.Serializable) AbortWithHttpErrorCodeException(org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException) FileNotFoundException(java.io.FileNotFoundException) Label(org.apache.wicket.markup.html.basic.Label) IOException(java.io.IOException) WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer) AjaxRequestTarget(org.apache.wicket.ajax.AjaxRequestTarget) ListView(org.apache.wicket.markup.html.list.ListView) LoadableDetachableModel(org.apache.wicket.model.LoadableDetachableModel) AbstractAjaxTimerBehavior(org.apache.wicket.ajax.AbstractAjaxTimerBehavior) ListItem(org.apache.wicket.markup.html.list.ListItem) JobPanel(eu.esdihumboldt.hale.server.webapp.components.JobPanel) File(java.io.File)

Aggregations

AbortWithHttpErrorCodeException (org.apache.wicket.request.http.flow.AbortWithHttpErrorCodeException)6 Vertex (com.tinkerpop.blueprints.Vertex)3 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)3 Template (eu.esdihumboldt.hale.server.model.Template)3 User (eu.esdihumboldt.hale.server.model.User)3 NonUniqueResultException (eu.esdihumboldt.util.blueprints.entities.NonUniqueResultException)3 Label (org.apache.wicket.markup.html.basic.Label)3 StringValue (org.apache.wicket.util.string.StringValue)3 WebMarkupContainer (org.apache.wicket.markup.html.WebMarkupContainer)2 PopoverConfig (de.agilecoders.wicket.core.markup.html.bootstrap.components.PopoverConfig)1 TransformationWorkspace (eu.esdihumboldt.hale.common.headless.transform.TransformationWorkspace)1 DeleteTemplateLink (eu.esdihumboldt.hale.server.templates.war.components.DeleteTemplateLink)1 ProjectURLPopover (eu.esdihumboldt.hale.server.templates.war.components.ProjectURLPopover)1 ResourcesPanel (eu.esdihumboldt.hale.server.templates.war.components.ResourcesPanel)1 TemplateForm (eu.esdihumboldt.hale.server.templates.war.components.TemplateForm)1 TemplateUploadForm (eu.esdihumboldt.hale.server.templates.war.components.TemplateUploadForm)1 JobPanel (eu.esdihumboldt.hale.server.webapp.components.JobPanel)1 HTMLPopoverBehavior (eu.esdihumboldt.hale.server.webapp.components.bootstrap.HTMLPopoverBehavior)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1