AXIS2_EXTERN axis2_status_t AXIS2_CALL
axis2_simple_http_svr_conn_write_response(
    axis2_simple_http_svr_conn_t * svr_conn,
    const axutil_env_t * env,
    axis2_http_simple_response_t * response)
{
    axis2_http_response_writer_t *response_writer = NULL;
    axutil_array_list_t *headers = NULL;
    axutil_stream_t *response_stream = NULL;
    axis2_char_t *response_body = NULL;
    int body_size = 0;

    axis2_http_header_t *enc_header = NULL;
    axis2_bool_t chuked_encoding = AXIS2_FALSE;
    axis2_char_t *status_line = NULL;
    axis2_bool_t binary_content = AXIS2_FALSE;
    axis2_char_t *content_type = NULL;

    AXIS2_PARAM_CHECK(env->error, response, AXIS2_FAILURE);

    response_writer = axis2_http_response_writer_create(env, svr_conn->stream);
    if(!response_writer)
    {
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Cannot create http response writer");
        return AXIS2_FAILURE;
    }

    content_type = (axis2_char_t *)axis2_http_simple_response_get_content_type(response, env);
    if(content_type)
    {
        if(strstr(content_type, AXIS2_HTTP_HEADER_ACCEPT_MULTIPART_RELATED)
            && strstr(content_type,AXIS2_HTTP_HEADER_ACCEPT_XOP_XML))
        {
            binary_content = AXIS2_TRUE;
        }
    }

    enc_header = axis2_http_simple_response_get_first_header(response, env,
        AXIS2_HTTP_HEADER_TRANSFER_ENCODING);
    if(enc_header)
    {
        axis2_char_t *enc_value = axis2_http_header_get_value(enc_header, env);
        if(enc_value && (0 == axutil_strcmp(enc_value, AXIS2_HTTP_HEADER_TRANSFER_ENCODING_CHUNKED)))
        {
            chuked_encoding = AXIS2_TRUE;

            /* remove the content length header */
            axis2_http_simple_response_remove_headers(response, env,
                AXIS2_HTTP_HEADER_CONTENT_LENGTH);
        }
    }

    /* print status line */
    status_line = axis2_http_simple_response_get_status_line(response, env);
    if(!status_line)
    {
        AXIS2_HANDLE_ERROR(env, AXIS2_ERROR_INVALID_HTTP_HEADER_START_LINE, AXIS2_FAILURE);
        axis2_http_response_writer_free(response_writer, env);
        return AXIS2_FAILURE;
    }
    axis2_http_response_writer_print_str(response_writer, env, status_line);

    headers = axis2_http_simple_response_get_headers(response, env);
    if(headers)
    {
        int i = 0;
        int count = axutil_array_list_size(headers, env);
        for(; i < count; i++)
        {
            axis2_http_header_t *header =
                (axis2_http_header_t *)axutil_array_list_get(headers, env, i);
            if(header)
            {
                axis2_char_t *header_ext_form = axis2_http_header_to_external_form(header, env);
                axis2_http_response_writer_print_str(response_writer, env, header_ext_form);
                AXIS2_FREE(env->allocator, header_ext_form);
            }
        }
    }

    /* write empty line after http headers */
    axis2_http_response_writer_print_str(response_writer, env, AXIS2_HTTP_CRLF);

    /* write the body */
    response_stream = axis2_http_simple_response_get_body(response, env);
    if(response_stream)
    {
        body_size = axutil_stream_get_len(response_stream, env);
        response_body = axutil_stream_get_buffer(response_stream, env);
        axutil_stream_flush_buffer(response_stream, env);
        response_body[body_size] = AXIS2_ESC_NULL;
    }

    if(body_size <= 0 && !binary_content)
    {
        /* no body available to write. Note that this is not an error. We might want to write only
         * status information and hence, this is a valid case */
        axis2_http_response_writer_free(response_writer, env);
        return AXIS2_SUCCESS;
    }

    if(!chuked_encoding && !binary_content)
    {
        /* This sending a normal SOAP response without chunk transfer encoding */
        axis2_status_t write_stat = AXIS2_FAILURE;
        write_stat = axis2_http_response_writer_println_str(response_writer, env, response_body);
        if(write_stat != AXIS2_SUCCESS)
        {
            AXIS2_HANDLE_ERROR(env, AXIS2_ERROR_WRITING_RESPONSE, AXIS2_FAILURE);
            axis2_http_response_writer_free(response_writer, env);
            return AXIS2_FAILURE;
        }
    }
    else if(!binary_content)
    {
        /* Sending a normal SOAP response enabling http chunking */
        axutil_http_chunked_stream_t *chunked_stream = NULL;
        int left = body_size;
        chunked_stream = axutil_http_chunked_stream_create(env, svr_conn->stream);
        while(left > 0)
        {
            int len = -1;
            len = axutil_http_chunked_stream_write(chunked_stream, env, response_body, body_size);
            if(len <= 0)
            {
                AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "cannot write data to chunked stream");
                axutil_http_chunked_stream_free(chunked_stream, env);
                axis2_http_response_writer_free(response_writer, env);
                return AXIS2_FAILURE;
            }
            left -= len;
        }
        axutil_http_chunked_stream_write_last_chunk(chunked_stream, env);
        axutil_http_chunked_stream_free(chunked_stream, env);
    }
    else
    {
        /* In the MTOM case we enable chunking in order to send the attachment */
        axutil_http_chunked_stream_t *chunked_stream = NULL;
        axis2_status_t write_stat = AXIS2_FAILURE;
        axutil_array_list_t *mime_parts = NULL;
        axis2_char_t *mtom_sending_callback_name = NULL;

        mime_parts = axis2_http_simple_response_get_mime_parts(response, env);
        if(!mime_parts)
        {
            AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "No mime parts are given");
            axis2_http_response_writer_free(response_writer, env);
            return AXIS2_FAILURE;
        }

        /* If the callback name is not there, then we will check whether there 
         * is any mime_parts which has type callback. If we found then no point 
         * of continuing we should return a failure */
        mtom_sending_callback_name = axis2_http_simple_response_get_mtom_sending_callback_name(
            response, env);
        if(!mtom_sending_callback_name)
        {
            if(axis2_http_transport_utils_is_callback_required(env, mime_parts))
            {
                AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Sender callback not specified");
                axis2_http_response_writer_free(response_writer, env);
                return AXIS2_FAILURE;
            }
        }

        chunked_stream = axutil_http_chunked_stream_create(env, svr_conn->stream);
        write_stat = axis2_http_transport_utils_send_mtom_message(chunked_stream, env, mime_parts,
            mtom_sending_callback_name);
        axutil_http_chunked_stream_free(chunked_stream, env);

        if(write_stat != AXIS2_SUCCESS)
        {
            AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "writing mime parts failed");
            axis2_http_response_writer_free(response_writer, env);
            return AXIS2_FAILURE;
        }
    }

    axis2_http_response_writer_free(response_writer, env);
    return AXIS2_SUCCESS;
}
Beispiel #2
0
/* Process a request. If the request has a response the response structure will be populated */
AXIS2_EXTERN axis2_bool_t AXIS2_CALL
axis2_udp_receiver_process_request(
	const axutil_env_t * env,
	axis2_conf_ctx_t *conf_ctx,    
    axis2_udp_request_t * request,
	axis2_udp_response_t * responce)
{   
    axis2_transport_out_desc_t *out_desc = NULL;
    axis2_transport_in_desc_t *in_desc = NULL;
    axis2_msg_ctx_t *msg_ctx = NULL;
    axiom_xml_reader_t *reader = NULL;
    axiom_stax_builder_t *builder = NULL;
    axiom_soap_builder_t *soap_builder = NULL;
    axiom_soap_envelope_t *soap_envelope = NULL;
    axis2_engine_t *engine = NULL;
    axis2_status_t status = AXIS2_FALSE;
    axutil_stream_t *out_stream = NULL;

    AXIS2_LOG_TRACE(env->log, AXIS2_LOG_SI,
                    "start:axis2_udp_worker_process_request");

    if (!conf_ctx)
    {
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "conf ctx not available");
        return AXIS2_FAILURE;
    }

    reader = axiom_xml_reader_create_for_memory(env, request->buff,
                                                request->buf_size,
                                                NULL,
                                                AXIS2_XML_PARSER_TYPE_BUFFER);
    if (!reader)
    {
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Failed to create XML reader");
        return AXIS2_FAILURE;
    }

    builder = axiom_stax_builder_create(env, reader);
    if (!builder)
    {
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI,
                        "Failed to create Stax builder");
        return AXIS2_FAILURE;
    }

    soap_builder = axiom_soap_builder_create(env, builder,
                                             AXIOM_SOAP12_SOAP_ENVELOPE_NAMESPACE_URI);

    if (!soap_builder)
    {
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI,
                        "Failed to create SOAP builder");
        return AXIS2_FAILURE;
    }    

    out_desc =
        axis2_conf_get_transport_out(axis2_conf_ctx_get_conf(conf_ctx, env),
                                     env, AXIS2_TRANSPORT_ENUM_UDP);
    if (!out_desc)
    {
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Transport out not set");
        return AXIS2_FAILURE;
    }

    in_desc =
        axis2_conf_get_transport_in(axis2_conf_ctx_get_conf(conf_ctx, env), env,
                                    AXIS2_TRANSPORT_ENUM_UDP);

    msg_ctx = axis2_msg_ctx_create(env, conf_ctx, in_desc, out_desc);
    axis2_msg_ctx_set_server_side(msg_ctx, env, AXIS2_TRUE);

    out_stream = axutil_stream_create_basic(env);
    axis2_msg_ctx_set_transport_out_stream(msg_ctx, env, out_stream);

    soap_envelope = axiom_soap_builder_get_soap_envelope(soap_builder, env);
    axis2_msg_ctx_set_soap_envelope(msg_ctx, env, soap_envelope);
	if (request->svc)
	{
		axis2_msg_ctx_set_svc(msg_ctx, env, request->svc);
	}
	if (request->op)
	{
		axis2_msg_ctx_set_op(msg_ctx, env, request->op);	
	}
    engine = axis2_engine_create(env, conf_ctx);
    status = axis2_engine_receive(engine, env, msg_ctx);

    responce->buff = axutil_stream_get_buffer(out_stream, env);
	responce->buf_size = axutil_stream_get_len(out_stream, env);
    AXIS2_LOG_TRACE(env->log, AXIS2_LOG_SI,
                    "end:axis2_udp_worker_process_request");
    return AXIS2_SUCCESS;
}