Search in sources :

Example 6 with NotAuthorizedException

use of io.milton.http.exceptions.NotAuthorizedException in project lobcder by skoulouzis.

the class WebDataDirResource method sendContent.

@Override
public void sendContent(OutputStream out, Range range, Map<String, String> params, String contentType) throws IOException, NotAuthorizedException, BadRequestException {
    Logger.getLogger(WebDataDirResource.class.getName()).log(Level.FINEST, "sendContent({0}) for {1}", new Object[] { contentType, getPath() });
    try (Connection connection = getCatalogue().getConnection()) {
        try (PrintStream ps = new PrintStream(out)) {
            HtmlCanvas html = new HtmlCanvas();
            html.table(border("1")).tr().th().content("Name").th().content("Size").th().content("Modification Date").th().content("Creation Date").th().content("Owner").th().content("Content Type").th().content("Type").th().content("Is Supervised").th().content("Uid");
            String ref;
            for (LogicalData ld : getCatalogue().getChildrenByParentRef(getLogicalData().getUid(), connection)) {
                if (ld.isFolder()) {
                    ref = "../dav" + getPath() + "/" + ld.getName();
                // if (ld.getUid() != 1) {
                // } else {
                // }
                } else {
                    ref = "../dav" + getPath() + "/" + ld.getName();
                }
                html._tr().tr().td().a(href(ref)).img(src("").alt(ld.getName()))._a()._td().td().content(String.valueOf(ld.getLength())).td().content(new Date(ld.getModifiedDate()).toString()).td().content(new Date(ld.getCreateDate()).toString()).td().content(ld.getOwner()).td().content(ld.getContentTypesAsString()).td().content(ld.getType()).td().content(ld.getSupervised().toString()).td().content(ld.getUid().toString());
            }
            html._tr()._table();
            ps.println(html.toHtml());
            getCatalogue().addViewForRes(getLogicalData().getUid(), connection);
            connection.commit();
            connection.close();
        } catch (Exception e) {
            if (connection != null && !connection.isClosed()) {
                connection.rollback();
                connection.close();
            }
            throw e;
        }
    } catch (Exception e) {
        Logger.getLogger(WebDataDirResource.class.getName()).log(Level.SEVERE, null, e);
        throw new BadRequestException(this);
    }
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData) Connection(java.sql.Connection) HtmlCanvas(org.rendersnake.HtmlCanvas) BadRequestException(io.milton.http.exceptions.BadRequestException) ConflictException(io.milton.http.exceptions.ConflictException) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) BadRequestException(io.milton.http.exceptions.BadRequestException) PreConditionFailedException(io.milton.http.exceptions.PreConditionFailedException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 7 with NotAuthorizedException

use of io.milton.http.exceptions.NotAuthorizedException in project lobcder by skoulouzis.

the class WebDataFileResource method copyTo.

@Override
public void copyTo(CollectionResource collectionResource, String name) throws NotAuthorizedException, BadRequestException, ConflictException {
    WebDataDirResource toWDDR = (WebDataDirResource) collectionResource;
    Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "copyTo(''{0}'', ''{1}'') for {2}", new Object[] { toWDDR.getPath(), name, getPath() });
    try (Connection connection = getCatalogue().getConnection()) {
        try {
            Permissions newParentPerm = getCatalogue().getPermissions(toWDDR.getLogicalData().getUid(), toWDDR.getLogicalData().getOwner(), connection);
            if (!getPrincipal().canWrite(newParentPerm)) {
                throw new NotAuthorizedException(this);
            }
            getCatalogue().copyFile(getLogicalData(), toWDDR.getLogicalData(), name, getPrincipal(), connection);
            connection.commit();
        } catch (SQLException | NotAuthorizedException e) {
            Logger.getLogger(WebDataFileResource.class.getName()).log(Level.SEVERE, null, e);
            connection.rollback();
            throw new BadRequestException(this, e.getMessage());
        }
    } catch (SQLException e) {
        Logger.getLogger(WebDataFileResource.class.getName()).log(Level.SEVERE, null, e);
        throw new BadRequestException(this, e.getMessage());
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) Permissions(nl.uva.cs.lobcder.auth.Permissions) BadRequestException(io.milton.http.exceptions.BadRequestException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException)

Example 8 with NotAuthorizedException

use of io.milton.http.exceptions.NotAuthorizedException in project lobcder by skoulouzis.

the class WebDataFileResource method moveTo.

@Override
public void moveTo(CollectionResource collectionResource, String name) throws ConflictException, NotAuthorizedException, BadRequestException {
    WebDataDirResource toWDDR = (WebDataDirResource) collectionResource;
    Logger.getLogger(WebDataFileResource.class.getName()).log(Level.FINEST, "moveTo(''{0}'', ''{1}'') for {2}", new Object[] { toWDDR.getPath(), name, getPath() });
    try (Connection connection = getCatalogue().getConnection()) {
        try {
            Permissions destPerm = getCatalogue().getPermissions(toWDDR.getLogicalData().getUid(), toWDDR.getLogicalData().getOwner(), connection);
            LogicalData parentLD = getCatalogue().getLogicalDataByUid(getLogicalData().getParentRef());
            Permissions parentPerm = getCatalogue().getPermissions(parentLD.getUid(), parentLD.getOwner());
            if (!(getPrincipal().canWrite(destPerm) && getPrincipal().canWrite(parentPerm))) {
                throw new NotAuthorizedException(this);
            }
            getCatalogue().moveEntry(getLogicalData(), toWDDR.getLogicalData(), name, connection);
            connection.commit();
        } catch (Exception e) {
            Logger.getLogger(WebDataFileResource.class.getName()).log(Level.SEVERE, null, e);
            connection.rollback();
            throw new BadRequestException(this, e.getMessage());
        }
    } catch (SQLException e) {
        Logger.getLogger(WebDataFileResource.class.getName()).log(Level.SEVERE, null, e);
        throw new BadRequestException(this, e.getMessage());
    }
}
Also used : LogicalData(nl.uva.cs.lobcder.resources.LogicalData) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Permissions(nl.uva.cs.lobcder.auth.Permissions) BadRequestException(io.milton.http.exceptions.BadRequestException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) ConflictException(io.milton.http.exceptions.ConflictException) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) BadRequestException(io.milton.http.exceptions.BadRequestException) IOException(java.io.IOException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) UnknownHostException(java.net.UnknownHostException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NotFoundException(io.milton.http.exceptions.NotFoundException)

Example 9 with NotAuthorizedException

use of io.milton.http.exceptions.NotAuthorizedException in project lobcder by skoulouzis.

the class WebDataResource method checkRedirect.

@Override
public String checkRedirect(Request request) throws NotAuthorizedException, BadRequestException {
    switch(request.getMethod()) {
        case PUT:
        case POST:
            if (!redirectPosts) {
                return null;
            }
            String redirect = null;
            try {
                if (!canRedirect(request)) {
                    return null;
                }
                Map<Long, Pair<WebDataFileResource, Long>> resources = createResouses(request);
                // lockResources(resources);
                Map<String, Pair<Long, Collection<Long>>> storageMap = getStorageMap(resources);
                StringBuilder sb = new StringBuilder();
                Set<String> keys = storageMap.keySet();
                for (String k : keys) {
                    sb.append("file_name=").append(k).append("/");
                    Pair pair = storageMap.get(k);
                    Long fileUid = (Long) pair.getLeft();
                    sb.append("file_uid=").append(fileUid).append("/");
                    Long pdriGroupUid = resources.get(fileUid).getRight();
                    sb.append("pdrigroup_uid=").append(pdriGroupUid).append("/");
                    Collection<Long> ssids = (Collection<Long>) pair.getRight();
                    for (Long ssid : ssids) {
                        sb.append("ss_id=").append(ssid).append("/");
                    }
                    sb.append("&");
                }
                sb.deleteCharAt(sb.length() - 1);
                String folder = request.getAbsolutePath();
                if (!folder.endsWith("/")) {
                    folder += "/";
                }
                redirect = "http://localhost:8080/lobcder-worker" + folder + "?" + sb.toString();
            } catch (Exception ex) {
                Logger.getLogger(WebDataResource.class.getName()).log(Level.SEVERE, null, ex);
            }
            return redirect;
        default:
            return null;
    }
// return null;
}
Also used : URISyntaxException(java.net.URISyntaxException) BadRequestException(io.milton.http.exceptions.BadRequestException) PreConditionFailedException(io.milton.http.exceptions.PreConditionFailedException) IOException(java.io.IOException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) LockedException(io.milton.http.exceptions.LockedException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MutablePair(org.apache.commons.lang3.tuple.MutablePair) Pair(org.apache.commons.lang3.tuple.Pair)

Example 10 with NotAuthorizedException

use of io.milton.http.exceptions.NotAuthorizedException in project lobcder by skoulouzis.

the class WebDataResource method unlock.

@Override
public void unlock(String token) throws NotAuthorizedException, PreConditionFailedException {
    try (Connection connection = getCatalogue().getConnection()) {
        try {
            String tokenID = getLogicalData().getLockTokenID();
            if (tokenID == null || tokenID.length() <= 0) {
                return;
            } else {
                if (tokenID.startsWith("<") && tokenID.endsWith(">") && !token.startsWith("<") && !token.endsWith(">")) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("<").append(token).append(">");
                    token = sb.toString();
                }
                if (!tokenID.startsWith("<") && !tokenID.endsWith(">") && token.startsWith("<") && token.endsWith(">")) {
                    token = token.replaceFirst("<", "");
                    token = token.replaceFirst(">", "");
                }
                if (!tokenID.equals(token)) {
                    throw new PreConditionFailedException(this);
                }
            }
            getCatalogue().setLockTokenID(getLogicalData().getUid(), null, connection);
            connection.commit();
            getLogicalData().setLockTokenID(null);
            getLogicalData().setLockScope(null);
            getLogicalData().setLockType(null);
            getLogicalData().setLockedByUser(null);
            getLogicalData().setLockDepth(null);
            getLogicalData().setLockTimeout(null);
        } catch (Exception ex) {
            Logger.getLogger(WebDataResource.class.getName()).log(Level.SEVERE, null, ex);
            connection.rollback();
            throw new PreConditionFailedException(this);
        }
    } catch (SQLException e) {
        Logger.getLogger(WebDataResource.class.getName()).log(Level.SEVERE, null, e);
        throw new PreConditionFailedException(this);
    }
}
Also used : PreConditionFailedException(io.milton.http.exceptions.PreConditionFailedException) URISyntaxException(java.net.URISyntaxException) BadRequestException(io.milton.http.exceptions.BadRequestException) PreConditionFailedException(io.milton.http.exceptions.PreConditionFailedException) IOException(java.io.IOException) NotAuthorizedException(io.milton.http.exceptions.NotAuthorizedException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) LockedException(io.milton.http.exceptions.LockedException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

NotAuthorizedException (io.milton.http.exceptions.NotAuthorizedException)40 BadRequestException (io.milton.http.exceptions.BadRequestException)29 IOException (java.io.IOException)12 Resource (io.milton.resource.Resource)10 URISyntaxException (java.net.URISyntaxException)9 Connection (java.sql.Connection)9 SQLException (java.sql.SQLException)9 ConflictException (io.milton.http.exceptions.ConflictException)7 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)7 PreConditionFailedException (io.milton.http.exceptions.PreConditionFailedException)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 Permissions (nl.uva.cs.lobcder.auth.Permissions)6 NotFoundException (io.milton.http.exceptions.NotFoundException)5 CollectionResource (io.milton.resource.CollectionResource)5 ReplaceableResource (io.milton.resource.ReplaceableResource)5 QName (javax.xml.namespace.QName)5 Path (io.milton.common.Path)4 LockedException (io.milton.http.exceptions.LockedException)4 ValueAndType (io.milton.http.values.ValueAndType)4 LogicalData (nl.uva.cs.lobcder.resources.LogicalData)4