Example usage for org.apache.commons.httpclient HttpStatus SC_FORBIDDEN

List of usage examples for org.apache.commons.httpclient HttpStatus SC_FORBIDDEN

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpStatus SC_FORBIDDEN.

Prototype

int SC_FORBIDDEN

To view the source code for org.apache.commons.httpclient HttpStatus SC_FORBIDDEN.

Click Source Link

Document

<tt>403 Forbidden</tt> (HTTP/1.0 - RFC 1945)

Usage

From source file:org.linagora.linshare.webservice.impl.PluginCompatibilityRestServiceImpl.java

@POST
@Path("/share/multiplesharedocuments")
@Override/*from w  w  w  .  j  a  v a2 s . c o m*/
public void multiplesharedocuments(@FormParam("targetMail") String targetMail,
        @FormParam("file") List<String> uuid, @FormParam("securedShare") @DefaultValue("0") int securedShare,
        @FormParam("message") @DefaultValue("") String message,
        @FormParam("inReplyTo") @DefaultValue("") String inReplyTo,
        @FormParam("references") @DefaultValue("") String references) throws BusinessException {
    User actor;

    actor = webServiceShareFacade.checkAuthentication();
    if ((actor instanceof Guest && !actor.getCanUpload()))
        throw giveRestException(HttpStatus.SC_FORBIDDEN, "You are not authorized to use this service");
    CollectionUtils.filter(uuid, StringPredicates.isNotBlank());
    if (uuid.isEmpty())
        throw giveRestException(HttpStatus.SC_BAD_REQUEST, "Missing parameter file");
    webServiceShareFacade.multiplesharedocuments(targetMail, uuid, securedShare, message, inReplyTo,
            references);
}

From source file:org.linagora.linshare.webservice.impl.PluginCompatibilityRestServiceImpl.java

/**
 * upload a file in user's space. send file inside a form
 *///from  ww w .j a v a  2 s.  com
