cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X

Preventing Authorization on Customization

bmessenger
7-Bedrock

Preventing Authorization on Customization

Hi,

I've written some code that works well in creating a document and uploading an attachment. I can run it from the command line but whenever it reaches the point in the code where it references a PersistenceHelper I get prompted for authorization. This makes the whole thing pretty useless because my next step is to call it from a .NET application after turning it intoa web service.

Does anybody know how to prevent this authorization request from popping up?

I have tried

     myServer = RemoteMethodServer.getDefault();

     myServer.setUserName("login");

     myServer.setPassword("password");

but this makes no difference.

For instance this line

QueryResult qr = PersistenceHelper.manager.find((StatementSpec)qs);

brings up a box asking for user and password. I can't have this when calling remotely. It defeats the purpose of having an automatic customized project.

Thanks

Ben

3 REPLIES 3

Ben,

Take a look at this answer from Stackoverflow: http://stackoverflow.com/a/21319453

Hope this help.

Hi Iuri,

I had looked at that item in stackoverflow.com and all it di was refer to the same issue. The solution was to ensure you had your code server side, which I had and it does not make an iota of difference. I have tried that many dozens of times and you ALWAYS get the prompt. So I raised a case with PTC and they responded with this which does in fact work.

In this case, we will use below code format to avoid authentication. You can try to put your required code to this format to avoid this situation.

Please this help for you.

package ext.test;

import java.io.*;

import java.util.*;

import java.sql.*;

import java.text.*;

import wt.util.*;

import wt.inf.container.*;

import wt.pom.*;

import wt.fc.*;

import wt.query.*;

import wt.method.*;

import wt.session.*;

public class getTest implements Serializable, RemoteAccess {

public void execute() throws Exception {

if (!RemoteMethodServer.ServerFlag){

RemoteMethodServer ms = RemoteMethodServer.getDefault();

Class[] argTypes = {};

Object[] argValues = {};

ms.invoke("execute", null, this, argTypes, argValues);

return;

}

        SessionContext.newContext();

SessionHelper.manager.setAdministrator();

// write what are you want to execute - Start

System.out.println();

System.out.println();

System.out.println("####### Start --- getTest");

Transaction trans = new Transaction();

try {

trans.start();

int rand = new Random().nextInt();

WTContainerRef ref = WTContainerHelper.service.getByPath("wt.inf.container.OrgContainer=Demo Organization/wt.pdmlink.PDMLinkProduct=Test_Product");

AdministrativeDomain adminDomain =  ref.getContainer().getDefaultDomain();

AdminDomainRef ref_admi = AdminDomainRef.newAdminDomainRef(adminDomain);

/* wt.epm.EPMDocument|com.ptc.ptcnet.DynamicDocument|com.ptc.ptcnet.Note is a EPMDocument soft type */

LWCNormalizedObject obj = new LWCNormalizedObject("WCTYPE|wt.epm.EPMDocument|com.ptc.ptcnet.DynamicDocument|com.ptc.ptcnet.Note",null, new CreateOperationIdentifier());

//obj.load("name","containerReference","CADName","authoringApplication","ownerApplication","docType","domainRef");

obj.load("name","containerReference","CADName","authoringApplication","ownerApplication","docType","domainRef","Test_IBA");

obj.set("name","Test_Test_TS-"+ rand);

obj.set("containerReference",ref);

obj.set("CADName","Test_Test_TS#-"+rand);

obj.set("authoringApplication","WINDCHILL");

obj.set("ownerApplication","EPM");

obj.set("docType","NOTE");

obj.set("domainRef",ref_admi);

obj.set("Test_IBA","Test_IBA_Value");

obj.persist();

System.out.println("####### obj has been persisted");

String nameValue = (String)obj.get("name");

System.out.println("####### nameValue --- " + nameValue.toString());

EPMDocument epm = (EPMDocument)getLatestIterationOfEPMDocument(nameValue);

System.out.println("####### epm : " + epm.toString());

trans.commit();

trans = null;

}

catch(Throwable t) {

throw new ExceptionInInitializerError(t);

} finally {

if(trans != null) {

trans.rollback();

}

}

System.out.println("####### End --- getTest ");

System.out.println();

System.out.println();

// write what are you want to execute - End

}

public static void main(String[] args) {

try {

getTest serviceInvoke = new getTest();

serviceInvoke.execute();

System.exit(0);

} catch (Exception e) {

System.exit(-1);

}

}

}



This will use the wcadmin user. If you wish to use a different user, I have discovered through trila and error that you can use

SessionHelper.manager.setPrincipal("someuserlogin");

I hope this helps others.

Regards

Ben

Hi Ben,

You can simply write a Authenticator class

package com.auth;

import java.net.Authenticator;
import java.net.PasswordAuthentication;

public class AuthCred extends Authenticator{

@Override
protected PasswordAuthentication getPasswordAuthentication() {

     String username = "name";

    String password = "password";

    return new PasswordAuthentication(username, password.toCharArray());
}
}

Write the line below at the start point of your custom code, hopefully it will work

Authenticator.setDefault(new AuthCred());

Please let me know if this helped.

Thanks,

Aditya

Top Tags