php soap client gets Server java.lang.NullPointerException when authentication is ok - java

If You are sending an object to a webmethod to use it as prameter:
Example: sending an object that will be represented as a class in the server like this:
$creditCard = new stdClass();
$creditCard->number="705";
$creditCard->expiryDate="03/10/2019";
$creditCard->controlNumber=9;
$param = array("creditCard" => $creditCard);
$Response = $client->__soapCall('valider', array($param));
You won't get any property of the class CreditCard that is in the server to work, and every time you do creditClass.getNumber() or trying to use accessors you will get a null pointer error because your object is not really created.

To fix this problem you have to modify the server webservice and not the client: you have to add the trivial jax-ws annotation #webParam:
public String valider(#WebParam(name = "creditCard")CreditCard creditCard){ ... }
And your PHP soapClient code is very simple: (if you want more security you have to encode your credentials or use an other way for authentication like digest authentication or other jax ws security implementations)
try {
$opts = array (
'http' => [
'header' => "username: myrealname\r\n".
'password: pass1myrealpassword']
);
$par= array(
'trace' => 1,
'exceptions' =>0,
'soap_version' => SOAP_1_1,
'connection_timeout' => 1800,
'stream_context' => stream_context_create($opts),
);
$client = new SoapClient($wsdl,$par);
//...... and call your service method as you do

Related

How to print byte array

I am trying to print some data which is in an array format. But while printing I am loosing my initial byte of the array which I need. How can I get that?
rray
(
[0] => stdClass Object
(
[company_category] => Company limited by Shares
[email_id] => example#domain.com
[last_processed] => 2016-07-01T08:42:34Z
[company_class] => Public
[country] =>
)
)
As you will be seeing that starting of the output should have be "Array" but it is not. What am I doing wrong here. As I am sending a request to a specific URL and getting a response from it. Below is the code I use to get the reponse
HttpEntity entity = CloseableHttpResponse.getEntity();
String response = EntityUtils.toString(entity,"UTF-8");
System.out.println(response);

Manipulate AWS base URL in the SDK for Java

I'm trying to change the base URL of the the AWS SDK for Java.
It is possible with the SDK for PHP like this:
require 'vendor/autoload.php';
use Aws\DynamoDb\DynamoDbClient;
// Create a client that that contacts a completely customized base URL
$client = DynamoDbClient::factory(array(
'endpoint' => 'http://my-custom-url',
'region' => 'my-region-1',
'credentials' => array(
'key' => 'abc',
'secret' => '123'
)
));
It's also possible to set this up for s3cmd in .s3conf:
host_base = s3.mylocalaws.com
host_bucket = %(bucket)s.s3.mylocalaws.com
I can't figure out how this works for the Java SDK.
I've tried this but the result is not https://s3.mylocalaws.com/bucketName/key as I expected but https://bucketName.s3.mylocalaws.com
AmazonS3 s3Client = new AmazonS3Client(new AWSTestCredentialsImpl());
s3Client.setEndpoint("https://s3.mylocalaws.com");
S3Object resource = s3Client.getObject(
new GetObjectRequest(bucketName, key));
Look at Choosing a Specific Endpoint in the documentation.
AmazonEC2 ec2 = new AmazonEC2(myCredentials);
ec2.setEndpoint("https://ec2.eu-west-1.amazonaws.com");

How to access wsdl in grails - 400 status code return

I want to send the data to person object. How to do it with PostMethod.
def payload ='<person><nationalId>'+1234567+'</nationalId></person>'
def method = new PostMethod(url)
def client = new HttpClient()
payload = payload.trim()
method.addRequestHeader("Content-Type","text/xml")
method.addRequestHeader("Accept","text/xml,application/xml;q=0.9")
Credentials credentials = new UsernamePasswordCredentials('simple', 'simple');
client.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST,8080, AuthScope.ANY_REALM, "digest"),credentials);
method.setRequestEntity(new StringRequestEntity(payload))
def statusCode = client.executeMethod(method)
println "STATUS CODE : ${statusCode}"
def resultsString = method.getResponseBodyAsString()
method.releaseConnection()
println resultsString
I tried above coding. How to set password and username and password digest also. For that i think status code 400 is coming.Please notify where i made mistake
Try to look at REST Client Builder Plugin. See docs and I guess, you'll find more convenient way to send request

PHP SoapClient against Java Server

