How do I pass username and password in SecuredAuthenticationSoapHeader using PowerShell? - powershell

I have a SOAP method called AuthenticateUser() that is defined in C# like this
[WebMethod(Description = "Returns a token string given a username and password.")]
[System.Web.Services.Protocols.SoapHeader(nameof(AuthenticationHeader))]
public AuthenticationResponse AuthenticateUser()
{
...
}
where AuthenticationHeader is type
public SecuredAuthenticationSoapHeader AuthenticationHeader;
I'm trying to make a call to this method to get a token so I can use the token to make calls to other methods in the webservice.
When I make this call in SOAP-UI the XML looks like this
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="https://www.example.com/webservices">
<soap:Header>
<web:SecuredAuthenticationSoapHeader>
<web:UserName>myusername</web:UserName>
<web:Password>mypassword</web:Password>
</web:SecuredAuthenticationSoapHeader>
</soap:Header>
<soap:Body>
<web:AuthenticateUser/>
</soap:Body>
</soap:Envelope>
However how can I do this in PowerSHell using Invoke-WebRequest or New-WebServiceProxy?
Is there an alternative way in which I just pass the XML as I do in SOAP-UI?

Related

WSO2 MSF4J receive large data in #Post

We are using wso2 microservice engine as middleware between WSO2 EI and DB. The target chain MQ->WSO EI->msf4j->DB. DTO which is transfered - formatted xml string which basically shouldn't be parsed till msf4j layer. So, for all this is just string. Sometimes this is a big string.
Our code
#Path("/accountdao")
public class AccountDaoService implements Microservice {
#POST
#Path("/getbyparam")
#Produces(MediaType.TEXT_PLAIN)
public String getAllAccounts(#QueryParam("commandText") String commandText) {
Previously we tested it in GET's style calls, I mean smth like
URL url = new URL(serverURL + requestURL + "/?commandText=" +
URLEncoder.encode(request,"UTF-8"));
Cause in all other styles, using let's say HttpClient from commons, we didn't receive data in commandText.
And I've found, that I don't know how to pass large data using SoapUI or just clear java client..
With small text blocks(like 200-300 chars) is all ok, but with 6k lenght this is already problem.
Is it possible to handle in msf4j large strings or we should use for it smth else?
thanks.
ps
probably we should use #FormParam & multipart/form-data?
UPDATE 1
<payloadFactory media-type="xml">
<format>
<uri.var.commandText xmlns="">$1</uri.var.commandText>
</format>
<args>
<arg evaluator="xml" expression="get-property('uri.var.commandText')"/>
</args>
</payloadFactory>
<call blocking="true" description="">
<endpoint key="microserviceEP"/>
</call>
and microservice code
#POST
#Path("/getclientcontract")
#Produces(MediaType.TEXT_PLAIN)
public String getClientContract(#Context Request request) {
List<ByteBuffer> fullMessageBody = request.getFullMessageBody();
StringBuilder sb = new StringBuilder();
for (ByteBuffer byteBuffer : fullMessageBody) {
sb.append(StandardCharsets.UTF_8.decode(byteBuffer).toString());
}
String commandText = sb.toString();
is it ok, or there is possible more correct way?

Send http request with encoded URI

I m trying to send http request as follows, using citrus test framework http://www.citrusframework.org/
http().client(ENV).post("/xx/v1/ouxxtbound/tel%3A%2B94xxxxxxx")
.header("Authorization", authorization)**strong text**
.header("Accept", "application/json")
.payload(send)
.contentType("application/json");
its is passing a url encoded values, but when it encode again when sending request by Citrus.as tel%253A%252B94xxxxxxx
Is there are way to send encoded URI correctly?
Citrus uses the following method on the Spring RestTemplate
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) throws RestClientException {
...
}
The url is given as String value and Spring will automatically encode this value. When passing in some already encoded String the encoding is done twice. When using non-encoded String value the Spring RestTemplate applies uriVariables logic which is also causing errors.
Citrus should use some other method signature on the RestTemplate that uses the URL object instead of String value. As a temporary workaround you can use a custom RestTemplate subclass that overwrites the methods like this and automatically creates the URL object from String:
#Override
public <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Object... uriVariables) throws RestClientException {
return super.exchange(URI.create(url), method, requestEntity, responseType);
}
You can add the custom RestTemplate subclass as Spring bean into the configuration and reference the bean on the Citrus client component by using the attribute rest-template.

Custom tags with spyne

Im trying to set up a small SOAP 1.1 server with twisted and spyne, but I can't seem to find anything on how to create custom tags(body), namespaces, or headers for my responses. Is there a better way to do this than creating my own ProtocolBase?
The goal is to have soap responses that look like this:
<?xml version="1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cwmp="urn:dslforum-org:cwmp-1-2">
<SOAP-ENV:Header>
<cwmp:ID SOAP-ENV:mustUnderstand="1">123456789</cwmp:ID>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<cwmp:SetParameterValues>
<ParameterList SOAP-ENC:arrayType="cwmp:ParameterValueStruct[1]">
<ParameterValueStruct>
<Name>MY.NAME</Name>
<Value xsi:type="xsd:unsignedInt">4000</Value>
</ParameterValueStruct>
</ParameterList>
<ParameterKey>Axess Update parameters</ParameterKey>
</cwmp:SetParameterValues>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Im able to produce what I'm looking for by creating my own protocol for which I hand a string of xml similar to below.
class spyne(ServiceBase):
isLeaf = TwistedWebResource
#rpc(AnyDict, AnyDict, _returns=Unicode)
def my_rpc(ctx, DeviceId, ParameterList):
out_document = etree.parse('data.xml')
return etree.tostring(out_document, pretty_print=True,
encoding='utf8', xml_declaration=False)
class my_protocol(XmlDocument):
mime_type = 'text/xml; charset=utf-8'
type = set(XmlDocument.type)
type.update(('soap', 'soap11'))
def __init__(self, *args, **kwargs):
super(TR_069_SOAP, self).__init__(*args, **kwargs)
def serialize(self, ctx, message):
ctx.out_document = ctx.out_object[0]
def create_out_string(self, ctx, charset=None):
ctx.out_string = [ctx.out_document]
I'm not sure if there is a better way to be doing this.
Spyne does not support serializing array using the SOAP-ENC:arrayType="cwmp:ParameterValueStruct[1]" style, whatever that means.
You don't need your own protocol but you do need to override XmlDocument's complex_to_parent in Soap11 and add special handling for arrays that have, say, Array(SomeType, array_type="cwmp:ParameterValueStruct[1]").
You can either patch Spyne itself and send a pull request my way or create a Soap11 subclass (NOT XmlDocument subclass) of your own and write overriding code there.
Chime in at http://lists.spyne.io/listinfo/people if you wish to proceed either way.

XSD2Code namespace issue

I am using XSD2Code to generate C# class from XSD file.
I got stuck with the following problem.
XML file looks something like
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Notification xmlns="http://message.domain.com">
<Object xmlns="http://type.domain.com" ID="97440" />
</Notification>
XML gets succefsully deserialized when xmls for Object is empty. But when there is a value like in the sample above, I get an error "Object reference not set to an instance of an object".
What could cause this error?
you have to change the Serializer to something like that
private static System.Xml.Serialization.XmlSerializer Serializer
{
get
{
if ((serializer == null))
{
serializer = new System.Xml.Serialization.XmlSerializer(typeof(Notification), "http://message.domain.com");
}
return serializer;
}
}
To turn off encoding, disable encoding on the Serialization tab

URL Mapping a Rest Webservice

I have to map a REST Webservice URL like "http://server:8080/application/service/customer/v1"
to createCustomer method in CreateCustomerBean class..
I have done the following mappings..
*Web.xml*
<servlet-mapping>
<servlet-name>RestiveServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
*Rest-Config.xml*
<jaxrs:server address="/customer/"
<jaxrs:serviceBean>
<ref bean="CreateCustomerBean"/>
</jaxrs:serviceBean>
</jaxrs:server>
Now the problem is the remaining path of the url("http://server:8080/application/service/customer/v1") is v1 rest all is mapped and i have to specify 2 #Path attributes one at the CreateCustomerBean class level and one at the createCustomer method in that bean.. so i have to append "create" before v1 .. and the url becomes
#Path (/create/)
CreateCustomerBean{
#Path(/v1)
createClient(String request){
}
}
http://server:8080/application/service/customer/create/v1/ which i dont want.. is there any way to avoid the #Path attribute at the class level and direct all the request to the createCustomer method.
In you code you can re-write code like this
#Path ("/v1")
CreateCustomerBean{
#Post
createClient(String request){ }
}
As long as you specify the POST attribute all the post request should be re-directed to the respective method.
Hope it helps.
Cheers