How to add user HTTP Headers in CXF response? - web-services

For some reason there is no 'Content-Length' HTTP headers in CXF response.
I've decided to implement interceptor that does it, but unfortunately there is no added header in sent data (intercepted using Wireshark). I can set breakpoint and i can see interceptor is invoked. What's wrong?
/**
* Adds Content-Length header for the outcoming messages
*/
public class AddContentLengthInterceptor extends AbstractLoggingInterceptor {
private static final String CONTENT_LENGTH_ADDED = AddContentLengthInterceptor.class.getName() + ".log-setup";
public AddContentLengthInterceptor(String phase) {
super(phase);
addBefore(StaxOutInterceptor.class.getName());
}
public AddContentLengthInterceptor() {
this(Phase.PRE_PROTOCOL); // before streaming
}
#Override
protected Logger getLogger() {
return null;
}
#Override
public void handleMessage(Message message) throws Fault {
final OutputStream os = message.getContent(OutputStream.class);
final Writer iowriter = message.getContent(Writer.class);
if (os == null && iowriter == null) {
return;
}
// ignore double processing of the message
boolean hasAddedHeader = message.containsKey(CONTENT_LENGTH_ADDED);
if (!hasAddedHeader) {
message.put(CONTENT_LENGTH_ADDED, Boolean.TRUE);
if (os != null) {
// Write the output while caching it for adding header later
final CacheAndWriteOutputStream newOut = new CacheAndWriteOutputStream(os);
message.setContent(OutputStream.class, newOut);
newOut.registerCallback(new LoggingCallback(message, os));
}
}
}
class LoggingCallback implements CachedOutputStreamCallback {
private final Message message;
private final OutputStream origStream;
public LoggingCallback(final Message msg, final OutputStream os) {
this.message = msg;
this.origStream = os;
}
public void onFlush(CachedOutputStream cos) {
}
public void onClose(CachedOutputStream cos) {
long contentLength = cos.size();
Map<String, List<String>> headers = (Map<String, List<String>>) message.get(Message.PROTOCOL_HEADERS);
if (headers == null)
headers = new HashMap<String, List<String>>();
headers.put("Content-Length", Arrays.asList(String.valueOf(contentLength)));
message.put(Message.PROTOCOL_HEADERS, headers);
try {
// empty out the cache
cos.lockOutputStream();
cos.resetOut(null, false);
} catch (Exception ex) {
//ignore
}
message.setContent(OutputStream.class, origStream);
}
}
}
This is now server-side endpoints are created:
mediaService = new MediaService(ip, rtspPort, streamUri);
ProviderImpl provider = new ProviderImpl();
mediaEndpoint = (EndpointImpl) provider.createEndpoint(null, mediaService);
String mediaServiceURL = MessageFormat.format("http://{0}:{1}/onvif/media_service", ip, String.valueOf(port));
mediaEndpoint.publish(mediaServiceURL);
// add "Content-Length" header
mediaEndpoint.getServer().getEndpoint().getOutInterceptors().add(contentLengthInterceptor);

I think you are using Transfer-Encoding as chunked. Which results in no content-length header since it is defined that way in RFC.
Messages MUST NOT include both a Content-Length header field and a
non-identity transfer-coding. If the message does include a
non-identity transfer-coding, the Content-Length MUST be ignored.
I am not exactly sure but, either cxf removes your header since it is not allowed or it sets but sub layers remove it. As far as I know cxf uses HttpUrlConnection to transport your message. And that layer sets Content-length header on normal cases. But since you are using chunked transfer, that layer may override it.
In order to fix it you must change your Transfer-Encoding. And since CXF's itself sets content-length header you are no longer need to write your Interceptor.
By the way, if you use a custom header, you will realise that your interceptor works like a charm.

Related

How to get download file size before download using C/C++ in Linux environment [duplicate]