I am at the end of my ropes so its time to ask the community for help, i've been pillaging the web for any resource I can find on this issue but none helps me.
I am currently trying to communicated with a Java Webservice server through PHP. I can pull down a WSDL and list its functions and types through __getFunctions(); and __getTypes();
I am creating the client in this format
new SoapClient("https://username:password#ip:port/path/to/wsdl?wsdl");
Now initially this poses no problems, but when I try to make a function call on this service I get 1 of 2 responses.
I know one of them is a timeout error, the second one I do not understand as of yet.
Error : SoapFault exception: [SOAP-ENV:Client] [MT-IP] SOAP message is not well formed in...
Here is the code:
// All of this works
$options["login"] = "login";
$options["password"] = "password";
$wsdl = "https://" . $options["login"] . ":" . $options["password"] . "#ip:port/path/to/wsdl?wsdl";
$client = new SoapClient($wsdl, $options);
try {
$functions = $client->__getFunctions();
$types = $client->__getTypes();
$params = new stdClass();
$params->pong = (string)"Hello World!";
// This fails
$result = $client->ping($params);
var_dump($result);
} catch (SoapFault $exception) {
echo $exception;
}
To add I have also attempted to call methods in all the ways available such as
$client->__soapCall("ping", array($params));
$client->__soapCall("ping", array(array("pong" => "Hello World!)));
$client->__soapCall("ping", array("pong" => "Hello World"));
$client->__soapCall("ping", array("parameters" => array("pong" => "Hello World"));
// and some more..
You can also see the WSDL I am testing against
WSDL
You can also view the page where I try to call webservice
SOAP TEST
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="com.computas.mt.extern.Ping">
<SOAP-ENV:Body>
<ns1:ping>
<pong>Hello World!</pong>
</ns1:ping>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
In my case the schematics the server wanted was different than that of the request PHP was sending out. The solution was to physically change the request XML before it was sent out by extending the SoapClient. We found the changes to the issue once we had the XML layout that the server usually gets provided with.
class MySoapClient extends SoapClient {
function __doRequest( $request, $location, $action, $version, $one_way = NULL ) {
$request = str_replace("SOAP-ENV", "soapenv", $request);
$request = str_replace("xsi", "com", $request);
$request = str_replace("ns1", "com", $request);
var_dump($request);
return parent::__doRequest( $request, $location, $action, $version, $one_way );
}
}
Do not put username and password into the url. SoapClient accepts an option array that offers parameters for this purpose. http://de2.php.net/manual/en/soapclient.soapclient.php

How to retrieve associated products for configurable product with Magento SOAP API2?

I'm trying to get all the associated products for a configurable product using the Magento SOAP API v2. The catalogProductLink call looks close, but doesn't handle configurable types. I don't see any other calls that contain the associated product and configurable type information for a product. How have others solved this issue?
I'm using Magento version 1.6 and the SOAP API V2 with Java.
I looked deeper into this solution and realized that you may need to override the API model (Mage_Catalog_Model_Product_Api) to achieve the results you're looking for.
In the items function (around line 90), you can do something like this:
foreach ($collection as $product) {
$childProductIds = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($product->getId());
$result[] = array(
'product_id' => $product->getId(),
'sku' => $product->getSku(),
'name' => $product->getName(),
'set' => $product->getAttributeSetId(),
'type' => $product->getTypeId(),
'category_ids' => $product->getCategoryIds(),
'website_ids' => $product->getWebsiteIds(),
'children' => $childProductIds[0],
);
}
create the folder app/code/local/Mage/Catalog/Model/Product/Link
copy the app/code/core/Mage/Catalog/Model/Product/Link/Api.php into this folder and edit it. (I’ve only done the change for the V1 but I’m pretty sure it’s as easy in V2)
replace the content of the the items() function with the following
if($type == "associated"){
$product = $this->_initProduct($productId);
try
{
$result = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);
}
catch (Exception $e)
{
$this->_fault('data_invalid', Mage::helper('catalog')->__('The product is not configurable.'));
}
}
else
{
$typeId = $this->_getTypeId($type);
$product = $this->_initProduct($productId, $identifierType);
$link = $product->getLinkInstance()
->setLinkTypeId($typeId);
$collection = $this->_initCollection($link, $product);
$result = array();
foreach ($collection as $linkedProduct) {
$row = array(
'product_id' => $linkedProduct->getId(),
'type' => $linkedProduct->getTypeId(),
'set' => $linkedProduct->getAttributeSetId(),
'sku' => $linkedProduct->getSku()
);
foreach ($link->getAttributes() as $attribute) {
$row[$attribute['code']] = $linkedProduct->getData($attribute['code']);
}
$result[] = $row;
}
}
return $result;
then you can call the API like this now:
$client->call($sessionId, 'product_link.list', array('associated', $id_of_your_configurable_product));
Basically my code is checking the type provided and if it’s “associated” it returns the child products.
I’m pretty sure there’s better way of doing it but I thought that the Product Link API was the most relevant place to do it.
Enjoy!
(please note: this code is not mine, i just adapted it and thought it would be a nice idea to help you guys out)

Categories