Search in sources :

Example 46 with GWException

use of com.pspace.ifs.ksan.gw.exception.GWException in project ksan by infinistor.

the class S3DataRequest method readJson.

protected String readJson() throws GWException {
    String ret = null;
    try {
        byte[] json = s3Parameter.getInputStream().readAllBytes();
        s3Parameter.addRequestSize(json.length);
        ret = new String(json);
    } catch (IOException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.INTERNAL_SERVER_ERROR, s3Parameter);
    }
    logger.info(ret);
    if (Strings.isNullOrEmpty(ret)) {
        logger.warn(GWErrorCode.INVALID_ARGUMENT.getMessage());
        throw new GWException(GWErrorCode.INVALID_ARGUMENT, s3Parameter);
    }
    return ret;
}
Also used : IOException(java.io.IOException) GWException(com.pspace.ifs.ksan.gw.exception.GWException)

Example 47 with GWException

use of com.pspace.ifs.ksan.gw.exception.GWException in project ksan by infinistor.

the class MariaDB method execute.

private void execute(String query, List<Object> params, S3Parameter s3Parameter) throws GWException {
    try (Connection conn = DriverManager.getConnection(GWConstants.JDBC_DRIVER);
        PreparedStatement pstmt = conn.prepareStatement(query)) {
        int index = 1;
        if (params != null) {
            for (Object p : params) {
                pstmt.setObject(index, p);
                index++;
            }
        }
        logger.info(pstmt.toString());
        pstmt.execute();
    } catch (SQLException e) {
        throw new GWException(GWErrorCode.INTERNAL_SERVER_DB_ERROR, s3Parameter);
    } catch (Exception e) {
        throw new GWException(GWErrorCode.INTERNAL_SERVER_DB_ERROR, s3Parameter);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) PreparedStatement(java.sql.PreparedStatement) GWException(com.pspace.ifs.ksan.gw.exception.GWException) GWException(com.pspace.ifs.ksan.gw.exception.GWException) SQLException(java.sql.SQLException)

Example 48 with GWException

use of com.pspace.ifs.ksan.gw.exception.GWException in project ksan by infinistor.

the class MariaDB method select.

public List<HashMap<String, Object>> select(String query, List<Object> params, S3Parameter s3Parameter) throws GWException {
    List<HashMap<String, Object>> rmap = null;
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rset = null;
    try {
        conn = DriverManager.getConnection(GWConstants.JDBC_DRIVER);
        pstmt = conn.prepareStatement(query);
        int index = 1;
        if (params != null) {
            for (Object p : params) {
                pstmt.setObject(index, p);
                index++;
            }
        }
        logger.info(pstmt.toString());
        rset = pstmt.executeQuery();
        ResultSetMetaData md = rset.getMetaData();
        int columns = md.getColumnCount();
        int init = 0;
        while (rset.next()) {
            if (init == 0) {
                rmap = new ArrayList<HashMap<String, Object>>();
                init++;
            }
            HashMap<String, Object> map = null;
            map = new HashMap<String, Object>(columns);
            for (int i = 1; i <= columns; ++i) {
                map.put(md.getColumnName(i), rset.getObject(i));
            }
            rmap.add(map);
        }
    } catch (SQLException e) {
        logger.error(e.getMessage());
        throw new GWException(GWErrorCode.INTERNAL_SERVER_DB_ERROR, s3Parameter);
    } catch (Exception e) {
        logger.error(e.getMessage());
        throw new GWException(GWErrorCode.INTERNAL_SERVER_DB_ERROR, s3Parameter);
    } finally {
        if (rset != null)
            try {
                rset.close();
            } catch (Exception e) {
                logger.error(e.getMessage());
                throw new GWException(GWErrorCode.INTERNAL_SERVER_DB_ERROR, s3Parameter);
            }
        if (pstmt != null)
            try {
                pstmt.close();
            } catch (Exception e) {
                logger.error(e.getMessage());
                throw new GWException(GWErrorCode.INTERNAL_SERVER_DB_ERROR, s3Parameter);
            }
        if (conn != null)
            try {
                conn.close();
            } catch (Exception e) {
                logger.error(e.getMessage());
                throw new GWException(GWErrorCode.INTERNAL_SERVER_DB_ERROR, s3Parameter);
            }
    }
    return rmap;
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) PreparedStatement(java.sql.PreparedStatement) GWException(com.pspace.ifs.ksan.gw.exception.GWException) SQLException(java.sql.SQLException) ResultSetMetaData(java.sql.ResultSetMetaData) ResultSet(java.sql.ResultSet) GWException(com.pspace.ifs.ksan.gw.exception.GWException)

Example 49 with GWException

use of com.pspace.ifs.ksan.gw.exception.GWException in project ksan by infinistor.

the class GWHandler method doHandle.

public final void doHandle(Request baseRequest, HttpServletRequest request, HttpServletResponse response, InputStream is) throws GWException {
    long requestSize = 0L;
    String method = request.getMethod();
    requestSize = method.length();
    String uri = request.getRequestURI();
    requestSize += uri.length();
    long startTime = System.currentTimeMillis();
    logger.info(GWConstants.LOG_GWHANDLER_PREURI, uri);
    uri = removeDuplicateRoot(uri);
    logger.info(GWConstants.LOG_GWHANDLER_URI, uri);
    logger.info(GWConstants.LOG_GWHANDLER_CLIENT_ADDRESS, request.getRemoteAddr());
    logger.info(GWConstants.LOG_GWHANDLER_CLIENT_HOST, request.getRemoteHost());
    logger.info(GWConstants.LOG_GWHANDLER_METHOD, method);
    for (String parameter : Collections.list(request.getParameterNames())) {
        logger.info(GWConstants.LOG_GWHANDLER_PARAMETER, parameter, Strings.nullToEmpty(request.getParameter(parameter)));
        requestSize += parameter.length();
        if (!Strings.isNullOrEmpty(request.getParameter(parameter))) {
            requestSize += request.getParameter(parameter).length();
        }
    }
    for (String headerName : Collections.list(request.getHeaderNames())) {
        for (String headerValue : Collections.list(request.getHeaders(headerName))) {
            logger.info(GWConstants.LOG_GWHANDLER_HEADER, headerName, Strings.nullToEmpty(headerValue));
            requestSize += headerName.length();
            if (!Strings.isNullOrEmpty(headerValue)) {
                requestSize += headerValue.length();
            }
        }
    }
    // make request id
    String requestID = UUID.randomUUID().toString().substring(24).toUpperCase();
    String[] path = uri.split(GWConstants.SLASH, 3);
    try {
        for (int i = 0; i < path.length; i++) {
            path[i] = URLDecoder.decode(path[i], GWConstants.CHARSET_UTF_8);
            logger.info(GWConstants.LOG_GWHANDLER_PATH, i, path[i]);
        }
    } catch (UnsupportedEncodingException e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.BAD_REQUEST, null);
    }
    String pathCategory = GWConstants.EMPTY_STRING;
    if (uri.equals(GWConstants.SLASH)) {
        pathCategory = GWConstants.CATEGORY_ROOT;
    } else if (path.length <= 2 || path[2].isEmpty()) {
        pathCategory = GWConstants.CATEGORY_BUCKET;
    } else {
        pathCategory = GWConstants.CATEGORY_OBJECT;
    }
    S3Parameter s3Parameter = new S3Parameter();
    s3Parameter.setRequestSize(requestSize);
    s3Parameter.setRequestID(requestID);
    s3Parameter.setRequest(request);
    s3Parameter.setResponse(response);
    s3Parameter.setInputStream(is);
    if (!Strings.isNullOrEmpty(path[1])) {
        s3Parameter.setBucketName(path[1]);
    }
    if (path.length == 3 && !Strings.isNullOrEmpty(path[2])) {
        s3Parameter.setObjectName(path[2]);
    }
    s3Parameter.setMethod(method);
    s3Parameter.setStartTime(startTime);
    s3Parameter.setPathCategory(pathCategory);
    s3Parameter.setMaxFileSize(maxFileSize);
    s3Parameter.setMaxTimeSkew(maxTimeSkew);
    s3Parameter.setRemoteHost(request.getRemoteHost());
    s3Parameter.setRequestURI(request.getRequestURI());
    s3Parameter.setReferer(request.getHeader(HttpHeaders.REFERER));
    s3Parameter.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT));
    s3Parameter.setAuthorization(request.getHeader(HttpHeaders.AUTHORIZATION));
    s3Parameter.setxAmzAlgorithm(request.getParameter(GWConstants.X_AMZ_ALGORITHM));
    s3Parameter.setHostName(request.getHeader(HttpHeaders.HOST));
    s3Parameter.setHostID(request.getHeader(GWConstants.X_AMZ_ID_2));
    s3Parameter.setRemoteAddr(!Strings.isNullOrEmpty(request.getHeader(GWConstants.X_FORWARDED_FOR)) ? request.getHeader(GWConstants.X_FORWARDED_FOR) : request.getRemoteAddr());
    if (request.getHeader(HttpHeaders.AUTHORIZATION) == null && request.getParameter(GWConstants.X_AMZ_ALGORITHM) == null && request.getParameter(GWConstants.AWS_ACCESS_KEY_ID) == null) {
        if (s3Parameter.getPathCategory().equals(GWConstants.CATEGORY_ROOT)) {
            throw new GWException(GWErrorCode.ACCESS_DENIED, s3Parameter);
        }
        S3Signing s3signing = new S3Signing(s3Parameter);
        s3Parameter = s3signing.publicvalidation();
        s3Parameter.setPublicAccess(true);
    } else {
        S3Signing s3signing = new S3Signing(s3Parameter);
        s3Parameter = s3signing.validation();
        s3Parameter.setPublicAccess(false);
    }
    logger.info(GWConstants.LOG_GWHANDLER_MOTHOD_CATEGORY, s3Parameter.getMethod(), s3Parameter.getPathCategory());
    S3Request s3Request = s3RequestFactory.createS3Request(s3Parameter);
    s3Request.process();
    s3Parameter.setStatusCode(response.getStatus());
    AsyncHandler.s3logging(s3Parameter);
}
Also used : S3Parameter(com.pspace.ifs.ksan.gw.identity.S3Parameter) S3Signing(com.pspace.ifs.ksan.gw.sign.S3Signing) UnsupportedEncodingException(java.io.UnsupportedEncodingException) GWException(com.pspace.ifs.ksan.gw.exception.GWException) S3Request(com.pspace.ifs.ksan.gw.api.S3Request)