I want to get the size of an http:/.../file before I download it. The file can be a webpage, image, or a media file. Can this be done with HTTP headers? How do I download just the file HTTP header?
Yes, assuming the HTTP server you're talking to supports/allows this:
public long GetFileSize(string url)
{
long result = -1;
System.Net.WebRequest req = System.Net.WebRequest.Create(url);
req.Method = "HEAD";
using (System.Net.WebResponse resp = req.GetResponse())
{
if (long.TryParse(resp.Headers.Get("Content-Length"), out long ContentLength))
{
result = ContentLength;
}
}
return result;
}
If using the HEAD method is not allowed, or the Content-Length header is not present in the server reply, the only way to determine the size of the content on the server is to download it. Since this is not particularly reliable, most servers will include this information.
Can this be done with HTTP headers?
Yes, this is the way to go. If the information is provided, it's in the header as the Content-Length. Note, however, that this is not necessarily the case.
Downloading only the header can be done using a HEAD request instead of GET. Maybe the following code helps:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://example.com/");
req.Method = "HEAD";
long len;
using(HttpWebResponse resp = (HttpWebResponse)(req.GetResponse()))
{
len = resp.ContentLength;
}
Notice the property for the content length on the HttpWebResponse object – no need to parse the Content-Length header manually.
Note that not every server accepts HTTP HEAD requests. One alternative approach to get the file size is to make an HTTP GET call to the server requesting only a portion of the file to keep the response small and retrieve the file size from the metadata that is returned as part of the response content header.
The standard System.Net.Http.HttpClient can be used to accomplish this. The partial content is requested by setting a byte range on the request message header as:
request.Headers.Range = new RangeHeaderValue(startByte, endByte)
The server responds with a message containing the requested range as well as the entire file size. This information is returned in the response content header (response.Content.Header) with the key "Content-Range".
Here's an example of the content range in the response message content header:
{
"Key": "Content-Range",
"Value": [
"bytes 0-15/2328372"
]
}
In this example the header value implies the response contains bytes 0 to 15 (i.e., 16 bytes total) and the file is 2,328,372 bytes in its entirety.
Here's a sample implementation of this method:
public static class HttpClientExtensions
{
public static async Task<long> GetContentSizeAsync(this System.Net.Http.HttpClient client, string url)
{
using (var request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Get, url))
{
// In order to keep the response as small as possible, set the requested byte range to [0,0] (i.e., only the first byte)
request.Headers.Range = new System.Net.Http.Headers.RangeHeaderValue(from: 0, to: 0);
using (var response = await client.SendAsync(request))
{
response.EnsureSuccessStatusCode();
if (response.StatusCode != System.Net.HttpStatusCode.PartialContent)
throw new System.Net.WebException($"expected partial content response ({System.Net.HttpStatusCode.PartialContent}), instead received: {response.StatusCode}");
var contentRange = response.Content.Headers.GetValues(#"Content-Range").Single();
var lengthString = System.Text.RegularExpressions.Regex.Match(contentRange, #"(?<=^bytes\s[0-9]+\-[0-9]+/)[0-9]+$").Value;
return long.Parse(lengthString);
}
}
}
}
WebClient webClient = new WebClient();
webClient.OpenRead("http://stackoverflow.com/robots.txt");
long totalSizeBytes= Convert.ToInt64(webClient.ResponseHeaders["Content-Length"]);
Console.WriteLine((totalSizeBytes));
HttpClient client = new HttpClient(
new HttpClientHandler() {
Proxy = null, UseProxy = false
} // removes the delay getting a response from the server, if you not use Proxy
);
public async Task<long?> GetContentSizeAsync(string url) {
using (HttpResponseMessage responce = await client.GetAsync(url))
return responce.Content.Headers.ContentLength;
}

Issue with a WS verifier method when migrating from Play 2.4 to Play 2.5

I have a method I need to refactor, as F.Promise has been deprecated in Play 2.5. It's pretty readable actually. It sends a request and authenticates via a custom security token and returns true if the response is 200.
public boolean verify(final String xSassToken){
WSRequest request = WS.url(mdVerifyXSassTokenURL)
.setHeader("X-SASS", xSassToken)
.setMethod("GET");
final F.Promise<WSResponse> responsePromise = request.execute();
try {
final WSResponse response = responsePromise.get(10000);
int status = response.getStatus();
if(status == 200 ) { //ok
return true;
}
} catch (Exception e) {
return false;
}
return false;
}
First thing I had to do was change this line:
final F.Promise<WSResponse> responsePromise = request.execute();
To this:
final CompletionStage<WSResponse> responsePromise = request.execute();
However, CompletionStage(T) doesn't have an equivalent get() method so I'm not sure the quickest and easiest way to get a WSResponse that I can verify the status of.
Yes, it does not. At least not directly.
What you are doing is "wrong" in the context of PlayFramework. get is a blocking call and you should avoid blocking as much as possible. That is why WS offers a non blocking API and a way to handle asynchronous results. So, first, you should probably rewrite your verify code to be async:
public CompletionStage<Boolean> verify(final String xSassToken) {
return WS.url(mdVerifyXSassTokenURL)
.setHeader("X-SASS", xSassToken)
.setMethod("GET")
.execute()
.thenApply(response -> response.getStatus() == Http.Status.OK);
}
Notice how I'm using thenApply to return a new a java.util.concurrent.CompletionStage instead of a plain boolean. That means that the code calling verify can also do the same. Per instance, an action at your controller can do something like this:
public class MyController extends Controller {
public CompletionStage<Result> action() {
return verify("whatever").thenApply(success -> {
if (success) return ok("successful request");
else return badRequest("xSassToken was not valid");
});
}
public CompletionStage<Boolean> verify(final String xSassToken) { ... }
}
This way your application will be able to handle a bigger workload without hanging.
Edit:
Since you have to maintain compatibility, this is what I would do to both evolve the design and also to keep code compatible while migrating:
/**
* #param xSassToken the token to be validated
* #return if the token is valid or not
*
* #deprecated Will be removed. Use {#link #verifyToken(String)} instead since it is non blocking.
*/
#Deprecated
public boolean verify(final String xSassToken) {
try {
return verifyToken(xSassToken).toCompletableFuture().get(10, TimeUnit.SECONDS);
} catch (Exception e) {
return false;
}
}
public CompletionStage<Boolean> verifyToken(final String xSassToken) {
return WS.url(mdVerifyXSassTokenURL)
.setHeader("X-SASS", xSassToken)
.setMethod("GET")
.execute()
.thenApply(response -> response.getStatus() == Http.Status.OK);
}
Basically, deprecate the old verify method and suggest users to migrate to new one.

Web Service Serialization produces an unwanted root in SOAP body

History:
A WSDL was provided to me with which I generated a service reference.
The object to serialize and send to the web service to consume, was a strongly, complex-typed class.
Instead of sending a strongly, complex-typed class, I wanted to send an XMLDocument instead.
So I modified the service and I am left with this:
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.17929")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name = "B2MML_ProcessProductionPerformance_MaterialConsumption_OBBinding", Namespace="http://company.com/M2D/Manufacturing/ManufacturingExecution/GoodsIssue/pi")]
public partial class B2MML_ProcessProductionPerformance_MaterialConsumption_OBService : System.Web.Services.Protocols.SoapHttpClientProtocol {
private System.Threading.SendOrPostCallback B2MML_ProcessProductionPerformance_MaterialConsumption_OBOperationCompleted;
public B2MML_ProcessProductionPerformance_MaterialConsumption_OBService()
{
}
public event B2MML_ProcessProductionPerformance_MaterialConsumption_OBCompletedEventHandler B2MML_ProcessProductionPerformance_MaterialConsumption_OBCompleted;
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://sap.com/xi/WebService/soap1.1", OneWay=true, Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Bare)]
public void B2MML_ProcessProductionPerformance_MaterialConsumption_OB(XmlDocument ProcessProductionPerformance)
{
this.Invoke("B2MML_ProcessProductionPerformance_MaterialConsumption_OB", new object[] {
ProcessProductionPerformance});
}
public System.IAsyncResult BeginB2MML_ProcessProductionPerformance_MaterialConsumption_OB(XmlDocument ProcessProductionPerformance, System.AsyncCallback callback, object asyncState)
{
return this.BeginInvoke("B2MML_ProcessProductionPerformance_MaterialConsumption_OB", new object[] {
ProcessProductionPerformance}, callback, asyncState);
}
public void EndB2MML_ProcessProductionPerformance_MaterialConsumption_OB(System.IAsyncResult asyncResult)
{
this.EndInvoke(asyncResult);
}
public void B2MML_ProcessProductionPerformance_MaterialConsumption_OBAsync(XmlDocument ProcessProductionPerformance)
{
this.B2MML_ProcessProductionPerformance_MaterialConsumption_OBAsync(ProcessProductionPerformance, null);
}
public void B2MML_ProcessProductionPerformance_MaterialConsumption_OBAsync(XmlDocument ProcessProductionPerformance, object userState)
{
if ((this.B2MML_ProcessProductionPerformance_MaterialConsumption_OBOperationCompleted == null))
{
this.B2MML_ProcessProductionPerformance_MaterialConsumption_OBOperationCompleted = new System.Threading.SendOrPostCallback(this.OnB2MML_ProcessProductionPerformance_MaterialConsumption_OBOperationCompleted);
}
this.InvokeAsync("B2MML_ProcessProductionPerformance_MaterialConsumption_OB", new object[] {
ProcessProductionPerformance}, this.B2MML_ProcessProductionPerformance_MaterialConsumption_OBOperationCompleted, userState);
}
private void OnB2MML_ProcessProductionPerformance_MaterialConsumption_OBOperationCompleted(object arg)
{
if ((this.B2MML_ProcessProductionPerformance_MaterialConsumption_OBCompleted != null))
{
System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
this.B2MML_ProcessProductionPerformance_MaterialConsumption_OBCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
}
}
public new void CancelAsync(object userState)
{
base.CancelAsync(userState);
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.17929")]
public delegate void B2MML_ProcessProductionPerformance_MaterialConsumption_OBCompletedEventHandler(object sender, System.ComponentModel.AsyncCompletedEventArgs e);
Problem Decription:
I am able to send an XMLDocument successfully, however, if my XMLDocument looks like this:
<ProcessProductionPerformance xmlns="SomeNS">
<ApplicationArea>Some data here</ApplicationArea>
<DataArea>Some data there</DataArea>
</ProcessProductionPerformance>
I am seeing the below in the SOAP Body (seen via Fiddler):
<ProcessProductionPerformance>
<ProcessProductionPerformance xmlns="SomeNS">
<ApplicationArea>Some data here</ApplicationArea>
<DataArea>Some data there</DataArea>
</ProcessProductionPerformance>
</ProcessProductionPerformance>
Information:
1) I do not have access to modify the web service
2) I've tried to pack XMLDocument ApplicationArea and XMLDocument DataArea individually into a class and try to serialize the class, I end up with this in the SOAP Body:
<ProcessProductionPerformance xmlns="SomeNS">
<ApplicationArea>
<ApplicationArea xmlns="SomeNS">Some data here</ApplicationArea>
<ApplicationArea>
<DataArea>
<DataArea xmlns="SomeNS">Some data there</DataArea>
<DataArea>
</ProcessProductionPerformance>
3) I suspect it's to do with the SOAPBindingUse or SOAPBindingStyle perhaps? I did not change this at all because I don't know much about it.
4) I'm just a kid new to C#.. Please have mercy.
EDIT:
Okay it seems that the enums SoapBindingUse.Literal and SoapParameterStyle.Bare have a major role to play in this. But I'm still stuck because my use of this has been correct all along.
https://msdn.microsoft.com/en-us/library/vstudio/2b4bx2t6%28v=vs.100%29.aspx
So what am I missing?
By the way, not sure if you humans care about the client but I'm calling the service like this:
var processProductionPerformance = new XmlDocument();
processProductionPerformance.LoadXml(#xmlText);
var sendPerformanceToSap = new B2MML_ProcessProductionPerformance_MaterialConsumption_OBService
{
//Url = Link here,
//Credentials since
};
sendPerformanceToSap.B2MML_ProcessProductionPerformance_MaterialConsumption_OBAsync(processProductionPerformance);

How to send additional fields to soap handler along with soapMessage?

I am logging RequestXML for a webservice client using SoapHandler as follows
public boolean handleMessage(SOAPMessageContext smc) {
logToSystemOut(smc);
return true;
}
private void logToSystemOut(SOAPMessageContext smc) {
Boolean outboundProperty = (Boolean)
smc.get (MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) {
out.println("\nOutbound message:");
} else {
out.println("\nInbound message:");
}
SOAPMessage message = smc.getMessage();
try {
message.writeTo(out);
out.println("");
} catch (Exception e) {
out.println("Exception in handler: " + e);
}
}
Got a new requirenment to add this xml to DB along with some extra values(which are not present in the xml). Is there any way I can pass few additional fields to above soap handler (in handleMessage method)?
Please note that changing the xml/WSDL or adding this to SOAP message header is not an option for me as it is owned by other interface. Any other solution?
Thanks!
You can cast your service class to a class of type "BindingProvider". In this form you can use it to assign it objects which you can access later from your SOAPHandler. Another useful usage is that you also can change the endPoint URL this way.
Before calling the service you do:
MySoapServicePortType service = new MySoapService().getMySoapServicePort();
BindingProvider bp = (BindingProvider)service;
MyTransferObject t = new MyTransferObject();
bp.getRequestContext().put("myTransferObject", t);
TypeResponse response = service.doRequest();
SOAPMessage message = t.getRequestMessage(message);
From your logging function you do:
private void logToSystemOut(SOAPMessageContext smc) {
...
MyTransferObject t = (MyTransferObject) messageContext.get("myTransferObject");
if (outboundProperty.booleanValue())
t.setRequestMessage(message);
else
t.setResponseMessage(message);
...
}

Custom webservice in Umbraco 4.5 giving odd error

We've created a custom webservice in Umbraco to add (async) files and upload them. After upload the service is called with node and file-information to add a new node to the content tree.
At first our main problem was that the service was running outside of the Umbraco context, giving strange errors with get_currentuser.
Now, we inherit the umbraco BaseWebService from the umbraco.webservices dll and we've set all acces information in the settings file; we authenticatie before doing anything else using (correct and ugly-hardcoded) administrator.
When we now execute the webservice (from the browser or anything else) we get:
at umbraco.DataLayer.SqlHelper`1.ExecuteReader(String commandText, IParameter[] parameters)
at umbraco.cms.businesslogic.CMSNode.setupNode()
at umbraco.cms.businesslogic.web.Document.setupNode()
at umbraco.cms.businesslogic.CMSNode..ctor(Int32 Id)
at umbraco.cms.businesslogic.Content..ctor(Int32 id)
at umbraco.cms.businesslogic.web.Document..ctor(Int32 id)
at FileUpload.AddDocument(String ProjectID, String NodeID, String FileName)*
Where AddDocument is our method. The node (filename w/o extension) does not exist in the tree (not anywhere, it's a new filename/node). We've cleared the recycle bin, so it's not in there either.
Are we missing something vital, does anyone has a solution?
Below is the source for the webservice;
using umbraco.cms.businesslogic.web;
using umbraco.BusinessLogic;
using umbraco.presentation.nodeFactory;
using umbraco.cms.businesslogic.member;
using umbraco.cms;
/// <summary>
/// Summary description for FileUpload
/// </summary>
[WebService(Namespace = "http://umbraco.org/webservices/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class FileUpload : umbraco.webservices.BaseWebService //System.Web.Services.WebService
{
private string GetMimeType(string fileName)
{
string mimeType = "application/unknown";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
return mimeType;
}
[WebMethod]
public string HelloWorld() {
return "Hello World";
}
[WebMethod]
public void AddDocument(string ProjectID, string NodeID, string FileName)
{
Authenticate("***", "***");
string MimeType = GetMimeType(FileName); //"application/unknown";
// Create node
int nodeId = 1197;
string fileName = System.IO.Path.GetFileNameWithoutExtension(#"*****\Upload\" + FileName);
string secGroups = "";
//EDIT DUE TO COMMENT: Behavior remains the same though
Document node = umbraco.cms.businesslogic.web.Document.MakeNew(fileName.Replace(".", ""), new DocumentType(1049), umbraco.BusinessLogic.User.GetUser(0), nodeId);
secGroups = "Intern";
StreamWriter sw = null;
try
{
//EXCEPTION IS THROWN SOMEWHERE HERE
Document doc = NodeLevel.CreateNode(fileName, "Bestand", nodeId);
doc.getProperty("bestandsNaam").Value = fileName;
byte[] buffer = System.IO.File.ReadAllBytes(#"****\Upload\" + FileName);
int projectId = 0;
int tempid = nodeId;
//EXCEPTION IS THROWN TO THIS POINT (SEE BELOW)
try
{
Access.ProtectPage(false, doc.Id, 1103, 1103);
Access.AddMembershipRoleToDocument(doc.Id, secGroups);
}
catch (Exception ex)
{
// write to file
}
try
{
doc.Publish(umbraco.BusinessLogic.User.GetUser(0));
umbraco.library.UpdateDocumentCache(doc.Id);
umbraco.content.Instance.RefreshContentFromDatabaseAsync();
}
catch (Exception ex)
{
// write to file
}
System.IO.File.Delete(FileName);
}
catch (Exception ex)
{
// THIS EXCEPTION IS CAUGHT!!
}
}
public override umbraco.webservices.BaseWebService.Services Service
{
get { return umbraco.webservices.BaseWebService.Services.DocumentService; }
}
}
If anyone has a solution, pointer, hint or whatever; help is appreciated!!
TIA,
riffnl
We've rewritten the whole procedure (dumped all code and restart) and we've got it working now.
I think we've been messing around with the old code so much in trying to get it to work we were missing some key issues, because it functions.
Thanks for thinking along anyway!