@POST
@Path("/document/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Override
public DocumentDto uploadfile(@Multipart(value = "file") InputStream theFile,
        @Multipart(value = "description", required = false) String description,
        @Multipart(value = "filename", required = false) String givenFileName, MultipartBody body)
        throws BusinessException {
    User actor = webServiceDocumentFacade.checkAuthentication();
    String fileName;
    String comment = (description == null) ? "" : description;

    if ((actor instanceof Guest && !actor.getCanUpload())) {
        throw giveRestException(HttpStatus.SC_FORBIDDEN, "You are not authorized to use this service");
    }
    if (theFile == null) {
        throw giveRestException(HttpStatus.SC_BAD_REQUEST, "Missing file (check parameter file)");
    }
    if (givenFileName == null || givenFileName.isEmpty()) {
        // parameter givenFileName is optional
        // so need to search this information in the header of the
        // attachement (with id file)
        fileName = body.getAttachment("file").getContentDisposition().getParameter("filename");
    } else {
        fileName = givenFileName;
    }

    try {
        byte[] bytes = fileName.getBytes("ISO-8859-1");
        fileName = new String(bytes, "UTF-8");
    } catch (UnsupportedEncodingException e1) {
        logger.error("Can not encode file name " + e1.getMessage());
    }

    // comment can not be null ?
    return webServiceDocumentFacade.uploadfile(theFile, fileName, comment);
}

From source file:org.linagora.linshare.webservice.interceptor.SoapExceptionInterceptor.java

private void generateSoapFault(Fault fault, BusinessException e) {
    fault.setFaultCode(createQName(e.getErrorCode().getCode()));
    fault.setMessage(e.getMessage());// w w w  .ja v  a2s.c om

    switch (e.getErrorCode()) {
    case WEBSERVICE_FAULT:
        fault.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
        break;
    case WEBSERVICE_FORBIDDEN:
        fault.setStatusCode(HttpStatus.SC_FORBIDDEN);
        break;
    case FORBIDDEN:
        fault.setStatusCode(HttpStatus.SC_FORBIDDEN);
        break;
    case USER_NOT_FOUND:
        fault.setStatusCode(HttpStatus.SC_NOT_FOUND);
        break;
    case WEBSERVICE_NOT_FOUND:
        fault.setStatusCode(HttpStatus.SC_NOT_FOUND);
        break;
    default:
        fault.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
    }
}

From source file:org.linagora.linshare.webservice.user.impl.DocumentRestServiceImpl.java

/**
 * upload a file in user's space. send file inside a form
 *//*www. ja  va2 s .  co  m*/
@POST
@Path("/")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
@Override
public DocumentDto uploadfile(@Multipart(value = "file") InputStream theFile,
        @Multipart(value = "description", required = false) String description,
        @Multipart(value = "filename", required = false) String givenFileName, MultipartBody body)
        throws BusinessException {
    User actor = webServiceDocumentFacade.checkAuthentication();
    String fileName;
    String comment = (description == null) ? "" : description;

    if ((actor instanceof Guest && !actor.getCanUpload())) {
        throw giveRestException(HttpStatus.SC_FORBIDDEN, "You are not authorized to use this service");
    }
    if (theFile == null) {
        throw giveRestException(HttpStatus.SC_BAD_REQUEST, "Missing file (check parameter file)");
    }
    if (givenFileName == null || givenFileName.isEmpty()) {
        // parameter givenFileName is optional
        // so need to search this information in the header of the
        // attachement (with id file)
        fileName = body.getAttachment("file").getContentDisposition().getParameter("filename");
    } else {
        fileName = givenFileName;
    }

    return webServiceDocumentFacade.uploadfile(theFile, fileName, comment);
}

From source file:org.linagora.linshare.webservice.user.impl.FineUploaderServiceImpl.java

@Path("/receiver")
@ApiOperation(value = "Upload a file in the user space.", response = FineUploaderDto.class)
@POST/*from   w ww  .  ja va  2s.com*/
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
@Override
public FineUploaderDto upload(
        @ApiParam(value = "File stream", required = true) @Multipart(value = FILE) InputStream file,
        @ApiParam(value = "File name") @Multipart(value = FILE_NAME, required = false) String fileName,
        MultipartBody body) throws BusinessException {
    User actor = null;

    // Authentication, permission and error checking
    actor = documentFacade.checkAuthentication();
    if (actor instanceof Guest && !actor.getCanUpload()) {
        throw giveRestException(HttpStatus.SC_FORBIDDEN, "You are not authorized to use this service");
    }
    if (file == null) {
        throw giveRestException(HttpStatus.SC_BAD_REQUEST, "Missing file (check parameter file)");
    }
    // Ensure fileName and description aren't null
    if (fileName == null || fileName.isEmpty()) {
        fileName = body.getAttachment(FILE).getContentDisposition().getParameter(FILE_NAME);
    }

    try {
        byte[] bytes = fileName.getBytes("ISO-8859-1");
        fileName = new String(bytes, "UTF-8");
    } catch (UnsupportedEncodingException e1) {
        logger.error("Can not encode file name " + e1.getMessage());
    }
    try {
        DocumentDto doc = documentFacade.uploadfile(file, fileName, "");
        return new FineUploaderDto(true, doc.getUuid());
    } catch (BusinessException e) {
        return new FineUploaderDto(false);
    }
}

From source file:org.linagora.linshare.webservice.user.impl.FineUploaderServiceImpl.java

@Path("/receiver/{uuid}")
@ApiOperation(value = "Remove a previously uploaded file by uuid", response = FineUploaderDto.class)
@DELETE/*w w w .  j a  v  a 2s.  c  o  m*/
@Produces(MediaType.TEXT_PLAIN)
@Override
public FineUploaderDto delete(@ApiParam(value = "File uuid", required = true) @PathParam("uuid") String uuid)
        throws BusinessException {
    User actor = null;

    // Authentication, permission and error checking
    actor = documentFacade.checkAuthentication();
    if (actor instanceof Guest && !actor.getCanUpload()) {
        throw giveRestException(HttpStatus.SC_FORBIDDEN, "You are not authorized to use this service");
    }
    if (uuid == null || uuid.isEmpty()) {
        throw giveRestException(HttpStatus.SC_BAD_REQUEST, "Missing file (check parameter file)");
    }
    try {
        documentFacade.deleteFile(uuid);
        return new FineUploaderDto(true);
    } catch (BusinessException e) {
        return new FineUploaderDto(false);
    }
}

From source file:org.linagora.linshare.webservice.user.impl.FineUploaderServiceImpl.java

@Path("/threadentry/{threadUuid}")
@ApiOperation(value = "Upload a file in a thread by uuid.", response = FineUploaderDto.class)
@POST//  ww  w  .j  a va 2s. c om
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
@Override
public FineUploaderDto uploadThreadEntry(
        @ApiParam(value = "Thread uuid", required = true) @PathParam("threadUuid") String threadUuid,
        @ApiParam(value = FILE, required = true) @Multipart(value = FILE) InputStream file,
        @ApiParam(value = FILE_NAME) @Multipart(value = FILE_NAME, required = false) String fileName,
        MultipartBody body) throws BusinessException {
    User actor = null;

    // Authentication, permission and error checking
    actor = documentFacade.checkAuthentication();
    if (actor instanceof Guest && !actor.getCanUpload()) {
        throw giveRestException(HttpStatus.SC_FORBIDDEN, "You are not authorized to use this service");
    }
    if (file == null) {
        throw giveRestException(HttpStatus.SC_BAD_REQUEST, "Missing file (check parameter file)");
    }
    if (threadUuid == null) {
        throw giveRestException(HttpStatus.SC_BAD_REQUEST, "Missing thread Uuid(check parameter threadUuid)");
    }
    // Ensure fileName and description aren't null
    if (fileName == null || fileName.isEmpty()) {
        fileName = body.getAttachment(FILE).getContentDisposition().getParameter(FILE_NAME);
    }

    try {
        byte[] bytes = fileName.getBytes("ISO-8859-1");
        fileName = new String(bytes, "UTF-8");
    } catch (UnsupportedEncodingException e1) {
        logger.error("Can not encode file name " + e1.getMessage());
    }
    try {
        ThreadEntryDto doc = threadEntryFacade.uploadfile(threadUuid, file, fileName, "");
        return new FineUploaderDto(true, doc.getUuid());
    } catch (BusinessException e) {
        return new FineUploaderDto(false);
    }
}

From source file:org.moxie.proxy.connection.ProxyDownload.java

/**
 * Do the download.//from w w w. j ava  2s. co  m
 * 
 * @throws IOException
 * @throws DownloadFailed
 */
public void download() throws IOException, DownloadFailed {
    if (!config.isAllowed(url)) {
        throw new DownloadFailed(
                "HTTP/1.1 " + HttpStatus.SC_FORBIDDEN + " Download denied by rule in Moxie Proxy config");
    }

    HttpClient client = new HttpClient();
    String userAgent = config.getUserAgent();
    if (userAgent != null && userAgent.trim().length() > 0) {
        client.getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent);
    }

    String msg = "";
    if (config.useProxy(url)) {
        Proxy proxy = config.getProxy(url);
        Credentials defaultcreds = new UsernamePasswordCredentials(proxy.username, proxy.password);
        AuthScope scope = new AuthScope(proxy.host, proxy.port, AuthScope.ANY_REALM);
        HostConfiguration hc = new HostConfiguration();
        hc.setProxy(proxy.host, proxy.port);
        client.setHostConfiguration(hc);
        client.getState().setProxyCredentials(scope, defaultcreds);
        msg = "via proxy ";
    }
    log.info("Downloading " + msg + "to " + dest.getAbsolutePath());

    GetMethod get = new GetMethod(url.toString());
    get.setFollowRedirects(true);
    try {
        int status = client.executeMethod(get);

        log.info("Download status: " + status);
        if (status == 1 && log.isLoggable(Level.FINE)) {
            Header[] header = get.getResponseHeaders();
            for (int i = 0; i < header.length; i++)
                log.fine(header[i].toString().trim());
        }

        log.info("Content: " + valueOf(get.getResponseHeader("Content-Length")) + " bytes; "
                + valueOf(get.getResponseHeader("Content-Type")));

        if (status != HttpStatus.SC_OK) {
            throw new DownloadFailed(get);
        }

        // Make sure the temporary file is created in
        // the destination folder, otherwise
        // dl.renameTo(dest) might not work
        // for example if you have a separate /tmp partition
        // on Linux
        File destinationFolder = dest.getParentFile();
        dest.getParentFile().mkdirs();
        File dl = File.createTempFile("moxie-", ".tmp", destinationFolder);
        OutputStream out = new BufferedOutputStream(new FileOutputStream(dl));
        copy(get.getResponseBodyAsStream(), out);
        out.close();

        // create folder structure after successful download
        // - no, we create it before the download!
        //dest.getParentFile().mkdirs();

        if (dest.exists()) {
            dest.delete();
        }
        dl.renameTo(dest);

        // preserve last-modified, if possible
        try {
            Header lastModified = get.getResponseHeader("Last-Modified");
            if (lastModified != null) {
                Date date = DateUtil.parseDate(lastModified.getValue());
                dest.setLastModified(date.getTime());
            }
        } catch (Exception e) {
            log.log(Level.WARNING, "could not parse \"last-modified\" for " + url, e);
        }
    } finally {
        get.releaseConnection();
    }
}

