You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Loh Kok Jeng <ko...@gmail.com> on 2010/05/21 07:17:13 UTC

Multipart Header Content-Location not Handled Correctly

Dear all,

I'm implementing a REST server using CXF.  I would like to return a
multipart/related response to the client.  Unfortunately, the
Content-Location header of the multipart attachment, e.g.:
"Content-Location: image.jpg", is always set to "content-location:
null".  Below is an example:

--uuid:1fb1641b-124e-4f3f-8598-4a90831d980a
Content-Type: image/jpeg;Name=albumart.jpg
Content-Transfer-Encoding: binary
Content-ID: <81...@TOOLAPDV02>
content-location: null

In my code, the Content-Location header is correctly added to a map,
and the MutipartBody object has been created correctly.  However, the
response received by the client still contains incorrect
"Content-Location" header in the attachments.

Can anyone help?  Thanks.

@Path("/mmsMessage")
@Produces("multipart/related")
public class MmsMessageServices {

	Logger log = Logger.getLogger(getClass());
	MMSReciveMessageClient receiveclient = new MMSReciveMessageClient();
	ErrorBean errorbean = new ErrorBean();
	NsnException exception = new NsnException();

	@GET
	// public List<Attachment> getMessage(@HeaderParam("Authorization") String
	// authorization,@Context MessageContext mc,
	public MultipartBody getMessage(
			@HeaderParam("Authorization") String authorization,
			@Context MessageContext mc,
			@QueryParam("messageIdentifier") String messageIdentifier)
			throws FileNotFoundException {
		log.info("getMessage Rest operation has been called");
		log.info("Requested URI::" + mc.getUriInfo().getRequestUri());
		Map<String, String> credentials = null;
		Iterator<AttachmentPartImpl> attach = null;
		List<Attachment> responseAttach = new LinkedList<Attachment>();
		
		if (messageIdentifier == null) {
			throw new WebApplicationException(400);
		}

		if (authorization == null) {
			throw new WebApplicationException(401);
		} else {
			BasicAuthorizationConversion basicauth = new BasicAuthorizationConversion();
			credentials = basicauth.getCredentials(authorization);
		}
		GetMessage param = new GetMessage();
		try {
			param.setMessageRefIdentifier(messageIdentifier);
			attach = receiveclient.getMessage(param, credentials);

			if (attach.hasNext()) {
				while (attach.hasNext()) {
					MultivaluedMap<String, String> map = new MetadataMap<String, String>();
					AttachmentPartImpl impl = attach.next();

					Iterator<MimeHeader> itor = impl.getAllMimeHeaders();
					while (itor.hasNext()) {
						MimeHeader mimeHdr = (MimeHeader) itor.next();
						map.add(mimeHdr.getName(), mimeHdr.getValue());
					}

					log.info("Incoming Headers: " + map.toString());
					Attachment attachment = new Attachment(impl
							.getDataHandler().getInputStream(), map);

					log.info("Outgoing Headers: "
							+ attachment.getHeaders().toString());
					responseAttach.add(attachment);
					// map.clear();
					// responseAttach.add(new
					// Attachment(impl.getContentId(),impl.getContentType(),impl.getDataHandler().getInputStream()));
				}
				for (int i = 0; i < responseAttach.size(); i++) {
					log.info("responseAttach[" + i + "]: "
							+ responseAttach.get(i).getHeaders().toString());
				}
			}

		} catch (MalformedURLException malexp) {
			log.error("MalformedURLException::" + malexp.getMessage());
		} catch (ServiceException se) {
			log.error("ServiceException::" + se.getMessage());
			errorbean = exception.getServiceException(se);
			throw new ServicePolicyException(errorbean);
		} catch (PolicyException pe) {
			log.error("PolicyException::" + pe.getMessage());
			errorbean = exception.getPolicyException(pe);
			throw new ServicePolicyException(errorbean);
		} catch (SOAPFaultException soapexp) {
			log.error("SOAPFaultException::" + soapexp.getMessage());
			errorbean = exception.getSOAPFaultException(soapexp);
			throw new ServicePolicyException(errorbean);
		} catch (WebApplicationException webexp) {
			log.error("WebApplication exception::" + webexp.getMessage());
		} catch (WebServiceException webserviceexp) {
			if (webserviceexp.getMessage().contains("Unauthorized")) {
				throw new WebApplicationException(401);
			} else {
				log.fatal("WebServiceException::" + webserviceexp.getMessage());
				throw new WebApplicationException(500);
			}
		} catch (Exception exp) {
			log.error("Exception::" + exp.getMessage());
		}
		log.info("response has been succesfully received from toolbox");

		// return responseAttach;
		return new MultipartBody(responseAttach, true);
	}

}