Example 50 with GWException

use of com.pspace.ifs.ksan.gw.exception.GWException in project ksan by infinistor.

the class S3Request method remove.

protected void remove(String bucket, String object, String versionId) throws GWException {
    try {
        setObjManager();
        objManager.remove(bucket, object, versionId);
    } catch (Exception e) {
        PrintStack.logging(logger, e);
        throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
    } finally {
        try {
            releaseObjManager();
        } catch (Exception e) {
            PrintStack.logging(logger, e);
            throw new GWException(GWErrorCode.SERVER_ERROR, s3Parameter);
        }
    }
}
Also used : GWException(com.pspace.ifs.ksan.gw.exception.GWException) GWException(com.pspace.ifs.ksan.gw.exception.GWException) ResourceAlreadyExistException(com.pspace.ifs.ksan.objmanager.ObjManagerException.ResourceAlreadyExistException) XMLStreamException(javax.xml.stream.XMLStreamException) ResourceNotFoundException(com.pspace.ifs.ksan.objmanager.ObjManagerException.ResourceNotFoundException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException)

Aggregations

GWException (com.pspace.ifs.ksan.gw.exception.GWException)130 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)61 S3Bucket (com.pspace.ifs.ksan.gw.identity.S3Bucket)58 XMLStreamException (javax.xml.stream.XMLStreamException)48 IOException (java.io.IOException)46 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)45 ResourceNotFoundException (com.pspace.ifs.ksan.objmanager.ObjManagerException.ResourceNotFoundException)43 ResourceAlreadyExistException (com.pspace.ifs.ksan.objmanager.ObjManagerException.ResourceAlreadyExistException)32 XmlMapper (com.fasterxml.jackson.dataformat.xml.XmlMapper)23 Metadata (com.pspace.ifs.ksan.objmanager.Metadata)23 S3Metadata (com.pspace.ifs.ksan.gw.identity.S3Metadata)17 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)16 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)15 AccessControlPolicy (com.pspace.ifs.ksan.gw.format.AccessControlPolicy)14 Writer (java.io.Writer)13 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)13 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)11 Grant (com.pspace.ifs.ksan.gw.format.AccessControlPolicy.AccessControlList.Grant)10 S3ObjectOperation (com.pspace.ifs.ksan.gw.object.S3ObjectOperation)10 Date (java.util.Date)8