From source file:org.moxie.proxy.connection.ProxyHead.java

/**
 * Do the download (of the headers).//from ww  w  .j  a  va2s .c o m
 * 
 * @throws IOException
 * @throws DownloadFailed
 */
public void download() throws IOException, DownloadFailed {
    if (!config.isAllowed(url)) {
        throw new DownloadFailed(
                "HTTP/1.1 " + HttpStatus.SC_FORBIDDEN + " Download denied by rule in Moxie Proxy config");
    }

    HttpClient client = new HttpClient();
    String userAgent = config.getUserAgent();
    if (userAgent != null && userAgent.trim().length() > 0) {
        client.getParams().setParameter(HttpMethodParams.USER_AGENT, userAgent);
    }

    String msg = "";
    if (config.useProxy(url)) {
        Proxy proxy = config.getProxy(url);
        Credentials defaultcreds = new UsernamePasswordCredentials(proxy.username, proxy.password);
        AuthScope scope = new AuthScope(proxy.host, proxy.port, AuthScope.ANY_REALM);
        HostConfiguration hc = new HostConfiguration();
        hc.setProxy(proxy.host, proxy.port);
        client.setHostConfiguration(hc);
        client.getState().setProxyCredentials(scope, defaultcreds);
        msg = "via proxy ";
    }
    log.info("Downloading " + msg + url);

    MyHeadMethod head = new MyHeadMethod(url.toString());
    head.setFollowRedirects(true);
    try {
        int status = client.executeMethod(head);

        log.info("Download status: " + status);
        this.header = head.getResponseHeaders();
        this.statusLine = head.getStatusLine().toString();
        this.responseBody = head.getResponseBody();
        if (status == 1 && log.isLoggable(Level.FINE)) {
            for (int i = 0; i < header.length; i++)
                log.fine(header[i].toString().trim());
        }

        log.info("Content: " + valueOf(head.getResponseHeader("Content-Length")) + " bytes; "
                + valueOf(head.getResponseHeader("Content-Type")));

        if (status != HttpStatus.SC_OK) {
            throw new DownloadFailed(head);
        }

    } finally {
        head.releaseConnection();
    }
}

From source file:org.openanzo.client.BinaryStoreClient.java

protected int executeAuthenticatedHttpClientMethod(HttpMethod method)
        throws HttpException, IOException, AnzoException {

    method.addRequestHeader("X-Requested-With", "XMLHttpRequest");
    String serviceUser = anzoClient.getServiceUser();
    if (serviceUser != null && serviceUser.equals(anzoClient.clientDatasource.getServiceUser()))
        method.addRequestHeader(AUTHRUNAS_HEADER, anzoClient.clientDatasource.getServiceUser());

    int rc = httpclient.executeMethod(method);
    if (rc == HttpStatus.SC_FORBIDDEN) {
        method.releaseConnection();//from w  w w. j a v  a 2 s  . c  o  m
        authenticate();
        rc = httpclient.executeMethod(method);
    }
    return rc;

}