regards,
KJLoh

Re: Multipart Header Content-Location not Handled Correctly

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

I applied a similar fix to 2.2.x/trunk last week

cheers, Sergey

On Mon, May 24, 2010 at 4:15 PM, Daniel Kulp <dk...@apache.org> wrote:

>
> Just a quick note:
>
> We really need to have these attached as patches to JIRA with the "grant to
> apache" box checked.    That (more or less) makes sure we have the legal
> right
> to use the code as well as makes sure the issue is tracked.
>
> Thanks!
> Dan
>
> On Saturday 22 May 2010 12:25:25 am Loh Kok Jeng wrote:
> > I forgot to mention that I'm using CXF 2.2.5.
> >
> > Below are the changes I made to solve the problem:
> >
> >
> apache-cxf-2.2.5\rt\core\src\main\java\org\apache\cxf\attachment\Attachment
> > Impl.java
> >
> > 59c59,60
> > <         headers.put(name.toLowerCase(), value);
> > ---
> >
> > >         //headers.put(name.toLowerCase(), value);
> > >         headers.put(name, value);
> >
> >
> apache-cxf-2.2.5\rt\frontend\jaxrs\src\main\java\org\apache\cxf\jaxrs\ext\M
> > essageContextImpl.java
> >
> > 146,147c146,147
> > <             Attachment handler = (Attachment)handlers.get(i);
> > <             AttachmentImpl att = new
> > AttachmentImpl(handler.getContentId(), handler.getDataHandler());
> > ---
> >
> > >             Attachment handler = (Attachment)handlers.get(i);
> > >             AttachmentImpl att = new
> > >             AttachmentImpl(handler.getContentId(),
> > >             handler.getDataHandler());
> >
> > 148,149c149,150
> > <                 att.setHeader(key, att.getHeader(key));
> > ---
> >
> > >                 att.setHeader(key, handler.getHeader(key));
> >
> > regards,
> > Loh Kok Jeng
> >
> > On 22 May 2010 04:55, Sergey Beryozkin <sb...@gmail.com> wrote:
> > > Hi
> > >
> > > Hi
> > >
> > > I'm pretty sure it is the same issue I've just talked about in the
> other
> > > email which I also believe has just been fixed...
> > >
> > >               GetMessage param = new GetMessage();
> > >
> > >>                try {
> > >>
>  param.setMessageRefIdentifier(messageIdentifier);
> > >>                        attach = receiveclient.getMessage(param,
> > >> credentials);
> > >
> > > At this moment of time you have the attachments received with headers
> in
> > > lower-case
> > >
> > >>                        if (attach.hasNext()) {
> > >>                                while (attach.hasNext()) {
> > >>                                        MultivaluedMap<String, String>
> > >> map = new MetadataMap<String, String>();
> > >>                                        AttachmentPartImpl impl =
> > >> attach.next();
> > >>
> > >>                                        Iterator<MimeHeader> itor =
> > >> impl.getAllMimeHeaders();
> > >>                                        while (itor.hasNext()) {
> > >>                                                MimeHeader mimeHdr =
> > >> (MimeHeader) itor.next();
> > >>
> > >>  map.add(mimeHdr.getName(), mimeHdr.getValue());
> > >>                                        }
> > >
> > > As suggested in the other email I can only think of the following
> > > workaround unless you can try 2.2.9-SNAPSHOT : get headers using
> > > well-known keys and set them using the same well-known keys
> (Content-ID,
> > > Content-Location, etc) hope it helps, Sergey
> > >
> > >> }
> > >>
> > >> regards,
> > >> KJLoh
>
> --
> Daniel Kulp
> dkulp@apache.org
> http://dankulp.com/blog
>

Re: Multipart Header Content-Location not Handled Correctly

Posted by Daniel Kulp <dk...@apache.org>.
Just a quick note:

We really need to have these attached as patches to JIRA with the "grant to 
apache" box checked.    That (more or less) makes sure we have the legal right 
to use the code as well as makes sure the issue is tracked.

Thanks!
Dan

On Saturday 22 May 2010 12:25:25 am Loh Kok Jeng wrote:
> I forgot to mention that I'm using CXF 2.2.5.
> 
> Below are the changes I made to solve the problem:
> 
> apache-cxf-2.2.5\rt\core\src\main\java\org\apache\cxf\attachment\Attachment
> Impl.java
> 
> 59c59,60
> <         headers.put(name.toLowerCase(), value);
> ---
> 
> >         //headers.put(name.toLowerCase(), value);
> >         headers.put(name, value);
> 
> apache-cxf-2.2.5\rt\frontend\jaxrs\src\main\java\org\apache\cxf\jaxrs\ext\M
> essageContextImpl.java
> 
> 146,147c146,147
> <             Attachment handler = (Attachment)handlers.get(i);
> <             AttachmentImpl att = new
> AttachmentImpl(handler.getContentId(), handler.getDataHandler());
> ---
> 
> >             Attachment handler = (Attachment)handlers.get(i);
> >             AttachmentImpl att = new
> >             AttachmentImpl(handler.getContentId(),
> >             handler.getDataHandler());
> 
> 148,149c149,150
> <                 att.setHeader(key, att.getHeader(key));
> ---
> 
> >                 att.setHeader(key, handler.getHeader(key));
> 
> regards,
> Loh Kok Jeng
> 
> On 22 May 2010 04:55, Sergey Beryozkin <sb...@gmail.com> wrote:
> > Hi
> > 
> > Hi
> > 
> > I'm pretty sure it is the same issue I've just talked about in the other
> > email which I also believe has just been fixed...
> > 
> >               GetMessage param = new GetMessage();
> > 
> >>                try {
> >>                        param.setMessageRefIdentifier(messageIdentifier);
> >>                        attach = receiveclient.getMessage(param,
> >> credentials);
> > 
> > At this moment of time you have the attachments received with headers in
> > lower-case
> > 
> >>                        if (attach.hasNext()) {
> >>                                while (attach.hasNext()) {
> >>                                        MultivaluedMap<String, String>
> >> map = new MetadataMap<String, String>();
> >>                                        AttachmentPartImpl impl =
> >> attach.next();
> >> 
> >>                                        Iterator<MimeHeader> itor =
> >> impl.getAllMimeHeaders();
> >>                                        while (itor.hasNext()) {
> >>                                                MimeHeader mimeHdr =
> >> (MimeHeader) itor.next();
> >>                                              
> >>  map.add(mimeHdr.getName(), mimeHdr.getValue());
> >>                                        }
> > 
> > As suggested in the other email I can only think of the following
> > workaround unless you can try 2.2.9-SNAPSHOT : get headers using
> > well-known keys and set them using the same well-known keys (Content-ID,
> > Content-Location, etc) hope it helps, Sergey
> > 
> >> }
> >> 
> >> regards,
> >> KJLoh

-- 
Daniel Kulp
dkulp@apache.org
http://dankulp.com/blog

Re: Multipart Header Content-Location not Handled Correctly

Posted by Loh Kok Jeng <ko...@gmail.com>.
I forgot to mention that I'm using CXF 2.2.5.

Below are the changes I made to solve the problem:

apache-cxf-2.2.5\rt\core\src\main\java\org\apache\cxf\attachment\AttachmentImpl.java

59c59,60
<         headers.put(name.toLowerCase(), value);
---
>         //headers.put(name.toLowerCase(), value);
>         headers.put(name, value);

apache-cxf-2.2.5\rt\frontend\jaxrs\src\main\java\org\apache\cxf\jaxrs\ext\MessageContextImpl.java

146,147c146,147
<             Attachment handler = (Attachment)handlers.get(i);
<             AttachmentImpl att = new
AttachmentImpl(handler.getContentId(), handler.getDataHandler());
---
>             Attachment handler = (Attachment)handlers.get(i);
>             AttachmentImpl att = new AttachmentImpl(handler.getContentId(), handler.getDataHandler());
148,149c149,150
<                 att.setHeader(key, att.getHeader(key));
---
>
>                 att.setHeader(key, handler.getHeader(key));





regards,
Loh Kok Jeng



On 22 May 2010 04:55, Sergey Beryozkin <sb...@gmail.com> wrote:
> Hi
>
> Hi
>
> I'm pretty sure it is the same issue I've just talked about in the other
> email which I also believe has just been fixed...
>
>               GetMessage param = new GetMessage();
>>                try {
>>                        param.setMessageRefIdentifier(messageIdentifier);
>>                        attach = receiveclient.getMessage(param,
>> credentials);
>>
>>
> At this moment of time you have the attachments received with headers in
> lower-case
>
>
>>                        if (attach.hasNext()) {
>>                                while (attach.hasNext()) {
>>                                        MultivaluedMap<String, String> map =
>> new MetadataMap<String, String>();
>>                                        AttachmentPartImpl impl =
>> attach.next();
>>
>>                                        Iterator<MimeHeader> itor =
>> impl.getAllMimeHeaders();
>>                                        while (itor.hasNext()) {
>>                                                MimeHeader mimeHdr =
>> (MimeHeader) itor.next();
>>                                                map.add(mimeHdr.getName(),
>> mimeHdr.getValue());
>>                                        }
>>
>
> As suggested in the other email I can only think of the following workaround
> unless you can try 2.2.9-SNAPSHOT : get headers using well-known keys and
> set them using the same well-known keys (Content-ID, Content-Location, etc)
> hope it helps, Sergey
>
>
>> }
>>
>> regards,
>> KJLoh
>>
>

Re: Multipart Header Content-Location not Handled Correctly

Posted by Sergey Beryozkin <sb...@gmail.com>.
Hi

Hi

I'm pretty sure it is the same issue I've just talked about in the other
email which I also believe has just been fixed...

               GetMessage param = new GetMessage();
>                try {
>                        param.setMessageRefIdentifier(messageIdentifier);
>                        attach = receiveclient.getMessage(param,
> credentials);
>
>
At this moment of time you have the attachments received with headers in
lower-case


>                        if (attach.hasNext()) {
>                                while (attach.hasNext()) {
>                                        MultivaluedMap<String, String> map =
> new MetadataMap<String, String>();
>                                        AttachmentPartImpl impl =
> attach.next();
>
>                                        Iterator<MimeHeader> itor =
> impl.getAllMimeHeaders();
>                                        while (itor.hasNext()) {
>                                                MimeHeader mimeHdr =
> (MimeHeader) itor.next();
>                                                map.add(mimeHdr.getName(),
> mimeHdr.getValue());
>                                        }
>

As suggested in the other email I can only think of the following workaround
unless you can try 2.2.9-SNAPSHOT : get headers using well-known keys and
set them using the same well-known keys (Content-ID, Content-Location, etc)
hope it helps, Sergey


> }
>
> regards,
> KJLoh
>