Search in sources :

Example 1 with MCRUsageException

use of org.mycore.common.MCRUsageException in project mycore by MyCoRe-Org.

the class MCRVersioningMetadataStoreTest method deletedVersions.

@Test
public void deletedVersions() throws Exception {
    Element root = new Element("bingo");
    Document xml1 = new Document(root);
    MCRVersionedMetadata vm = getVersStore().create(new MCRJDOMContent(xml1));
    assertFalse(vm.isDeleted());
    vm.delete();
    assertTrue(vm.isDeleted());
    assertFalse(getVersStore().exists(vm.getID()));
    vm = getVersStore().retrieve(vm.getID());
    assertTrue(vm.isDeletedInRepository());
    List<MCRMetadataVersion> versions = vm.listVersions();
    MCRMetadataVersion v1 = versions.get(0);
    MCRMetadataVersion v2 = versions.get(1);
    boolean cannotRestoreDeleted = false;
    try {
        v2.restore();
    } catch (MCRUsageException ex) {
        cannotRestoreDeleted = true;
    }
    assertTrue(cannotRestoreDeleted);
    v1.restore();
    assertFalse(vm.isDeletedInRepository());
    assertEquals(root.getName(), vm.getMetadata().asXML().getRootElement().getName());
}
Also used : MCRUsageException(org.mycore.common.MCRUsageException) Element(org.jdom2.Element) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) Document(org.jdom2.Document) Test(org.junit.Test)

Example 2 with MCRUsageException

use of org.mycore.common.MCRUsageException in project mycore by MyCoRe-Org.

the class MCRUploadHandlerManager method getHandler.

public static MCRUploadHandler getHandler(String uploadID) {
    long yesterday = System.currentTimeMillis() - 86400000;
    MCRUploadHandlerCacheEntry entry = HANDLERS.getIfUpToDate(uploadID, yesterday);
    if (entry == null)
        throw new MCRUsageException("Upload session " + uploadID + " timed out");
    String sessionID = entry.getSessionID();
    if (!sessionID.equals(MCRSessionMgr.getCurrentSessionID())) {
        MCRSession session = MCRSessionMgr.getSession(sessionID);
        if (session != null)
            MCRSessionMgr.setCurrentSession(session);
    }
    return entry.getUploadHandler();
}
Also used : MCRUsageException(org.mycore.common.MCRUsageException) MCRSession(org.mycore.common.MCRSession)

Example 3 with MCRUsageException

use of org.mycore.common.MCRUsageException in project mycore by MyCoRe-Org.

the class MCRQLSearchUtils method buildNameValueQuery.

/**
 * Search using name=value pairs from HTTP request
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static MCRQuery buildNameValueQuery(HttpServletRequest req) {
    MCRAndCondition condition = new MCRAndCondition();
    for (Enumeration<String> names = req.getParameterNames(); names.hasMoreElements(); ) {
        String name = names.nextElement();
        if (name.endsWith(".operator")) {
            continue;
        }
        if (name.contains(".sortField")) {
            continue;
        }
        if (SEARCH_PARAMETER.contains(name)) {
            continue;
        }
        if (name.startsWith("XSL.")) {
            continue;
        }
        String[] values = req.getParameterValues(name);
        MCRSetCondition parent = condition;
        if ((values.length > 1) || name.contains(",")) {
            // Multiple fields with same name, combine with OR
            parent = new MCROrCondition();
            condition.addChild(parent);
        }
        for (String fieldName : name.split(",")) {
            String operator = getReqParameter(req, fieldName + ".operator", "=");
            for (String value : values) {
                parent.addChild(new MCRQueryCondition(fieldName, operator, value));
            }
        }
    }
    if (condition.getChildren().isEmpty()) {
        throw new MCRUsageException("Missing query condition");
    }
    return new MCRQuery(MCRQueryParser.normalizeCondition(condition));
}
Also used : MCRUsageException(org.mycore.common.MCRUsageException) MCRQueryCondition(org.mycore.services.fieldquery.MCRQueryCondition) MCROrCondition(org.mycore.parsers.bool.MCROrCondition) MCRQuery(org.mycore.services.fieldquery.MCRQuery) MCRSetCondition(org.mycore.parsers.bool.MCRSetCondition) MCRAndCondition(org.mycore.parsers.bool.MCRAndCondition)

Example 4 with MCRUsageException

use of org.mycore.common.MCRUsageException in project mycore by MyCoRe-Org.

the class MCRPackerServlet method doGetPost.

@Override
protected void doGetPost(MCRServletJob job) throws IOException {
    String packer = job.getRequest().getParameter("packer");
    if (packer == null || packer.isEmpty()) {
        try {
            job.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST, "No or invalid 'packer' parameter!");
        } catch (IOException e) {
            LOGGER.error("Error while sending request error to client!", e);
            return;
        }
    }
    Map<String, String> jobParameters = resolveJobParameters(job);
    try {
        MCRJob mcrJob = MCRPackerManager.startPacking(jobParameters);
        if (mcrJob == null) {
            job.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST, "No packer parameter!");
        }
    } catch (MCRAccessException e) {
        job.getResponse().sendError(HttpServletResponse.SC_UNAUTHORIZED, e.getMessage());
    } catch (MCRUsageException e) {
        job.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid Parameters: " + e.getMessage());
    }
    if (jobParameters.containsKey("redirect")) {
        String redirect = jobParameters.get("redirect");
        job.getResponse().sendRedirect(job.getResponse().encodeRedirectURL(redirect));
    }
}
Also used : MCRUsageException(org.mycore.common.MCRUsageException) MCRAccessException(org.mycore.access.MCRAccessException) MCRJob(org.mycore.services.queuedjob.MCRJob) IOException(java.io.IOException)

Example 5 with MCRUsageException

use of org.mycore.common.MCRUsageException in project mycore by MyCoRe-Org.

the class MCRFile method setContentFrom.

/**
 * Reads the content of this file from a source file in the local filesystem and stores it in an MCRContentStore.
 *
 * @param source
 *            the file in the local host's filesystem thats content should be imported
 */
public void setContentFrom(File source) throws MCRPersistenceException {
    Objects.requireNonNull(source, "source file is null");
    if (!source.exists()) {
        throw new MCRUsageException("source file does not exist:" + source.getPath());
    }
    if (!source.canRead()) {
        throw new MCRUsageException("source file not readable:" + source.getPath());
    }
    FileInputStream fin = null;
    try {
        fin = new FileInputStream(source);
    } catch (FileNotFoundException ignored) {
    }
    // We already checked it exists
    setContentFrom(new BufferedInputStream(fin));
}
Also used : MCRUsageException(org.mycore.common.MCRUsageException) BufferedInputStream(java.io.BufferedInputStream) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream)

Aggregations

MCRUsageException (org.mycore.common.MCRUsageException)11 IOException (java.io.IOException)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)2 BufferedInputStream (java.io.BufferedInputStream)1 NameNotFoundException (javax.naming.NameNotFoundException)1 NamingException (javax.naming.NamingException)1 Attribute (javax.naming.directory.Attribute)1 Attributes (javax.naming.directory.Attributes)1 DirContext (javax.naming.directory.DirContext)1 InitialDirContext (javax.naming.directory.InitialDirContext)1 SearchControls (javax.naming.directory.SearchControls)1 SearchResult (javax.naming.directory.SearchResult)1 Document (org.jdom2.Document)1 Element (org.jdom2.Element)1 Test (org.junit.Test)1 MCRAccessException (org.mycore.access.MCRAccessException)1 MCRException (org.mycore.common.MCRException)1