Me parece que a URL está correta, pois o endpoint da API está aceitando as credenciais, caso eu mande diferente dá 401 e parece estar conforme a documentação, o problema é que a resposta está vindo com o conteudo do InputStream Null, como se necessitasse de algum header explicitando aceitar resposta, como eu faço isso em java ? Acho que preciso tipo de um “observe: response” como no Angular. Alguém tem a solução?
Na linha onde eu pego o Element do documento vem assim (session: null) (era para vir um ID)
public class SessionService {
/**
* @var Log
*/
private static Log log = new Log(SessionService.class);
private static String buildSessionRequestUrl(ConnectionData connectionData) //
throws PagSeguroServiceException {
String url = connectionData.getPaymentSessionUrl() + "?" + connectionData.getCredentialsUrlQuery();
System.out.println(url);
return url;
}
public static String createSession(Credentials credentials) //
throws PagSeguroServiceException {
log.info("SessionService.createSession() - begin");
ConnectionData connectionData = new ConnectionData(credentials);
String url = SessionService.buildSessionRequestUrl(connectionData);
HttpConnection connection = new HttpConnection();
HttpStatus httpCodeStatus = null;
HttpURLConnection response = connection.post(url,connectionData.getCredentials().getAttributes(), //
connectionData.getServiceTimeout(), //
connectionData.getCharset());
try {
httpCodeStatus = HttpStatus.fromCode(response.getResponseCode());
System.out.println( response.toString());
if (httpCodeStatus == null) {
throw new PagSeguroServiceException("Connection Timeout");
} else if (HttpURLConnection.HTTP_OK == httpCodeStatus.getCode().intValue()) {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
InputSource inputSource = new InputSource(response.getInputStream());
Document document = documentBuilder.parse(inputSource);
Element element = document.getDocumentElement();
// setting <startPaymentSessionResult><sessionId>
String sessionId = XMLParserUtils.getTagValue("session", element);
log.info("SessionService.createSession() - end");
return sessionId;
} else if (HttpURLConnection.HTTP_BAD_REQUEST == httpCodeStatus.getCode().intValue()) {
List<Error> errors = ErrorsParser.readErrosXml(response.getErrorStream());
PagSeguroServiceException exception = new PagSeguroServiceException(httpCodeStatus, errors);
log.error(String.format("SessionService.createSession() - error %s", //
exception.getMessage()));
throw exception;
} else if (HttpURLConnection.HTTP_UNAUTHORIZED == httpCodeStatus.getCode().intValue()) {
PagSeguroServiceException exception = new PagSeguroServiceException(httpCodeStatus);
log.error(String.format("SessionService.createSession() - error %s", //
exception.getMessage()));
throw exception;
} else {
throw new PagSeguroServiceException(httpCodeStatus);
}
} catch (PagSeguroServiceException e) {
throw e;
} catch (Exception e) {
log.error(String.format("SessionService.createSession() - error %s", //
e.getMessage()));
throw new PagSeguroServiceException(httpCodeStatus, e);
} finally {
response.disconnect();
}
}
}
CONNECTION POST QUE É CHAMADO
public HttpURLConnection post(String urlPS, Map<Object, Object> data, String timeout, String charset)
throws PagSeguroServiceException {
HttpURLConnection connection = getConnection(urlPS, timeout, charset, "POST");
try {
// Send POST data
OutputStream out = connection.getOutputStream();
Writer write = new OutputStreamWriter(out, charset);
write.write(PagSeguroUtil.urlQuery(data));
write.close();
out.close();
return connection;
} catch (IOException e) {
log.error("Error when trying execute method connection: " + e.getMessage());
throw new PagSeguroServiceException("Error when trying write or set request method", e);
}
}