public String post()

in src/main/java/org/apache/directory/fortress/core/rest/RestUtils.java [299:410]


    public String post( String userId, String password, String szInput, String function ) throws RestException
    {
        LOG.debug( "post uri=[{}], function=[{}], request=[{}]", uri, function, szInput );
        String szResponse = null;
        HttpPost post = new HttpPost( uri + function);
        post.addHeader( "Accept", "text/xml" );
        setMethodHeaders( post );
        try
        {
            HttpEntity entity = new StringEntity( szInput, ContentType.TEXT_XML );
            post.setEntity( entity );
            org.apache.http.client.HttpClient httpclient = HttpClientBuilder.create().useSystemProperties()
                .setDefaultCredentialsProvider(getCredentialProvider(userId, password)).build();
            HttpResponse response = httpclient.execute( post );
            String error;

            switch ( response.getStatusLine().getStatusCode() )
            {
                case HTTP_OK :
                    szResponse = IOUtils.toString( response.getEntity().getContent(), "UTF-8" );
                    if( StringUtils.isNotEmpty( szResponse ) && szResponse.contains(VALID_RESPONSE) )
                    {
                        LOG.debug( "post uri=[{}], function=[{}], response=[{}]", uri, function, szResponse );
                    }
                    else
                    {
                        error = generateErrorMessage( uri, function, "invalid response" );
                        LOG.error( error );
                        throw new RestException( GlobalErrIds.REST_NOT_FOUND_ERR, error );
                    }
                    break;
                case HTTP_401_UNAUTHORIZED :
                    error = generateErrorMessage( uri, function, "401 function unauthorized on host" );
                    LOG.error( error );
                    throw new RestException( GlobalErrIds.REST_UNAUTHORIZED_ERR, error );
                case HTTP_403_FORBIDDEN :
                    error = generateErrorMessage( uri, function, "403 function forbidden on host" );
                    LOG.error( error );
                    throw new RestException( GlobalErrIds.REST_FORBIDDEN_ERR, error );
                case HTTP_404_NOT_FOUND:
                    szResponse = IOUtils.toString( response.getEntity().getContent(), "UTF-8" );
                    // Crack the response and see if it can be parsed as a valid Fortress Response object or generic HTTP:
                    if( StringUtils.isNotEmpty( szResponse ) && szResponse.contains(VALID_RESPONSE) )
                    {
                        LOG.debug( "HTTP: 404: post uri=[{}], function=[{}], response=[{}]", uri, function, szResponse );
                    }
                    else
                    {
                        error = generateErrorMessage( uri, function, "HTTP Error:" + response.getStatusLine().getStatusCode());
                        LOG.error( error );
                        throw new RestException( GlobalErrIds.REST_NOT_FOUND_ERR, error );
                    }
                    break;
                case HTTP_500_INTERNAL_SERVER_ERROR:
                    szResponse = IOUtils.toString( response.getEntity().getContent(), "UTF-8" );
                    // Crack the response and see if it can be parsed as a valid Fortress Response object or generic HTTP:
                    if( StringUtils.isNotEmpty( szResponse ) && szResponse.contains(VALID_RESPONSE) )
                    {
                        LOG.debug( "HTTP 500: post uri=[{}], function=[{}], response=[{}]", uri, function, szResponse );
                    }
                    else
                    {
                        error = generateErrorMessage( uri, function, "HTTP 500 Internal Error:" + response.getStatusLine().getStatusCode());
                        LOG.error( error );
                        throw new RestException( GlobalErrIds.REST_INTERNAL_ERR, error );
                    }
                    break;
                case HTTP_400_VALIDATION_EXCEPTION:
                    szResponse = IOUtils.toString( response.getEntity().getContent(), "UTF-8" );
                    // Crack the response and see if it can be parsed as a valid Fortress Response object or generic HTTP:
                    if( StringUtils.isNotEmpty( szResponse ) && szResponse.contains(VALID_RESPONSE) )
                    {
                        LOG.debug( "HTTP 400: post uri=[{}], function=[{}], response=[{}]", uri, function, szResponse );
                    }
                    else
                    {
                        error = generateErrorMessage( uri, function, "HTTP 400 Validation Error:" + response.getStatusLine().getStatusCode());
                        LOG.error( error );
                        throw new RestException( GlobalErrIds.REST_VALIDATION_ERR, error );
                    }
                    break;
                default :
                    error = generateErrorMessage( uri, function, "error received from host: " + response.getStatusLine().getStatusCode() );
                    LOG.error( error );
                    throw new RestException( GlobalErrIds.REST_UNKNOWN_ERR, error );
            }
        }
        catch ( IOException ioe )
        {
            String error = generateErrorMessage( uri, function, "caught IOException=" + ioe.getMessage() );
            LOG.error( error, ioe );
            throw new RestException( GlobalErrIds.REST_IO_ERR, error, ioe );
        }
        catch ( WebApplicationException we )
        {
            String error = generateErrorMessage( uri,function, "caught WebApplicationException=" + we.getMessage() );
            LOG.error( error, we);
            throw new RestException( GlobalErrIds.REST_WEB_ERR, error, we );
        }
        catch ( java.lang.NoSuchMethodError e )
        {
            String error = generateErrorMessage( uri, function, "caught Exception = "+ e.getMessage() );
            LOG.error( error, e );
            throw new RestException( GlobalErrIds.REST_UNKNOWN_ERR, error);
        }
        finally
        {
            // Release current connection to the connection pool.
            post.releaseConnection();
        }
        return szResponse;
    }